import Vue from 'vue'
import MiniUtil from '@/util/mini'
import store from '@/store'
import i18n from '@/util/translate'

class DialogUtil {
  public static showDialog(vueTemplate: any, vueName: string, data: any) {
    const flag = MiniUtil.isHasMini(vueName)
    if (flag) this.closeMini(vueName)
    const showComponents = Vue.prototype.$showComponents
    const compent = showComponents.get(vueName)
    if (!compent) {
      const MountComponent = Vue.extend(vueTemplate)
      const vm = new MountComponent({
        propsData: {
          tempData: data
        },
        store,
        i18n
      })
      const menug = vm.$data.menuGroup
      if (menug) {
        const group: Map<string, any> = Vue.prototype.$menuGroup
        if (!group.has(menug)) {
          group.set(menug, vueName)
        } else {
          this.closeCom(group.get(menug))
          group.set(menug, vueName)
        }
      }
      vm.$mount(document.createElement('div'));
      (document.getElementById('ship-type-box') as HTMLElement).append(vm.$el)
      showComponents.set(vueName, vm)
    } else {
      compent.$props.tempData = data
      compent.$forceUpdate()
    }
  }

  /**
   * 查看组件是否显示
   * @param vueName
   */
  public static isShow(vueName: string) {
    const showComponents = Vue.prototype.$showComponents
    const component = showComponents.get(vueName)
    if (component) {
      return component.$options.propsData.tempData.show
    } else {
      return false
    }
  }

  /**
   * 隐藏组件
   * @param vueName 组件名称
   */
  public static hideCom(vueName: string) {
    const showComponents = Vue.prototype.$showComponents
    const component = showComponents.get(vueName)
    if (component) {
      component.$props.tempData.show = false
      component.$forceUpdate()
    }
  }

  /**
   * 显示组件
   * @param vueName 组件名称
   */
  public static showCom(vueName: string) {
    const showComponents = Vue.prototype.$showComponents
    const component = showComponents.get(vueName)
    if (component) {
      component.$props.tempData.show = true
      component.$forceUpdate()
    }
  }

  /**
   * 添加最小化显示
   * @param vueName
   */
  public static showMini(vueName: string) {
    const showComponents = Vue.prototype.$showComponents
    const component = showComponents.get(vueName)
    MiniUtil.addMini({
      type: vueName,
      title: component.dialogConfig.header.title
    })
    this.hideCom(vueName)
  }

  /**
   * 关闭最小化显示
   * @param vueName
   */
  public static closeMini(vueName: string) {
    MiniUtil.delMini(vueName)
    this.closeCom(vueName)
  }

  /**
   * 关闭销毁组件
   * @param vueName
   */
  public static closeCom(vueName: string) {
    const showComponents = Vue.prototype.$showComponents
    const component = showComponents.get(vueName)
    if (component) {
      if (MiniUtil.isHasMini(vueName)) {
        MiniUtil.delMini(vueName)
      }
      const el = component.$el
      component.$refs[vueName].cancel()
      el.parentElement.removeChild(el)
      component.$destroy()
      showComponents.delete(vueName)
    }
  }

  public static getCom(vueName: string) {
    const showComponents = Vue.prototype.$showComponents
    return showComponents.get(vueName)
  }
}

export default DialogUtil
