import { Resource } from "@/mini3d"
import { FileLoader } from "three"
import { CHINA_GEOJSON_PATH, getPublicMapResourcePath, HONGYUAN_COUNTY_OUTLINE_PATH, HONGYUAN_TOWNSHIP_SCREEN_GEOJSON_PATH } from "@/config/mapResources"

import pathLine from "@/assets/texture/pathLine4.png"
import pathLine3 from "@/assets/texture/pathLine2.png"
import pathLine2 from "@/assets/texture/pathLine.png"

import side from "@/assets/texture/side.png"
import ocean from "@/assets/texture/ocean-bg.png"
import rotationBorder1 from "@/assets/texture/rotationBorder1.png"
import rotationBorder2 from "@/assets/texture/rotationBorder2.png"
import chinaBlurLine from "@/assets/texture/chinaBlurLine.png"
import guangquan1 from "@/assets/texture/guangquan01.png"
import guangquan2 from "@/assets/texture/guangquan02.png"
import huiguang from "@/assets/texture/huiguang.png"
import arrow from "@/assets/texture/arrow.png"
import point from "@/assets/texture/point1.png"
// 焦点贴图
import focusArrowsTexture from "@/assets/texture/focus/focus_arrows.png"
import focusBarTexture from "@/assets/texture/focus/focus_bar.png"
import focusBgTexture from "@/assets/texture/focus/focus_bg.png"
import focusMidQuanTexture from "@/assets/texture/focus/focus_mid_quan.png"
import focusMoveBgTexture from "@/assets/texture/focus/focus_move_bg.png"

const sharedAssetLoaders = new Map()

export class Assets {
  constructor(options = {}) {
    this.options = {
      includeChinaMap: false,
      ...options,
    }
    this.ready = null
    this.init()
  }
  init() {
    const key = this.getSharedKey()
    const shared = getSharedAssetLoader(key, () => this.createAssetList())
    this.instance = shared.instance
    this.ready = shared.ready
  }

  getSharedKey() {
    return this.options.includeChinaMap ? "hongyuan-map-with-china" : "hongyuan-map"
  }

  createAssetList() {
    let assets = [
      { type: "Texture", name: "flyline", path: pathLine },
      { type: "Texture", name: "pathLine", path: pathLine },
      { type: "Texture", name: "pathLine2", path: pathLine2 },
      { type: "Texture", name: "pathLine3", path: pathLine3 },

      {
        type: "File",
        name: "china",
        path: getPublicMapResourcePath(HONGYUAN_COUNTY_OUTLINE_PATH),
      },

      {
        type: "File",
        name: "mapJson",
        path: getPublicMapResourcePath(HONGYUAN_TOWNSHIP_SCREEN_GEOJSON_PATH),
      },
      {
        type: "File",
        name: "mapStroke",
        path: getPublicMapResourcePath(HONGYUAN_COUNTY_OUTLINE_PATH),
      },
      { type: "Texture", name: "huiguang", path: huiguang },
      { type: "Texture", name: "rotationBorder1", path: rotationBorder1 },
      { type: "Texture", name: "rotationBorder2", path: rotationBorder2 },
      { type: "Texture", name: "guangquan1", path: guangquan1 },
      { type: "Texture", name: "guangquan2", path: guangquan2 },
      { type: "Texture", name: "chinaBlurLine", path: chinaBlurLine },
      { type: "Texture", name: "ocean", path: ocean },
      { type: "Texture", name: "side", path: side },
      { type: "Texture", name: "arrow", path: arrow },
      { type: "Texture", name: "point", path: point },

      // focus
      { type: "Texture", name: "focusArrows", path: focusArrowsTexture },
      { type: "Texture", name: "focusBar", path: focusBarTexture },
      { type: "Texture", name: "focusBg", path: focusBgTexture },
      { type: "Texture", name: "focusMidQuan", path: focusMidQuanTexture },
      { type: "Texture", name: "focusMoveBg", path: focusMoveBgTexture },
    ]
    if (this.options.includeChinaMap) {
      assets.push({
        type: "File",
        name: "chinaMap",
        path: getPublicMapResourcePath(CHINA_GEOJSON_PATH),
      })
    }
    return assets
  }
}

function getSharedAssetLoader(key, createAssetList) {
  if (sharedAssetLoaders.has(key)) return sharedAssetLoaders.get(key)
  const instance = new Resource({ cloneTextures: true })
    // 添加Fileloader
  instance.addLoader(FileLoader, "FileLoader")

  // 只共享前端静态底图资源，避免每个地图模块重复下载和解码。
  const ready = instance.loadAll(createAssetList())
  const shared = { instance, ready }
  sharedAssetLoaders.set(key, shared)
  return shared
}
