import { Component, Node, Prefab, Widget, _decorator, instantiate } from 'cc'

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

import DialogBase from './DialogBase'

const { ccclass, property } = _decorator

@ccclass('DarkLayer')
export default class DarkLayer extends Component {
  private static prefab: Prefab

  onLoad() {
    this.getComponent(Widget).target = DialogBase.GetRootCanvas()
  }

  start() {}

  public static preLoad(): Promise<void> {
    return new Promise((resolve, reject) => {
      PrefabLoader.loadPrefab(
        `${GameConfig.GameName}/share/uicomponent/DarkLayer`,
        (loadedResource) => {
          DarkLayer.prefab = loadedResource
          resolve()
        },
      )
    })
  }

  public static getDarkLayer() {
    const dialogNode: Node = instantiate(DarkLayer.prefab)
    return dialogNode
  }
}
