local this = class("moduleMapCollectionInfo", G_UIModuleBase)
local _worldFilterMarkTpl = L_GameTpl:getWorldFilterMarkTpl()
local _homeBuildingCollectionTpl = L_GameTpl:getHomeBuildingCollectionTpl()
local _productsCollectionTpl = L_GameTpl:getProductsCollectionTpl()
local Status2ShowTxt = {
  [L_MapConst.CollectionStatus.Working] = L_GameUtil.fillColor(L_WordsTpl:getValue("notice_cellHomeCenterCollection_01"), L_Const.colorHtml.green002),
  [L_MapConst.CollectionStatus.NotSetPet] = "",
  [L_MapConst.CollectionStatus.Pause] = L_GameUtil.fillColor(L_WordsTpl:getValue("ui_moduleMapCollectionInfo_01"), L_Const.colorHtml.red003),
  [L_MapConst.CollectionStatus.UnFixed] = "",
  [L_MapConst.CollectionStatus.Free] = "",
  [L_MapConst.CollectionStatus.Full] = L_GameUtil.fillColor(L_WordsTpl:getValue("ui_moduleMapCollectionInfo_02"), L_Const.colorHtml.red003)
}

function this.bind()
  return {
    img_icon = nil,
    img_collect = false,
    img_bg = false,
    go_notFixed = false,
    go_status = false,
    isOn_fullStatus = false,
    isOn_working = false,
    txt_pointName = nil,
    txt_buildingName = nil,
    alpha_buildingName = 1,
    txt_status = nil,
    txt_desc = nil,
    go_hungry = false,
    go_full = false,
    list_reward = L_Const.ModuleInfo.CellIconBag,
    txt_trace = nil
  }
end

function this.methods()
  return {
    onClick_trace = function(self)
      local curTracedNode = L_MapStore:getCurNodeTargetTracing()
      if table.isEmpty(curTracedNode) then
        self.parent:excuteNodeFunction()
      elseif curTracedNode.nodeType == self.node.type and curTracedNode.id == self.node.id then
        L_MapStore:disposeNodeTarget()
      else
        self.parent:excuteNodeFunction()
      end
    end
  }
end

function this:open()
  L_MapStore:listenCallFunc(L_MapStore.event.refreshNodeTrace, self.judgeCurNodeTraced, self)
end

function this:close()
  L_MapStore:unListenCallFunc(L_MapStore.event.refreshNodeTrace, self.judgeCurNodeTraced)
end

function this:initModule(node)
  self.node = node
  self._collectionId = node:getCollectionId()
  self._worldFilterMarkId = node:getWorldFilterMarkId()
  if math.isEmpty(self._worldFilterMarkId) then
    errorf(string.format("collection:%s,worldFilterMarkId is empty", self._collectionId))
    return
  end
  local collection = L_CollectionStore:getCollection(self._collectionId)
  local tpl_worldFilterMark = _worldFilterMarkTpl:getTplById(self._worldFilterMarkId)
  self.bind.img_icon = _worldFilterMarkTpl:getIconBg(tpl_worldFilterMark)
  local status = L_CollectionManager:getStatus(self._collectionId)
  local isFixed = status ~= L_MapConst.CollectionStatus.UnFixed
  self.bind.go_status = status == L_MapConst.CollectionStatus.NotSetPet or status == L_MapConst.CollectionStatus.Working or status == L_MapConst.CollectionStatus.Pause or status == L_MapConst.CollectionStatus.Full
  self.bind.go_notFixed = not isFixed
  self.bind.isOn_working = status == L_MapConst.CollectionStatus.Working or status == L_MapConst.CollectionStatus.Full
  self.bind.go_fullStatus = status == L_MapConst.CollectionStatus.Full
  self.bind.txt_pointName = _worldFilterMarkTpl:getName(tpl_worldFilterMark)
  local tpl_collectionBuilding
  if isFixed then
    tpl_collectionBuilding = _homeBuildingCollectionTpl:getTplById(collection.build_id)
    self.bind.txt_buildingName = string.format("%sLv.%s", _homeBuildingCollectionTpl:getName(tpl_collectionBuilding), _homeBuildingCollectionTpl:getLevel(tpl_collectionBuilding))
    self.bind.alpha_buildingName = 1
  else
    tpl_collectionBuilding = _homeBuildingCollectionTpl:getTplByCollectionId(self._collectionId)[1]
    self.bind.txt_buildingName = L_GameUtil.fillColor(L_WordsTpl:getValue("ui_moduleMapCollectionInfo_03"), L_Const.colorHtml.white001)
    self.bind.alpha_buildingName = 0.5
  end
  self.bind.txt_status = Status2ShowTxt[status]
  self.bind.txt_desc = _worldFilterMarkTpl:getDes(tpl_worldFilterMark)
  self.bind.go_hungry = status == L_MapConst.CollectionStatus.Pause
  self.bind.go_full = status == L_MapConst.CollectionStatus.Full
  local reward = _homeBuildingCollectionTpl:getProducts(tpl_collectionBuilding)
  local tmp = {}
  for i, productId in pairs(reward) do
    local tpl_product = _productsCollectionTpl:getTplById(productId)
    local data = L_DataUtil.parseRewardConfig(_productsCollectionTpl:getDropShow(tpl_product))[1]
    local configData = L_ItemTplManager:getItemConfig(data.itemType, data.itemId)
    table.insert(tmp, {
      itemType = data.itemType,
      itemId = data.itemId,
      itemNumTxt = configData.name
    })
  end
  self.bind.list_reward:clear()
  self.bind.list_reward:insert_array(tmp)
  self:judgeCurNodeTraced()
end

function this:judgeCurNodeTraced()
  local curTracedNode = L_MapStore:getCurNodeTargetTracing()
  if table.isEmpty(curTracedNode) then
    self.bind.txt_trace = L_WordsTpl:getValue("ui_moduleMapCollectionInfo_04")
  elseif curTracedNode.nodeType == self.node.type and curTracedNode.id == self.node.id then
    self.bind.txt_trace = L_WordsTpl:getValue("ui_moduleMapCollectionInfo_05")
  else
    self.bind.txt_trace = L_WordsTpl:getValue("ui_moduleMapCollectionInfo_04")
  end
end

return this
