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

function this.bind()
  return {
    opacity = 1,
    isChoose = false,
    collectionIcon = "",
    toggleWorkingStatus = false,
    toggleFullStatus = false,
    workPetActive = true,
    petLock = false,
    foundToggle = false,
    createIconShow = false,
    buildingColor = C_Color.white,
    buildingFrameColor = C_Color.white,
    locationName = "",
    indexNum = "",
    statusShow = true,
    txt_status = "",
    statusIconShow = true,
    statusIcon = "",
    decorate_color = C_Color.white,
    itemIcon = "",
    module_buildFrameTexture = L_Const.ModuleInfo.FrameTexture,
    module_petPixelIcon = {
      moduleName = "pages/pet/modulePetPixelIcon"
    }
  }
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)
  L_TimerManager:clearTimer(self)
  if self.isBind then
    self.modules.module_petPixelIcon:close()
  end
end

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

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

function this:setCollectionData(data, index)
  for k, v in pairs(data) do
    self.bind[k] = v
  end
  self.bind.index = index
  self:initCell()
end

function this:initCell()
  if not self.bind.data or not self.isBind then
    return
  end
  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.modules.module_buildFrameTexture:setFrameId(0)
  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.buildingFrameColor = 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 self.bind.workPetActive then
      self.bind.petLock = L_PetStore:getPetIsDesignated(L_PetStore:getCsPetData(self.bind.serverData.pet_guid))
    end
    self.modules.module_petPixelIcon:setGuid(self.bind.serverData.pet_guid or 0, true)
    local buildFrameId
    if status == L_MapConst.CollectionStatus.Working then
      buildFrameId = homeBuildingCollectionTpl:getFrameTexture(homeData)
    else
    end
    if not math.isEmpty(buildFrameId) then
      self.modules.module_buildFrameTexture:setFrameId(buildFrameId)
      self.bind.buildingColor = C_Color(1, 1, 1, 0)
    else
      self.modules.module_buildFrameTexture:setFrameId(0)
      self.bind.buildingColor = C_Color.white
    end
    self:refreshReward()
    L_TimerManager:newOrResetTimer(self, "refreshProduction", function()
      self:refreshReward()
    end, 5, -1)
  else
    self.bind.workPetActive = false
    self.bind.buildingColor = C_Color(0.5, 0.5, 0.5)
    self.bind.buildingFrameColor = C_Color(0.5, 0.5, 0.5)
    L_TimerManager:clearTimer(self)
  end
end

function this:refreshReward()
  if not self.bind.data or not self.isBind then
    return
  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
    local status = L_CollectionManager:getStatus(self.bind.collectionGuid)
    self.bind.toggleFullStatus = status == L_MapConst.CollectionStatus.Full
  end
end

function this:getRaycastGo()
  if self.isBind then
    return self.bindComponents.collectionTrans.gameObject
  end
end

return this
