local this = class("moduleMainCityBtnFunc", G_UIModuleBase)

function this.bind()
  return {go_btnExitDungeon = false, go_btnOpenMenu = true}
end

function this.methods()
  return {
    onClick_gameAct = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.gameAct)
    end,
    onClick_activity = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.activation)
    end,
    onClick_bag = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.bag)
    end,
    onClick_hero = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.hero)
    end,
    onClick_pet = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.pet)
    end,
    onClick_noviceTask = function(self)
      L_UI:open("pageNoviceTask")
    end,
    onClick_menu = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.mainMenu)
    end,
    onClick_exitDungeon = function(self)
      L_DungeonManager:exitDungeon(true)
    end,
    onClick_tutorial = function(self)
      L_UI:open("pageTutorial")
    end,
    onClick_formation = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.formation)
    end
  }
end

function this:open()
  L_SystemUnlockStore:listenCallFunc(L_SystemUnlockStore.event.updateSystem, self.onEvent_updateSystem, self)
  L_NoviceTaskStore:listenCallFunc(L_NoviceTaskStore.event.refreshNoviceTask, self.onEvent_refreshNoviceTask, self)
  L_SceneManager:addListener(L_SceneManager.event.switchSceneEnd, self.onEvent_switchSceneEnd, self)
  self:checkIsInDungeon()
  self:initRedDot()
  self:initSystem()
  self:bindPcKey()
end

function this:checkIsInDungeon()
  local isInDungeon = L_DungeonManager:getIsInDungeon()
  self.bind.go_btnExitDungeon = isInDungeon
  self.bind.go_btnOpenMenu = not isInDungeon
end

function this:initSystem()
  local allSystem = {
    [L_SystemConst.enum.hero] = {
      self.bindComponents.transHero
    },
    [L_SystemConst.enum.pet] = {
      self.bindComponents.transPet
    },
    [L_SystemConst.enum.bag] = {
      self.bindComponents.transBag
    },
    [L_SystemConst.enum.activation] = {
      self.bindComponents.transActivity
    },
    [L_SystemConst.enum.gameAct] = {
      self.bindComponents.transGameAct
    },
    [L_SystemConst.enum.mainMenu] = {
      self.bindComponents.transMenu
    },
    [L_SystemConst.enum.tutorial] = {
      self.bindComponents.transBtnTutorial
    },
    [L_SystemConst.enum.home] = {
      self.bindComponents.transBtnHome
    },
    [L_SystemConst.enum.formation] = {
      self.bindComponents.transBtnFormation
    }
  }
  for i, v in pairs(allSystem) do
    for _, rectTrans in pairs(v) do
      rectTrans.gameObject:ActiveTrans(false)
    end
  end
  if L_DungeonManager:getIsInTrainTaskDungeon() then
    self.systemToTrans = {
      [L_SystemConst.enum.hero] = {
        self.bindComponents.transHero
      },
      [L_SystemConst.enum.pet] = {
        self.bindComponents.transPet
      }
    }
  else
    self.systemToTrans = table.clone(allSystem)
  end
  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)
    else
      for _, rectTrans in pairs(v) do
        rectTrans.gameObject:ActiveTrans(true)
      end
    end
  end
  local specialSystemId = {
    L_SystemConst.enum.noviceTask
  }
  for _, systemId in pairs(specialSystemId) do
    self:refreshSpecialSystem(systemId)
  end
end

function this:initRedDot()
  local reddotMap = {
    [L_ReddotManager.DotDef.Activity] = self.bindComponents.activityDot,
    [L_ReddotManager.DotDef.Hero] = self.bindComponents.heroDot,
    [L_ReddotManager.DotDef.Bag] = self.bindComponents.bagDot,
    [L_ReddotManager.DotDef.Pet] = self.bindComponents.petDot,
    [L_ReddotManager.DotDef.GameAct_ActCenter] = self.bindComponents.gameActDot,
    [L_ReddotManager.DotDef.NoviceTask] = self.bindComponents.noviceTaskDot,
    [L_ReddotManager.DotDef.Menu] = self.bindComponents.menuDot
  }
  for key, go in pairs(reddotMap) do
    L_ReddotManager:registerReddot(go, key)
  end
end

