import {ToasterService} from 'angular2-toaster';
import {AppComponent} from '../../app.component';
import {FrameThreadRunningComponent} from '../../routes/frame/thread/frame-thread-running/frame-thread-running.component';
import {BsModalService} from 'ngx-bootstrap';

/********************************
 *  消息服务，包括右下脚消息，弹框服务
 */
// 加载弹框服务
const swal = require('sweetalert');

export class MsgService {

  public static TOASTER: ToasterService = null;
  public static APP_COMPONENT: AppComponent = null;

  /********************
   * 显示操作成功消息
   */
  public static successToaster(msg) {
    MsgService.TOASTER.pop('success', '', msg);
  }

  /********************
   * 显示错误信息
   */
  public static errorToaster(msg) {
    MsgService.TOASTER.pop('error', '', msg);
  }

  /********************
   * 显示警告信息
   */
  public static warnToaster(msg) {
    MsgService.TOASTER.pop('warning', '', msg);
  }

  /********************
   * 显示Loading
   */
  public static showLoading() {
    MsgService.APP_COMPONENT.isLoading = true;
  }

  /********************
   * 隐藏Loading
   */
  public static hideLoading() {
    MsgService.APP_COMPONENT.isLoading = false;
  }

  public static confirm(msg, callback) {
    swal(msg, {
      buttons: {
        cancel: `取消操作`,
        catch: {
          text: '确认操作',
          value: 'catch',
        }
      },
    })
      .then((value) => {
        switch (value) {
          case 'catch':
            swal.close();
            callback();
            break;
        }
      });
  }

  public static errorAlert(msg: string, title?: string, callback?: Function) {
    swal({
      title: title,
      text: msg,
      icon: 'error',
      button: {
        text: '知道了 ...',
        value: true,
        visible: true,
        closeModal: true,
      }
    }).then((value) => {
      if (callback) {
        callback();
      }
    });
  }

  public static successAlert(msg: string, title?: string, callback?: Function) {
    swal({
      title: title,
      text: msg,
      icon: 'success',
      button: {
        text: '知道了 ...',
        value: true,
        visible: true,
        closeModal: true,
      }
    }).then((value) => {
      if (callback) {
        callback();
      }
    });
  }

  public static showThreadRunning(bsService: BsModalService, thread: any, data?: any, param?: any) {
    const modal = Object.assign({}, {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-6'
    });
    if (!data) {
      data = {};
    }
    modal.initialState = data;
    // modal.initialState['FramePageComponent'] = this;
    modal.initialState['frameThread'] = thread;
    modal.initialState['threadParam'] = param;
    bsService.show(FrameThreadRunningComponent, modal);

  }
}
