import {Component, Input, OnDestroy, OnInit} from '@angular/core';
import {BsModalRef} from "ngx-bootstrap/modal";
import {AjaxService} from "../../../../core/service/ajax.service";
import {MsgService} from "../../../../core/service/msg.service";
import {FrameDetailComponent} from "../../core/detail/frame.detail";
import {FuncService} from "../../../../core/service/func.service";

@Component({
  selector: 'app-frame-thread-running',
  templateUrl: './frame-thread-running.component.html',
  styleUrls: ['./frame-thread-running.component.scss']
})
export class FrameThreadRunningComponent extends FrameDetailComponent implements OnInit, OnDestroy {

  threadParam: any;
  frameThread: any;
  /*********************
   * 去服务器获取线程间隔时长，单位秒
   */
  intervalSeconds = 3;
  /*********************
   * 是否允许操作员解除当前界面
   */
  allowCancel = false;
  /**********************
   * 信息显示模式，
   */
  msgType = 'array';
  /*********************
   * 标题
   */
  runningTitle = '后台线程运行中，请稍后';


  /*************************
   * 消息队列
   */
  msgArray = new Array();


  // 定时器，用于轮询时使用
  private intervalId: any;


  constructor(bsModalRef: BsModalRef) {
    super(bsModalRef);
    this.frameThread = {}
  }


  ngOnInit() {
    this.intervalId = setInterval(this.queryThreadStatus.bind(this), this.intervalSeconds * 1000);
  }

  initDefautEntity() {
    this.frameThread = this.currentEntity;
    this.frameThread.thread_percent = 1;
  }

  ngOnDestroy(): void {
  }

  queryThreadStatus() {
    AjaxService.requestEntity('frame/thread/find.htm', {thread_id: this.currentEntity.thread_id}, (entity) => {
      // 如果线程结束
      if (entity.thread_status === 'Complete' || entity.thread_status === 'Error') {
        clearTimeout(this.intervalId);
      }
      // 判断是否消息有更新
      if (this.frameThread.thread_notice === '' || entity.thread_notice !== this.frameThread.thread_notice) {
        if (this.msgType === 'entity') {
          this.msgArray = [entity];
        } else {
          this.msgArray.push(entity);
        }
      }
      this.frameThread = entity;
      if (entity.thread_status === 'Running') {
        // 继续，等待下一次轮询
      } else if (entity.thread_status === 'Complete') {
        if (entity.callback_type === 'Download') {
          // 如果是下载文件
          let json = eval('(' + entity.callback_param + ')');
          // AjaxService.ajaxDownload('frame/temporary/download.htm', JSON.parse( this.frameThread.callback_param));
          FuncService.fileDownload('frame/template/download.htm', json);
          MsgService.successAlert('任务已完成，该任务将返回一个文件，我们已经在新窗口开始了文件下载，请留意下载进度！');
        } else if (entity.callback_type === 'Swal') {
          // 如果是弹出框
          MsgService.successAlert(entity.callback_param);
        }
        // 判断是否需要回调
        if (this.threadParam && this.threadParam.callback) {
          this.threadParam.callback.call(undefined);
        }
      } else if (entity.thread_status === 'Error') {
        // 如果是错误，弹出错误提示
        MsgService.errorAlert(entity.callback_param);
      }
    });
  }

  /******************
   * 关闭窗体
   */
  closeWindow() {
    clearTimeout(this.intervalId);
    super.closeWindow();
  }

}