function this:bindPcKey()
  L_ShortCutManager:addShortKeyFlag(self, "F3", L_Const.AnchorType.Bottom, self.bindComponents.transGameAct, L_Vector3.new(0, -3, 0))
  L_ShortCutManager:addShortKeyFlag(self, "F2", L_Const.AnchorType.Bottom, self.bindComponents.transActivity, L_Vector3.new(0, -3, 0))
  L_ShortCutManager:addShortKeyFlag(self, "F1", L_Const.AnchorType.Bottom, self.bindComponents.transNoviceTask, L_Vector3.new(0, -3, 0))
  L_ShortCutManager:addShortKeyFlag(self, L_Const.settingType.openBag, L_Const.AnchorType.Bottom, self.bindComponents.transBag, L_Vector3.new(0, -3, 0))
  L_ShortCutManager:addShortKeyFlag(self, L_Const.settingType.openPet, L_Const.AnchorType.Bottom, self.bindComponents.transPet, L_Vector3.new(0, -3, 0))
  L_ShortCutManager:addShortKeyFlag(self, L_Const.settingType.openHero, L_Const.AnchorType.Bottom, self.bindComponents.transHero, L_Vector3.new(0, -3, 0))
  L_ShortCutManager:addShortKeyFlag(self, "N", L_Const.AnchorType.Bottom, self.bindComponents.transBtnTutorial, L_Vector3.new(0, 2, 0), 0.8)
  L_ShortCutManager:addShortKeyFlag(self, "L", L_Const.AnchorType.Bottom, self.bindComponents.transBtnFormation, L_Vector3.new(0, -3, 0))
  if L_DungeonManager:getIsInDungeon() then
    L_ShortCutManager:addShortKeyFlag(self, L_Const.settingType.openMenu, L_Const.AnchorType.Bottom, self.bindComponents.transExitDungeon)
  else
    L_ShortCutManager:addShortKeyFlag(self, L_Const.settingType.openMenu, L_Const.AnchorType.Bottom, self.bindComponents.transMenu)
  end
end

function this:onEvent_updateSystem(systemId)
  self:refreshSpecialSystem(systemId)
end

function this:onEvent_refreshNoviceTask()
  self:refreshSpecialSystem(L_SystemConst.enum.noviceTask)
end

function this:onEvent_switchSceneEnd()
  self:checkIsInDungeon()
end

function this:close()
  self:destroyAllLocker()
  L_ShortCutManager:clearShortKeyFlag(self)
  L_SystemUnlockStore:unListenCallFunc(L_SystemUnlockStore.event.updateSystem, self.onEvent_updateSystem, self)
  L_NoviceTaskStore:unListenCallFunc(L_NoviceTaskStore.event.refreshNoviceTask, self.onEvent_refreshNoviceTask, self)
  L_SceneManager:removeListener(L_SceneManager.event.switchSceneEnd, self.onEvent_switchSceneEnd)
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:refreshSystem(systemId, isUnlock)
  local node = self.systemToTrans[systemId]
  local locker = node.locker
  for _, v in ipairs(node) do
    local isNeedShow = locker:tryShowOrHide(v, isUnlock)
    if isNeedShow then
      local lock = v:Find("lock")
      local unlock = v:Find("unlock")
      if lock then
        lock.gameObject:ActiveTrans(not isUnlock)
      end
      if unlock then
        unlock.gameObject:ActiveTrans(isUnlock)
      end
    end
  end
end

function this:checkIfShowNoviceTaskBtn()
  if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.noviceTask) then
    if L_NoviceTaskStore:checkIfAllTaskCompletedAndAllRewardReceived() then
      return false
    end
    return true
  end
  return false
end

function this:refreshSpecialSystem(systemId)
  self.systemId2Trans = self.systemId2Trans or {
    [L_SystemConst.enum.noviceTask] = self.bindComponents.transNoviceTask
  }
  self.systemId2CheckFunc = self.systemId2CheckFunc or {
    [L_SystemConst.enum.noviceTask] = "checkIfShowNoviceTaskBtn"
  }
  if not self.systemId2Trans[systemId] then
    return
  end
  local show = L_SystemUnlockStore:getSystemIsOpen(systemId) and self[self.systemId2CheckFunc[systemId]](self) and not L_DungeonManager:getIsInTrainTaskDungeon()
  self.systemId2Trans[systemId].gameObject:ActiveTrans(show)
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

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

return this
