local this = class("cellHomeCenterCollection", G_UIModuleBase)
local homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local productsTpl = L_GameTpl:getProductsCollectionTpl()
local colorStatus = L_HomeConst.HomeCenterProductionBuildingStatusColor
local statusIcon = L_HomeConst.HomeCenterProductionBuildingStatusIcon
local statusText = L_HomeConst.HomeCenterProductionBuildingStatusText

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 = "",
    active_bubble = false,
    module_buildFrameTexture = L_Const.ModuleInfo.FrameTexture,
    module_petPixelIcon = {
      moduleName = "pages/pet/modulePetPixelIcon"
    }
  }
end

function this.methods()
  return {
    onClick_choose = function(self)
      self:onClickImgBuilding()
    end,
    onClick_productionBubble = function(self)
      self:onClickProductionBubble()
    end
  }
end

function this:onHomeBuildSync()
  self:refreshInnerData()
  self:refreshUI()
end

function this:open()
  self.onHomeBuildSyncHandler = handler(self, self.onHomeBuildSync)
  L_HomeStore:getCsHomeStore():RegisterEvent(C_EHomeStoreEvent.HomeBuildSync, self.onHomeBuildSyncHandler)
end

function this:close()
  L_HomeStore:getCsHomeStore():UnregisterEvent(C_EHomeStoreEvent.HomeBuildSync, self.onHomeBuildSyncHandler)
  L_TimerManager:clearTimer(self)
  if self.isBind then
    self.modules.module_petPixelIcon:close()
  end
end

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

function this:refreshData(homeHubWorkingBuildingInfo)
  self.uiData = {}
  self.uiData.isEmpty = table.isEmpty(homeHubWorkingBuildingInfo)
  if self.uiData.isEmpty then
    return
  end
  self.uiData.viewIndex = homeHubWorkingBuildingInfo.index + 1
  self.uiData.buildingGuid = homeHubWorkingBuildingInfo.guid
  self:refreshInnerData()
end

function this:refreshInnerData()
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self.uiData.buildingGuid)
  self.uiData.buildingId = L_HomeStore:getHomeBuildingConfigId(csHomeBuildData)
  self.uiData.buildingType = L_HomeStore:getHomeBuildingType(csHomeBuildData)
  self.uiData.stationedPetGuidList = L_HomeStore:getHomeBuildingStationedPetGuidList(csHomeBuildData)
  self:refreshProductionData()
  self:refreshBuildingStateDescData()
  self:refreshBuildingImgData()
end

function this:refreshBuildingStateDescData()
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self.uiData.buildingGuid)
  local petGuid = self.uiData.stationedPetGuidList[1]
  local csHomeBuildProductDataList = L_HomeStore:getHomeBuildProductDataList(csHomeBuildData)
  if petGuid then
    local csPetData = L_PetStore:getCsPetData(petGuid)
    local petWorkPause = L_PetStore:getPetWorkPause(csPetData)
    if petWorkPause then
      self.uiData.status = L_HomeConst.HomeCenterProductionBuildingStatus.Pause
      self.uiData.buildingStateDesc = L_Lang:get(L_WordsTpl:getValue(statusText[self.uiData.status]))
    else
      self.uiData.buildingStateDesc = self:getBuildingStateDescByProductInfo(csHomeBuildProductDataList)
    end
  else
    self.uiData.buildingStateDesc = self:getBuildingStateDescByProductInfo(csHomeBuildProductDataList)
  end
end

function this:refreshBuildingImgData()
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self.uiData.buildingGuid)
  local csHomeBuildProductDataList = csHomeBuildData.products
  if csHomeBuildProductDataList.Count > 0 and 0 < csHomeBuildProductDataList[csHomeBuildProductDataList.Count - 1].totalCount then
    self.uiData.bBuildingIsWorking = true
  else
    self.uiData.bBuildingIsWorking = false
  end
  self.uiData.imgBuilding = homeBuildingTpl:getBuildingPixelIcon(homeBuildingTpl:getTplById(self.uiData.buildingId))
  self.uiData.frameIdOfBuildingFrameTexture = homeBuildingTpl:getFrameTexture(homeBuildingTpl:getTplById(self.uiData.buildingId))
end

