import { config } from "../constants/config"
import { respath } from "../constants/respath"

export class ResourceManager {
  private static instance: ResourceManager
  public static getInstance(): ResourceManager {
    if (!ResourceManager.instance) {
      ResourceManager.instance = new ResourceManager()
    }
    return ResourceManager.instance
  }

  private resources: Array<{ url: string, type?: any, lazy?: boolean }> = []
  private textureReqs: Map<string, Array<Laya.Image>> = new Map()
  private prefabReqs: Map<string, Array<any>> = new Map()

  public init(): void {
    Laya.timer.frameLoop(1, this, () => {
      const removes: Array<any> = []
      this.prefabReqs.forEach((list: Array<any>, path: string) => {
        const obj = Laya.loader.getRes(path)
        if (obj) {
          removes.push(path)
          for (let i = 0; i < list.length; i++) {
            list[i](obj)
          }
        }
      })
      for (let i = 0; i < removes.length; i++) {
        this.prefabReqs.delete(removes[i])
      }
    })
  }

  private started: boolean = false
  public startSchedule(): void {
    if (this.started)
      return
    this.started = true
    this.resources.push({ url: respath.gamepause_ui_res() })
    this.resources.push({ url: respath.gameover_ui_res() })
    this.resources.push({ url: respath.gamedone_ui_res(true) })
    this.resources.push({ url: respath.gamedone_ui_res(false) })
    this.resources.push({ url: respath.score_add_ui_res() })
    this.resources.push({ url: respath.dc_ui_res() })
    this.resources.push({ url: respath.trophy_claim_ui_res() })
    this.resources.push({ url: respath.trophy_show_ui_res() })
    this.resources.push({ url: respath.dc_ui_res() })
    this.resources.push({ url: respath.trophy_ui_res() })
    this.resources.push({ url: respath.trophy_year_ui_res() })
    this.resources.push({ url: respath.trophy_cell_ui_res() })
    if (config.H_SCREEN) {
      this.resources.push({ url: respath.common_tips_ui_res })
    } else {
      this.resources.push({ url: respath.difficulty_ui_res })
    }

    this.resources.push({ url: respath.bg_done, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.bg_done_light, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.bg_done_top, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.icon_star1, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.icon_star2, type: Laya.Loader.IMAGE })

    this.resources.push({ url: respath.home_top_dc_bg_normal, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.home_top_dc_bg_finish, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.dc_top_bg_normal, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.dc_top_bg_finish, type: Laya.Loader.IMAGE })
    const dc_lights = respath.get_dc_lights()
    for (let i = 0; i < dc_lights.length; i++) {
      this.resources.push({ url: dc_lights[i], type: Laya.Loader.IMAGE })
    }

    this.resources.push({ url: respath.trophy_bg_light_gold, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_bg_light_grey, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_01, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_02, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_03, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_04, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_05, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_06, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_07, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_08, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_09, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_10, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_11, type: Laya.Loader.IMAGE })
    this.resources.push({ url: respath.trophy_12, type: Laya.Loader.IMAGE })

    Laya.loader.load(this.resources, Laya.Handler.create(this, (obj: object) => { // 第二个参数：成功回调
      console.log("预加载资源结束>>>>", obj)
      this.textureReqs.forEach((list: Array<Laya.Image>, path: string) => {
        for (let i = 0; i < list.length; i++) {
          const img = list[i]
          if (img && img.parent) {
            img.texture = Laya.loader.getRes(path)
          }
        }
      })
      this.textureReqs.clear()
    }), Laya.Handler.create(this, (resProg: number) => { // 第三个参数：进度回调
      console.log("预加载资源进度>>>>", resProg)
    }))
  }

  public loadTexture(path: string, image: Laya.Image): void {
    const obj = Laya.loader.getRes(path)
    // console.log("loadTexture obj", path, obj)
    image.skin = ""
    if (obj) {
      image.texture = obj
    } else {
      if (!this.textureReqs.has(path)) {
        this.textureReqs.set(path, [])
      }
      const list = this.textureReqs.get(path)
      list.push(image)

      let find = false
      for (let i = 0; i < this.resources.length; i++) {
        const info = this.resources[i]
        if (info.url === path) {
          find = true
          break
        }
      }
      // console.log("loadTexture find", path, find)
      if (find === false) {
        Laya.loader.load(path, Laya.Handler.create(this, (obj: Laya.Texture) => { // 第二个参数：成功回调
          // console.log("加载资源成功>>>>", path, obj, image, image.parent)
          const list = this.textureReqs.get(path)
          for (let i = 0; i < list.length; i++) {
            const img = list[i]
            if (img && img.parent) {
              img.texture = obj
            }
          }
          this.textureReqs.delete(path)
        }))
      }
    }
  }

  public loadPrefab(path: string, callback: any): void {
    const obj = Laya.loader.getRes(path)
    console.log("loadPrefab >>>>>", path, obj)
    if (obj) {
      if (callback) {
        callback(obj)
      }
    } else {
      if (callback) {
        if (!this.prefabReqs.has(path)) {
          this.prefabReqs.set(path, [])
        }
        const list = this.prefabReqs.get(path)
        list.push(callback)
      }
      let find = false
      for (let i = 0; i < this.resources.length; i++) {
        const info = this.resources[i]
        if (info.url === path) {
          find = true
          break
        }
      }
      // console.log("loadPrefab find", path, find)
      if (find === false) {
        Laya.loader.load(path, Laya.Handler.create(this, (obj: object) => { // 第二个参数：成功回调
          console.log("加载资源成功>>>>", path, obj)
        }))
      }
    }
  }
}
