local this = class("mapCollectionNode", require("ui.manager.map.node.mapNode"))
local PATH = "UI/Pages/Map/BigMap/cellMapCollectionNode.prefab"
local _typeImage = typeof(C_LImage)
local _typeRect = typeof(C_RectTransform)
local _typeToggle = typeof(C_LToggle)
local _typeBtn = typeof(C_LButton)
local _worldMapTpl = L_GameTpl:getWorldMapTpl()
local _worldFilterMarkTpl = L_GameTpl:getWorldFilterMarkTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.iconBg = "UI/Atlas/AreaMapPage/tex_areamap_icon_fieldbg.png"
  self._collectionId = nil
  self.configData = nil
end

function this:generate(parent, pool, bMiniMap)
  self.bMiniMap = false
  if bMiniMap then
    self.bMiniMap = bMiniMap
  end
  self:_generate(parent, pool or L_ResPool)
  if parent then
    self.trans:Reset(parent)
  else
    L_Vector3.setScale(self.trans, L_Vector3.getTemp(1, 1, 1))
    L_Vector3.setLocalPos(self.trans, L_Vector3.getTemp())
  end
  self.go.name = self.id
  self:refreshView()
  L_CollectionStore:listenCallFunc(L_CollectionStore.event.refreshCollection, self.onEvent_refreshCollection, self)
end

function this:onDestroy()
  L_CollectionStore:unListenCallFunc(L_CollectionStore.event.refreshCollection, self.onEvent_refreshCollection, self)
  this.super.onDestroy(self)
end

function this:_generate(parent, pool)
  local go = pool:syncGameObject(PATH)
  self.go = go
  self.trans = self.go:GetComponent(_typeRect)
  self.imgIcon = L_GameUtil.findTranByName(self.go, "img_icon").gameObject:GetComponent(_typeImage)
  self.imgIconCollect = L_GameUtil.findTranByName(self.go, "img_collect").gameObject:GetComponent(_typeImage)
  self.goToggleWorking = L_GameUtil.findTranByName(self.go, "toggle_isWorking").gameObject
  self.toggleWorking = L_GameUtil.findTranByName(self.go, "toggle_isWorking").gameObject:GetComponent(_typeToggle)
  self.toggleFullStatus = L_GameUtil.findTranByName(self.go, "fullStatus").gameObject:GetComponent(_typeToggle)
  self.goNotFix = L_GameUtil.findTranByName(self.go, "img_hammer").gameObject
  self.btnNode = L_GameUtil.findTranByName(self.go, "Button").gameObject:GetComponent(_typeBtn)
  self.animation = self.go:GetComponent(typeof(C_Animation))
  self.choose = self.trans:Find("img_collect/IsChoose").gameObject
  self.traceEffect = self.go:GetComponent(typeof(C_UIEffectControl))
  self.location = self.trans:Find("img_collect/content/tophalf/img_icon/location").gameObject
  self.locationTrans = self.location:GetComponent(typeof(C_RectTransform))
  self.goTopHalf = L_GameUtil.findTranByName(self.go, "tophalf").gameObject
end

function this:refreshPosByMapData(mapData)
  this.super.refreshPosByMapData(self, mapData)
end

function this:refreshLockState()
end

function this:getCollectionId()
  return self._collectionId
end

function this:getWorldFilterMarkId()
  return _worldMapTpl:getFilterMark(self.configData)
end

function this:setCollectionId(id)
  self._collectionId = id
  local worldFilterMarkId = self:getWorldFilterMarkId()
  if math.isEmpty(worldFilterMarkId) then
    local worldMapId = _worldMapTpl:getId(self.configData)
    printf(string.format("collection:%s,worldMapId:%s,worldFilterMarkId is empty", self._collectionId, worldMapId))
    return
  end
  local tpl_worldFilterMark = _worldFilterMarkTpl:getTplById(worldFilterMarkId)
  self.iconPath = _worldFilterMarkTpl:getIcon(tpl_worldFilterMark)
  self.iconbgPath = _worldFilterMarkTpl:getIconBg(tpl_worldFilterMark)
end

function this:refreshView()
  if self.bMiniMap == true then
    self.goTopHalf:SetActive(false)
  else
    self.goTopHalf:SetActive(true)
    self.imgIcon:LoadSprite(self.iconPath)
    local status = L_CollectionManager:getStatus(self._collectionId)
    self.goToggleWorking:ActiveTrans(status == L_MapConst.CollectionStatus.Full or status == L_MapConst.CollectionStatus.NotSetPet or status == L_MapConst.CollectionStatus.Pause or status == L_MapConst.CollectionStatus.Working)
    self.goNotFix:ActiveTrans(status == L_MapConst.CollectionStatus.UnFixed)
    self.toggleWorking.isOn = status == L_MapConst.CollectionStatus.Working
    self.toggleFullStatus.isOn = status == L_MapConst.CollectionStatus.Full
  end
  self.imgIconCollect:LoadSprite(self.iconbgPath)
end

function this:onEvent_refreshCollection()
  self:refreshView()
end

function this:setSize(size)
  local nodeIcon = self.go.transform:Find("img_collect").gameObject
  nodeIcon.transform.localScale = L_Vector3.new(0.7, 0.7)
end

return this
