local this = class("moduleMainCityHome", G_UIModuleBase)
local _BlockTpl = L_GameTpl:getHomeBlockTpl()
local HomeMainMode = L_HomeConst.homeMainMode

function this.bind()
  return {
    contentActive = false,
    module_homeCrop = {
      moduleName = "pages/home/homeCrop/moduleHomeCrop"
    },
    active_Crop = false,
    module_belt = {
      moduleName = "pages/home/homeBag/moduleMainCityHomeBelt"
    },
    module_homeCropHud = {
      moduleName = "pages/Home/homeCrop/moduleHomeCropHud"
    },
    module_homeScience = {
      moduleName = "pages/home/homeScience/moduleHomeScience"
    },
    txt_homeName = "",
    size_nameBg = C_Vector2(0, 62),
    active_buildLock = false,
    text_buildLock = ""
  }
end

function this.methods()
  return {
    onClick_info = function(self)
      L_UI:open("pageHomeLevelInfo")
    end,
    onClick_homeScience = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.homeScience)
    end
  }
end

function this:open()
  self.parentPageName = "pageMainCity"
  self.csHomeManager = AzurWorld.HomeMgr
  self.updateHandle = handler(self, self.update)
  C_UpdateSource.AddUpdateEventHandler(self.updateHandle, "moduleMainCityHome.update")
  L_SceneManager:addListener(L_SceneManager.event.switchSceneEnd, self.refreshState, self)
  L_HomeResidentStore:listenCallFunc(L_HomeResidentStore.event.residentDataUpdate, self.refreshState, self)
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshPlayerStatus, self.updateButtonsActive, self)
  L_HomeStore:listenCallFunc(L_HomeStore.event.syncHomeContentUnlock, self.onEvent_syncHomeContentUnlock, self)
  L_HomeStore:listenCallFunc(L_HomeStore.event.refreshHomeInfo, self.refreshHomeName, self)
  L_ReddotManager:registerReddot(self.bindComponents.trans_homeTechLevelShowReddot, L_ReddotManager.DotDef.MainCityHomeTechLevelShow)
  self:refreshHomeName()
  self:refreshState()
  self:bindPcKey()
  self:initSystem()
end

function this:bindPcKey()
  L_ShortCutManager:addShortKeyFlag(self, "K", L_Const.AnchorType.Bottom, self.bindComponents.transHomeScience, L_Vector3.new(0, 2, 0), 0.8)
end

function this:refreshHomeName()
  self.bind.txt_homeName = L_HomeStore:getHomeName()
  self.bind.size_nameBg = C_Vector2(self.bindComponents.nameText.preferredWidth + 150, 62)
end

function this:close()
  C_UpdateSource.RemoveUpdateEventHandler(self.updateHandle)
  L_SceneManager:removeListener(L_SceneManager.event.switchSceneEnd, self.refreshState, self)
  L_HomeResidentStore:unListenCallFunc(L_HomeResidentStore.event.residentDataUpdate, self.refreshState, self)
  L_PlayerStore:unListenCallFunc(L_PlayerStore.event.refreshPlayerStatus, self.updateButtonsActive, self)
  L_HomeStore:unListenCallFunc(L_HomeStore.event.syncHomeContentUnlock, self.onEvent_syncHomeContentUnlock)
  L_HomeStore:unListenCallFunc(L_HomeStore.event.refreshHomeInfo, self.refreshHomeName, self)
  self:destroyAllLocker()
end

function this:refreshState()
  local curModule = L_WorldManager:getCurModule()
  local isHome = curModule == L_Const.worldModule.home
  self.bind.contentActive = isHome
  if not isHome then
    return
  end
end

function this:registerShortCut(pageName)
  self.modules.module_belt:registerShortCut(pageName)
end

function this:show()
  self:updateButtonsActive()
  self.modules.module_homeCrop:show()
  self.modules.module_belt:refreshView()
  if not self.parentPageName then
    return
  end
  self:registerShortCut(self.parentPageName)
end

function this:isCropAvaliable()
  return self._currMode == HomeMainMode.CROP
end

function this:update()
  local curModule = L_WorldManager:getCurModule()
  if curModule ~= L_Const.worldModule.home then
    return
  end
  self:updateButtonsActive()
end

function this:updateButtonsActive()
  local playerEntity = L_PlayerManager:getMainControlEntity()
  if not playerEntity then
    return
  end
  self._checkPos = playerEntity:getWorldPosition()
  local currMode = self.csHomeManager.curHomeMainMode:GetHashCode()
  if currMode == self._currMode then
    return
  end
  self._currMode = currMode
  local currBlockId, currGridId = self.csHomeManager:GetInteractBlockIdAndGridIdWithPlayerByOffset()
  self.bind.active_Crop = currMode == HomeMainMode.CROP
  self.bind.active_buildLock = currMode == HomeMainMode.BUILD_LOCK
  if currMode == HomeMainMode.CROP then
    self.modules.module_belt:activeBelt()
    self.modules.module_homeCrop:initPage(self.modules.module_belt)
  elseif currMode == HomeMainMode.BUILD then
    self.modules.module_belt:activeBelt()
    self._curBlockId = currBlockId
    self.modules.module_belt:setBelt(L_Const.HomeShortcutBarType.HSBT_BUILD, false, {
      onClickCell = function(id, viewId)
        if not L_HomeUtil.getIsBlockUnlock(currBlockId) then
          return
        end
        local build = L_HomeStore:getBuildingInBag(id)
        if not build then
          return
        end
        local num = build.total_num - build.used_num
        if 0 < num then
          L_UI:open("pageHomePlayerEditCS", {buildingId = id})
        else
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_homeProduction_36"))
        end
      end
    })
  elseif currMode == HomeMainMode.BUILD_LOCK then
    self.modules.module_belt:hideBelt()
    local blockTpl = _BlockTpl:getTplById(currBlockId)
    local condition = _BlockTpl:getUnlockCondi(blockTpl)
    self.bind.text_buildLock = L_ConditionManager:getTipDesc({condition})
  else
    self.modules.module_belt:hideBelt()
  end
end

function this:getCurrMode()
  return self._currMode
end

function this:onEvent_syncHomeContentUnlock()
  self:updateButtonsActive()
end

function this:initSystem()
  self.systemToTrans = {
    [L_SystemConst.enum.homeScience] = {
      self.bindComponents.root_transHomeScience
    }
  }
  for systemId, v in pairs(self.systemToTrans) do
    v.locker = L_SystemUnlockManager:tryGenerateLocker(systemId)
    if v.locker then
      self:refreshSystem(systemId, false)
      v.locker:setCallback(self.refreshSystem, self, self.gameObject)
    end
  end
end

function this:refreshSystem(systemId, isUnlock)
  local node = self.systemToTrans[systemId]
  local locker = node.locker
  for _, v in ipairs(node) do
    locker:tryShowOrHide(v, isUnlock)
  end
end

function this:destroyAllLocker()
  for _, v in pairs(self.systemToTrans) do
    local locker = v.locker
    if locker then
      L_SystemUnlockManager:removeLocker(locker)
    end
  end
end

function this:activate(...)
  local data = {
    ...
  }
  self.parentPageName = data[1]
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
end

return this