function this:refreshUI()
  if self.uiData.isEmpty or not self.isBind then
    return
  end
  local homeBuilding = homeBuildingTpl:getTplById(self.uiData.buildingId)
  self.bind.locationName = homeBuildingTpl:getName(homeBuilding)
  self.bind.collectionIcon = homeBuildingTpl:getBuildingPixelIcon(homeBuildingTpl:getTplById(self.uiData.buildingId))
  self.bind.statusShow = true
  self.bind.foundToggle = true
  self.bind.createIconShow = false
  self.bind.indexNum = string.format("NO.%d", self.uiData.viewIndex)
  do
    local homeData = homeBuildingTpl:getTplById(self.uiData.buildingId)
    local status = self.uiData.status
    self.bind.buildingColor = C_Color.white
    self.bind.buildingFrameColor = C_Color.white
    self.bind.txt_status = L_GameUtil.fillColor(self.uiData.buildingStateDesc, colorStatus[status])
    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
    local petGuid = self.uiData.stationedPetGuidList[1]
    self.bind.workPetActive = not math.isEmpty(petGuid)
    if self.bind.workPetActive then
      self.bind.petLock = L_PetStore:getPetIsDesignated(L_PetStore:getCsPetData(petGuid))
    end
    self.modules.module_petPixelIcon:setGuid(petGuid or 0, true)
    local buildFrameId
    if status == L_MapConst.CollectionStatus.Working then
      buildFrameId = homeBuildingTpl: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
      goto lbl_172
      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)
    end
  end
  ::lbl_172::
  self:refreshProductionUI()
  L_TimerManager:newOrResetTimer(self, "refreshProduction", function()
    self:refreshProduction()
  end, 5, -1)
end

function this:refreshProduction()
  self:refreshProductionData()
  self:refreshProductionUI()
end

function this:refreshProductionData()
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self.uiData.buildingGuid)
  self.uiData.canReceiveReward = csHomeBuildData:CanReward()
  self.uiData.canReceiveAllProductionQueueReward = not self:checkHasUnCompleteProduction()
  if self.uiData.canReceiveReward == true then
    local productGuid = csHomeBuildData.products[0].guid
    self.uiData.defaultProductionIconPath = L_HomeUtil.getHomeBuildingProductionInfo(self.uiData.buildingGuid, productGuid).iconPath
  else
    self.uiData.defaultProductionIconPath = ""
  end
end

function this:refreshProductionUI()
  if self.uiData.isEmpty or not self.isBind then
    return
  end
  if self.bind.active_productionPart == self.uiData.canReceiveReward then
    return
  end
  self.bind.itemIcon = self.uiData.defaultProductionIconPath
  self.bind.toggleWorkingStatus = self.uiData.canReceiveReward
  self.bind.toggleFullStatus = self.uiData.canReceiveAllProductionQueueReward
end

function this:onClickImgBuilding()
  self:emit("onClickImgBuilding", {
    buildingGuid = self.uiData.buildingGuid,
    buildingType = self.uiData.buildingType
  })
end

function this:onClickProductionBubble()
  L_ProductManager:reqCollectProduct(self.uiData.buildingGuid, function()
  end)
end

function this:inactiveDragBtn()
  self.bindComponents.group_drag.blocksRaycasts = false
end

function this:activeDragBtn()
  self.bindComponents.group_drag.blocksRaycasts = true
end

function this:checkIfOnDragCell(go)
  if not self.isBind then
    return false
  end
  if go == self.bindComponents.cell.gameObject then
    return true
  else
    return false
  end
end

function this:getRect()
  if self.isBind then
    return self.bindComponents.cell
  end
end

function this:getIsEmpty()
  return self.uiData.isEmpty
end

function this:getBuildingStateDescByProductInfo(csHomeBuildProductDataList)
  if table.isEmpty(csHomeBuildProductDataList) then
    self.uiData.status = L_HomeConst.HomeCenterProductionBuildingStatus.Free
    return L_Lang:get(L_WordsTpl:getValue("ui_cellFastProductQueue_02"))
  else
    local hasUnCompleteProduction = self:checkHasUnCompleteProduction()
    if hasUnCompleteProduction then
      self.uiData.status = L_HomeConst.HomeCenterProductionBuildingStatus.Working
      return L_Lang:get(L_WordsTpl:getValue("notice_cellHomeCenterCollection_01"))
    else
      self.uiData.status = L_HomeConst.HomeCenterProductionBuildingStatus.CanReceiveAllQueueReward
      return L_Lang:get(L_WordsTpl:getValue("ui_homemanage_tag_tobeclaimed"))
    end
  end
end

function this:checkHasUnCompleteProduction()
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self.uiData.buildingGuid)
  local csHomeBuildProductDataList = csHomeBuildData.products
  for _, csHomeBuildProductData in pairs(csHomeBuildProductDataList) do
    if csHomeBuildProductData.totalCount ~= 0 then
      return true
    end
  end
  return false
end

return this
