import {TranslateService} from '@ngx-translate/core';
import {VarService} from "./var.service";


// 加载弹框服务
const swal = require('sweetalert');

/*********************
 * 提供全局消息服务
 */
export class MsgService {

  /***********************
   * 右下角操作 成功提示
   */
  public static successToastr(msg: string) {
    VarService.TOASTR.success(msg);
  }

  /***********************
   * 右下角操作 警告提示
   */
  public static warningToastr(msg: string) {
    VarService.TOASTR.warning(msg);
  }

  /***********************
   * 右下角操作 错误提示
   */
  public static errorToastr(msg: string) {
    VarService.TOASTR.error(msg);
  }

  /********************
   * 显示Loading
   */
  public static showLoading() {
    VarService.SHOW_LOADING = true;
  }

  /********************
   * 隐藏Loading
   */
  public static hideLoading() {
    VarService.SHOW_LOADING = false;
  }

  /**************************
   * 单一输入框，用于简单的参数输入
   */
  public static input(msg: string, callback: Function) {
    swal(msg, {
      content: "input",
    }).then((value) => {
      callback.call(undefined, value);
    });
  }

  /***********************
   * 弹框确认操作
   */
  public static confirmAlert(msg: string, callback: Function) {
    swal(msg, {
      buttons: {
        cancel: MsgService.translateText(`msg.button.cancel`),
        catch: {
          text: MsgService.translateText(`msg.button.submit`),
          value: 'catch',
        }
      },
    })
      .then((value) => {
        switch (value) {
          case 'catch':
            swal.close();
            callback();
            break;
        }
      });
  }

  /******************************
   * 弹框 错误提示
   */
  public static errorAlert(msg: string, callback?: Function) {
    MsgService.curstomAlert('error', '', msg, callback);
  }

  /******************************
   * 弹框 正确提示
   */
  public static successAlert(msg: string, callback?: Function) {
    MsgService.curstomAlert('success', '', msg, callback);
  }

  /******************************
   * 弹框 提醒提示
   */
  public static warningAlert(msg: string, callback?: Function) {
    MsgService.curstomAlert('warning', '', msg, callback);
  }

  /******************************
   * 弹框 正常提示
   */
  public static infoAlert(msg: string, callback?: Function) {
    MsgService.curstomAlert('info', '', msg, callback);
  }

  /******************************
   * 自定义弹框提示
   */
  public static curstomAlert(type: string, msg: string, title: string, callback?: Function) {
    swal({
      title: title,
      text: msg,
      icon: type,
      button: {
        text: MsgService.translateText(`msg.button.know`),
        value: true,
        visible: true,
        closeModal: true,
      }
    }).then((value) => {
      if (callback) {
        callback();
      }
    });
  }

  /*************************
   * 国际化
   */
  public static translateText(text: string) {
    return VarService.INJECTOR.get(TranslateService).instant(text);
  }




}
