import { Component, Label, Node, Vec3, _decorator, instantiate, tween } from 'cc'

import { GameConfig } from '../../game/config/GameConfig'
import PrefabLoader from '../utils/PrefabLoader'

import DialogBase from './DialogBase'

const { ccclass, property } = _decorator

@ccclass('CommonTips')
export default class CommonTips extends Component {
  public static TipsZorderIndex: number = 999
  @property({ type: Label })
  txtContent: Label | null = null

  private tips: string = ''
  private static showingNameList: Array<string> = []

  onLoad() {}

  start() {
    tween(this.node)
      .by(1.5, { position: new Vec3(0, 100, 0) })
      .to(0.2, { /* opacity: 0 */ scale: Vec3.ZERO })
      .call(() => {
        this.node.destroy()
      })
      .start()
  }

  onDestroy() {
    const index: number = CommonTips.showingNameList.indexOf(this.tips)
    CommonTips.showingNameList.splice(index, 1)
    this.unscheduleAllCallbacks()
  }

  public static showMsg(msg: string, parentNode: Node = null) {
    PrefabLoader.loadPrefab(
      `${GameConfig.GameName}/share/uicomponent/CommonTips`,
      (loadedResource) => {
        parentNode = parentNode || DialogBase.GetRootCanvas()
        if (!CommonTips.showingNameList.includes(msg)) CommonTips.showingNameList.push(msg)

        const dialogNode = instantiate(loadedResource)
        dialogNode.setPosition(0, 0)
        const dialogScript: CommonTips = dialogNode.getComponent(CommonTips)
        dialogScript.tips = msg
        dialogScript.txtContent.string = msg
        parentNode.insertChild(dialogNode, CommonTips.TipsZorderIndex)
      },
    )
  }
}
