local this = class("moduleHomeCenterBuildingManage", require("ui.pages.home.homeCenter.moduleHomeCenterToggleModule"))
local moduleHomeCenterBuildingManageConfig = require("ui.pages.home.homeCenter.moduleHomeCenterBuildingManage.moduleHomeCenterBuildingManageConfig")
local ToggleKeyList = moduleHomeCenterBuildingManageConfig.toggleKeyList
local RewardTab = {
  CAN_REWARD = 1,
  EMPTY = 2,
  LOCK = 3
}

function this.bind()
  return {
    toggleModule = {
      type = "toggleModule",
      moduleHomeCenterProduction = {
        assetName = "ui/pages/home/homeCenter/moduleHomeCenterProduction/moduleHomeCenterProduction",
        moduleName = "Pages/Home/HomeCenter/ModuleHomeCenterProduction/moduleHomeCenterProduction"
      },
      moduleHomeCenterCrop = {
        assetName = "ui/pages/home/homeCenter/moduleHomeCenterCrop/moduleHomeCenterCrop",
        moduleName = "Pages/Home/HomeCenter/ModuleHomeCenterCrop/moduleHomeCenterCrop"
      },
      moduleHomeCenterCollection = {
        assetName = "ui/pages/home/homeCenter/moduleHomeCenterCollection/moduleHomeCenterCollection",
        moduleName = "Pages/Home/HomeCenter/ModuleHomeCenterCollection/moduleHomeCenterCollection"
      }
    },
    toggleModuleName = "",
    list_toggle = {
      moduleName = "pages/home/homeCenter/moduleHomeCenterBuildingManage/cellModuleHomeCenterBuildingManageToggle"
    },
    tab_chest = RewardTab.CAN_REWARD
  }
end

function this.methods()
  return {
    list_toggle = {
      onClick_toggle = function(self, params)
        self:onClickToggle(params)
      end
    },
    onClick_btn_receiveAll = function(self)
      self:onClickBtnReceiveAll()
    end
  }
end

function this:open()
  L_ReddotManager:registerReddot(self.bindComponents.trans_reddot_receiveAll, L_ReddotManager.DotDef.HomeCenter_BuildingManage_ReceiveAll)
  
  function self._refreshCenReceive()
    self:refreshCenReceive()
  end
  
  L_HomeStore:getCsHomeStore():RegisterEvent(C_EHomeStoreEvent.HomeBuildSync, self._refreshCenReceive)
end

function this:close()
  L_HomeStore:getCsHomeStore():UnregisterEvent(C_EHomeStoreEvent.HomeBuildSync, self._refreshCenReceive)
end

function this:refreshData()
  self.UIData = {}
end

function this:refreshUI()
  local insertArray = {}
  for _ = 1, #ToggleKeyList do
    table.insert(insertArray, {})
  end
  self.bind.list_toggle:freshAll(insertArray)
  for i = 1, #ToggleKeyList do
    local toggleKey = ToggleKeyList[i]
    local cellModuleHomeCenterBuildingManageToggle = self.bind.list_toggle:getItemCls(i)
    cellModuleHomeCenterBuildingManageToggle:refreshData(toggleKey)
    cellModuleHomeCenterBuildingManageToggle:refreshUI()
  end
  if self.bind.toggleModuleName == "" then
    for i = 1, #ToggleKeyList do
      local cellModuleHomeCenterBuildingManageToggle = self.bind.list_toggle:getItemCls(i)
      if cellModuleHomeCenterBuildingManageToggle:getIfSystemUnlocked() then
        self:changeToggleModule(cellModuleHomeCenterBuildingManageToggle:getToggleModuleName())
        break
      end
    end
  end
  local csHomeStore = L_HomeStore:getCsHomeStore()
  self.bind.tab_chest = RewardTab.LOCK
  local guidList = csHomeStore:GetHomeHubWorkingBuildingInfoListByTabType(L_Const.HomeHubTabType.HHTT_AUTO_WORK)
  if guidList and guidList.Count > 0 then
    self.bind.tab_chest = RewardTab.EMPTY
  end
  guidList = csHomeStore:GetHomeHubWorkingBuildingInfoListByTabType(L_Const.HomeHubTabType.HHTT_COLLECT)
  if guidList and guidList.Count > 0 then
    self.bind.tab_chest = RewardTab.EMPTY
  end
  guidList = csHomeStore:GetHomeHubWorkingBuildingInfoListByTabType(L_Const.HomeHubTabType.HHTT_PRODUCT)
  if guidList and guidList.Count > 0 then
    self.bind.tab_chest = RewardTab.EMPTY
  end
  self:refreshCenReceive()
end

function this:showAnim(animName)
  self.bindComponents.anim:Play(animName)
end

function this:onClickToggle(params)
  local toggleModuleName = params.toggleModuleName
  if toggleModuleName == self.bind.toggleModuleName then
    return
  end
  self:changeToggleModule(toggleModuleName)
end

function this:changeToggleModule(toggleModuleName)
  local oldmoduleName = self.bind.toggleModuleName
  self.bind.toggleModuleName = toggleModuleName
  self.curToggleModule = self.modules.toggleModule[toggleModuleName]
  self.curToggleModule:refreshData()
  self.curToggleModule:refreshUI()
  for i, v in ipairs(self.bind.list_toggle:getItemCls()) do
    if v.UIData.toggleModuleName == oldmoduleName then
      v:playInactive()
    elseif v.UIData.toggleModuleName == toggleModuleName then
      v:playActive()
    end
  end
end

function this:refreshCenReceive()
  if L_HomeStore:getCsHomeStore():QueryCanReceiveAnyRewardInHomeCenter() then
    self.bind.tab_chest = RewardTab.CAN_REWARD
  else
    self.bind.tab_chest = RewardTab.EMPTY
  end
end

function this:onClickBtnReceiveAll()
  L_HomeStore:getCsHomeStore():ReqReceiveAllBuildingDataInHomeCenter()
  self.bind.tab_chest = RewardTab.EMPTY
end

return this
