import Vue from "vue";
import store from "@/store";
import { redirectIndex } from "@/router";
import { QSpinnerHourglass } from "quasar";
import bus from "@/config/event-bus";

// --------「服务于通用而不是组件内部 」: 分平台全局提示 UI -------- //

const progress = {};
const _env = process.env;
const _vm = Vue.prototype;

// store 赋值必定是晚于页面直接访问, 做一个兼容
let _native = false;
setTimeout(() => {
  _native = store?.state?.config?.native;
}, 200);

// 若同时存在多个组件库, 公共位置优先引用库
const quasar = _env.VUE_APP_PRIORITY == _env.VUE_APP_UI_QUASAR;
const elementUI = _env.VUE_APP_PRIORITY == _env.VUE_APP_UI_ELEMENT;

// loading 显示
progress.showLoading = function (message = "拼命加载中...") {
  // 避免多个可用对象存在, 尽量避免闪屏
  if (this.$loading) return;
  if (quasar) {
    this.$loading = _vm.$q.loading;
    this.$loading.show({
      message,
      spinner: QSpinnerHourglass
    });
  }
  if (elementUI) {
    this.$loading = _vm.$el_loading({
      lock: true,
      text: message,
      fullscreen: true,
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.3)"
    });
  }
};

// loading 关闭
progress.hideLoading = function () {
  if (!this.$loading) return;
  if (quasar) this.$loading.hide();
  if (elementUI) this.$loading.close();
  this.$loading = null;
};

// 错误提示提示
progress.showErrorMessage = function (message, duration = 2000) {
  if (!message) return;
  if (quasar) {
    _vm.$q.notify({
      message,
      color: _native ? "" : "negative",
      icon: "warning",
      position: _native ? "center" : "top",
      timeout: duration
    });
  }
  if (elementUI) {
    _vm.$el_message.error({
      message,
      duration,
      icon: "danger"
    });
  }
};

// 成功提示信息
progress.showSuccessMessage = function (message, duration = 2000) {
  if (!message) return;
  if (quasar) {
    _vm.$q.notify({
      message,
      color: "teal",
      icon: "done_all",
      position: _native ? "center" : "top",
      timeout: duration
    });
  }
  if (elementUI) {
    _vm.$el_message({
      type: "success",
      message,
      duration
    });
  }
};

// token 失效处理
progress.invalidToken = async function (message) {
  await store.dispatch("user/resetToken");
  redirectIndex(message); // 若当前路由非首页, 提示登录失效信息, 跳转首页
  bus.$vm.$emit(bus.$sel.login_timeout);
  // bus.$vm.$on(bus.$sel.login_timeout, () => { }); // 响应抛出登录失效
};

// 弹出确认框
progress.dialogConfirm = function (title, message) {
  return new Promise((resolve, reject) => {
    if (quasar) {
      _vm.$q
        .dialog({
          title,
          message,
          ok: "确认",
          cancel: "取消"
        })
        .onOk(() => {
          resolve();
        })
        .onCancel(err => {
          reject(err);
        });
    }
    if (elementUI) {
      _vm
        .$el_confirm(message, title, {
          type: "warning"
        })
        .then(
          () => {
            resolve();
          },
          err => {
            reject(err);
          }
        );
    }
  });
};

// import progress from '@/service/progress'
export default progress;
