local this = class("cellHomeCollection", G_UIModuleBase)
local petTpl = L_GameTpl:getPetTpl()
local homeCollectionWorldTpl = L_GameTpl:getHomeCollectionWorldTpl()
local homeBuildingCollectionTpl = L_GameTpl:getHomeBuildingCollectionTpl()
local productsTpl = L_GameTpl:getProductsCollectionTpl()
local txtStatus = L_MapConst.CollectionStatusText
local colorStatus = L_MapConst.CollectionStatusColor
local statusIcon = L_MapConst.CollectionStatusIcon

function this.bind()
  return {
    isChoose = false,
    collectionIcon = "",
    toggleWorkingStatus = false,
    toggleFullStatus = false,
    workPetIcon = "",
    workPetActive = true,
    foundToggle = false,
    createIconShow = false,
    buildingColor = C_Color.white,
    locationName = "",
    indexNum = "",
    statusShow = true,
    txt_status = "",
    statusIconShow = true,
    statusIcon = "",
    decorate_color = C_Color.white,
    itemIcon = ""
  }
end

function this.methods()
  return {
    onClick_choose = function(self)
      self:emit("onClick_choose", self.bind, self.bindComponents.collectionTrans.position)
    end
  }
end

function this:open()
  L_CollectionStore:listenCallFunc(L_CollectionStore.event.updateCollection, self.onEvent_updateCollection, self)
end

function this:close()
  L_CollectionStore:unListenCallFunc(L_CollectionStore.event.updateCollection, self.onEvent_updateCollection, self)
end

function this:refresh()
  self:initCell()
end

function this:onEvent_updateCollection(updateList)
  if updateList[self.bind.collectionGuid] then
    self:initCell()
  end
end

function this:initCell()
  self.bind.locationName = homeCollectionWorldTpl:getLocalName(self.bind.data)
  self.bind.objId = homeCollectionWorldTpl:getWorldMapObjectUnfixed(self.bind.data)
  self.bind.collectionIcon = homeCollectionWorldTpl:getPixelIcon(self.bind.data)
  self.bind.serverData = L_CollectionStore:getCollection(self.bind.collectionGuid) or {}
  self.bind.statusShow = not table.isEmpty(self.bind.serverData) and true or false
  self.bind.foundToggle = not table.isEmpty(self.bind.serverData) and true or false
  self.bind.createIconShow = table.isEmpty(self.bind.serverData) and true or false
  self.bind.indexNum = string.format("NO.%d", self.bind.index)
  if not table.isEmpty(self.bind.serverData) then
    local homeData = homeBuildingCollectionTpl:getTplById(self.bind.serverData.build_id)
    local level = homeBuildingCollectionTpl:getLevel(homeData)
    local status = L_CollectionManager:getStatus(self.bind.collectionGuid)
    self.bind.buildingColor = C_Color.white
    self.bind.txt_status = L_WordsTpl:getValue(txtStatus[status]) or ""
    self.bind.statusIconShow = not string.isEmpty(statusIcon[status])
    self.bind.statusIcon = statusIcon[status]
    local _, color = C_ColorUtility.TryParseHtmlString(colorStatus[status])
    self.bind.decorate_color = color
    self.bind.workPetActive = status ~= L_MapConst.CollectionStatus.NotSetPet
    if not math.isEmpty(self.bind.serverData.pet_guid) then
      local pet = L_PetStore:getPetItem(self.bind.serverData.pet_guid)
      local tpl = petTpl:getTplById(pet.id)
      self.bind.workPetIcon = petTpl:getPetPixelIcon(tpl, pet:isSpecialPet())
    end
    local rewardList = L_CollectionManager:getCollectCommonItemRewardList(self.bind.collectionGuid)
    self.bind.toggleWorkingStatus = 0 < #rewardList
    if 0 < #rewardList then
      local tpl_pro = productsTpl:getTplById(rewardList[1].product_id)
      local reward = L_DataUtil.parseRewardConfigItem(productsTpl:getDropShow(tpl_pro)[1])
      local configData = L_ItemTplManager:getItemConfig(reward.itemType, reward.itemId)
      self.bind.itemIcon = configData.icon
      self.bind.toggleFullStatus = status == L_MapConst.CollectionStatus.Full
    end
  else
    self.bind.workPetActive = false
    self.bind.buildingColor = C_Color(0.5, 0.5, 0.5)
  end
end

return this
