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 reqs: Map<string, Array<Laya.Image>> = new Map()


  public init(): void {

    this.resources.push({ url: respath.score_add_ui_res() })
    this.resources.push({ url: respath.dc_ui_res() })
    this.resources.push({ url: respath.trophy_ui_res() })

    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 })

    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.reqs.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.reqs.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.reqs.has(path)) {
        this.reqs.set(path, new Array())
      }
      let list = this.reqs.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.reqs.get(path)
          for (let i = 0; i < list.length; i++) {
            const img = list[i]
            if (img && img.parent) {
              img.texture = obj
            }
          }
          this.reqs.delete(path)
        }), Laya.Handler.create(this, (resProg: number) => { // 第三个参数：进度回调
          // console.log("加载资源进度>>>>", path, resProg)
        }))
      }

    }
  }
}
