local this = class("cellFreeWorkProduction", G_UIModuleBase)
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()

function this.bind()
  return {
    img_building = nil,
    frameImg_building = L_Const.ModuleInfo.FrameTexture,
    text_buildingName = "",
    list_cellFreeWorkProductionPet = {
      moduleName = "pages/home/homeCenter/moduleHomeCenterProduction/cellFreeWorkProductionPet"
    },
    active_btn_addPet = false,
    active_productionPart = false,
    img_production = nil,
    text_order = "",
    text_desc = ""
  }
end

function this.methods()
  return {
    onClick_imgBuilding = function(self)
      self:onClickImgBuilding()
    end,
    onClick_btnReceiveProduction = function(self)
      self:onClickBtnReceiveProduction()
    end
  }
end

function this:open()
  self.onHomeBuildSyncHandler = handler(self, self.onHomeBuildSync)
  L_HomeStore:getCsHomeStore():RegisterEvent(C_EHomeStoreEvent.HomeBuildSync, self.onHomeBuildSyncHandler)
  L_TimerManager:newOrResetTimer(self, "refreshProduction", function()
    self:refreshProduction()
  end, 1, -1)
end

function this:close()
  L_HomeStore:getCsHomeStore():UnregisterEvent(C_EHomeStoreEvent.HomeBuildSync, self.onHomeBuildSyncHandler)
  L_TimerManager:clearTimer(self)
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.UIData.canReceiveReward = csHomeBuildData:CanReward() and true or false
  local defaultProduct = L_HomeStore:getHomeBuildProductDataList(csHomeBuildData)[1]
  if self.UIData.canReceiveReward and defaultProduct then
    local productGuid = defaultProduct.guid
    self.UIData.defaultProductionIconPath = L_HomeUtil.getHomeBuildingProductionInfo(self.UIData.buildingGuid, productGuid).iconPath
  else
    self.UIData.defaultProductionIconPath = ""
  end
  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.buildingStateDesc = L_Lang:get(L_WordsTpl:getValue("notice_cellHomeCenterCollection_03"))
    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()
  self.bind.text_order = tostring(self.UIData.viewIndex)
  local homeBuildingTpl = _homeBuildingTpl:getTplById(self.UIData.buildingId)
  self.bind.text_buildingName = _homeBuildingTpl:getName(homeBuildingTpl)
  self.bind.active_btn_addPet = table.isEmpty(self.UIData.stationedPetGuidList)
  local insertArray = {}
  for _, _ in pairs(self.UIData.stationedPetGuidList) do
    table.insert(insertArray, {})
  end
  self.bind.list_cellFreeWorkProductionPet:clear()
  self.bind.list_cellFreeWorkProductionPet:insert_array(insertArray)
  for i = 1, #self.UIData.stationedPetGuidList do
    local cellFreeWorkProductionPet = self.bind.list_cellFreeWorkProductionPet:getItemCls(i)
    local stationedPetGuid = self.UIData.stationedPetGuidList[i]
    cellFreeWorkProductionPet:refreshData(stationedPetGuid)
    cellFreeWorkProductionPet:refreshUI()
  end
  if self.bind.active_productionPart ~= self.UIData.canReceiveReward then
    self.bind.active_productionPart = self.UIData.canReceiveReward
  end
  self.bind.img_production = self.UIData.defaultProductionIconPath
  self.bind.text_desc = self.UIData.buildingStateDesc
  self.bind.img_building = self.UIData.bBuildingIsWorking and "" or self.UIData.imgBuilding
  self.modules.frameImg_building:setFrameId(self.UIData.bBuildingIsWorking and self.UIData.frameIdOfBuildingFrameTexture or 0)
end

function this:refreshProduction()
  if self.UIData.isEmpty then
    return
  end
  local csHomeBuildData = L_HomeStore:getCsHomeBuildDataByGuid(self.UIData.buildingGuid)
  self.UIData.canReceiveReward = csHomeBuildData:CanReward() and true or false
  if self.bind.active_productionPart == self.UIData.canReceiveReward then
    return
  end
  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
  self.bind.active_productionPart = self.UIData.canReceiveReward
  self.bind.img_production = self.UIData.defaultProductionIconPath
end

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

function this:onClickBtnReceiveProduction()
  if self.UIData.canReceiveReward then
    if self.UIData.buildingType == L_Const.WorldMapBuildType.WMBT_HATCH then
      local queueData = L_ProductManager:getProduceQueueForBuilding(self.UIData.buildingGuid)
      local special = L_ProductManager:getProductSpecialParams(self.UIData.buildingGuid, queueData[1].product.product_guid)
      L_PetStore:onClickHatchOutPetEgg(self.UIData.buildingGuid, special.extra_guid, function()
      end)
    else
      L_ProductManager:reqCollectProduct(self.UIData.buildingGuid, function()
      end)
    end
  end
end

function this:inactiveDragBtn()
  self.bindComponents.btn_drag.gameObject:SetActive(false)
end

function this:activeDragBtn()
  self.bindComponents.btn_drag.gameObject:SetActive(true)
end

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

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

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

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

function this:getBuildingStateDescByProductInfo(csHomeBuildProductDataList)
  if table.isEmpty(csHomeBuildProductDataList) then
    return L_Lang:get(L_WordsTpl:getValue("ui_cellFastProductQueue_02"))
  else
    local hasUnCompleteProduction = false
    for _, csHomeBuildProductData in pairs(csHomeBuildProductDataList) do
      if csHomeBuildProductData.totalCount ~= 0 then
        hasUnCompleteProduction = true
      end
    end
    if hasUnCompleteProduction then
      return L_Lang:get(L_WordsTpl:getValue("notice_cellHomeCenterCollection_01"))
    else
      return L_Lang:get(L_WordsTpl:getValue("ui_cellFastProductQueue_02"))
    end
  end
end

return this
