local this = class("pageMainMenu", G_UIPageBase)
local _unitTpl = L_GameTpl:getUnitTpl()

function this.bind()
  return {
    module_currency = {
      moduleName = "modulePages/moduleCurrency"
    },
    module_banner = {
      moduleName = "pages/main/moduleActivityBanner"
    },
    txt_shop_lock_name = "",
    txt_shop_unlock_name = ""
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      self:closePage()
    end,
    onClick_exit = function(self)
      L_LoginManager:exitGame()
    end,
    onClick_photograph = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.photo)
    end,
    onClick_playerCard = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.playerCard)
    end,
    onClick_announcement = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.announcemenet)
    end,
    onClick_option = function(self)
      L_UI:open("pageOption")
    end,
    onClick_shop = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.specialShop, {shopId = 9999})
    end,
    onClick_homeCenter = function(self)
      L_UI:open("pageHomeCenter")
    end,
    onClick_formation = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.formation)
    end,
    onClick_home = function(self)
      L_HomeUtil.switchInOutHome()
    end,
    onClick_pet = function(self)
      L_UI:open("pagePetBox")
    end,
    onClick_petCatalogue = function(self)
      if L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.petCatalog) then
        L_UI:open("pagePetCatalogue")
      end
    end,
    btn_commission = function(self)
      L_UI:open("pageEntrustChallenge", {
        entrustType = L_Const.EntrustType.AreaEntrust
      })
    end,
    onClick_hero = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.hero)
    end,
    onClick_adventure = function(self)
      L_UI:open("pageBigMap")
    end,
    onClick_bag = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.bag)
    end,
    onClick_mail = function(self)
      L_SystemBreakManager:jumpTo(L_SystemConst.enum.mail)
    end
  }
end

function this:ctor()
  this.super.ctor(self)
  self._needsToPlayBottomButtonAnim = true
  self.data = {
    pageLock = {}
  }
end

function this:check(options, callback)
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.mainMenu)
  if not result then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_systemMessage_04~"))
  end
  callback(result)
end

function this:preOpen(options)
  self:initPage()
  if self._needsToPlayBottomButtonAnim then
    self.bindComponents.effectAnim:Open()
    self._needsToPlayBottomButtonAnim = false
  end
  self.cameraTactics = C_CameraManager.CreateCommonTactics(self.bindComponents.virtualCam_menuVirtualCamera.gameObject, C_CinemachineBlendType.Cut, true)
  L_PhotoManager:addListener(L_PhotoManager.event.pagePhotoOpen, self.onPagePhotoOpen, self)
  AzurWorld.HUDMgr:SetAllVisible(false)
  C_BattleUIManager.GMSetUIShowState(false)
  self.bindComponents.virtualCam_menuVirtualCamera.enabled = true
  self.bindComponents.trans_lookAtPoint:SetParent(nil)
end

function this:show()
  AzurWorld.RedDotMgr:RefreshRedDot(1001)
  if self._needsToPlayBottomButtonAnim then
    self.bindComponents.effectAnim:Open()
    self._needsToPlayBottomButtonAnim = false
  end
  self.bindComponents.virtualCam_menuVirtualCamera.gameObject:SetActive(true)
  self:cameraSearchPos()
  self:refreshUI()
end

function this:hide()
  self.bindComponents.effectAnim:Close()
  self._needsToPlayBottomButtonAnim = true
end

function this:close()
  self.petCatalogRedNode = nil
  AzurWorld.RedDotMgr:UnRegistRedDot(1001)
  self.bindComponents.virtualCam_menuVirtualCamera.enabled = false
  self:destroyAllLocker()
  if L_CommonUtil.isValid(self.bindComponents.virtualCam_menuVirtualCamera.gameObject) then
    C_GameObject.Destroy(self.bindComponents.virtualCam_menuVirtualCamera.gameObject)
  end
  if L_CommonUtil.isValid(self.bindComponents.trans_lookAtPoint) then
    C_GameObject.Destroy(self.bindComponents.trans_lookAtPoint.gameObject)
  end
  if L_CommonUtil.isValid(self.cameraTactics) then
    C_CameraManager.ExitAndCompleteCamera(self.cameraTactics)
    self.cameraTactics:Dispose()
    C_CameraManager.RemoveTactics(self.cameraTactics)
  end
  L_PhotoManager:removeListener(L_PhotoManager.event.pagePhotoOpen, self.onPagePhotoOpen, self)
  AzurWorld.HUDMgr:SetAllVisible(true)
  C_BattleUIManager.GMSetUIShowState(true)
end

function this:initPage()
  self.modules.module_banner:initModule()
  self:initCurrencyList()
  self:initSystem()
  self:initReddot()
  self:checkIsInHome()
  self:checkIsInMeeting()
  self:requestAnnouncement()
  self:cameraSearchPos()
  self:IsOpenShop()
end

function this:initCurrencyList()
  if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.assetMenu) then
    local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplById(L_SystemConst.enum.mainMenu)
    self.modules.module_currency:refreshRes(tpl_sys)
  else
    self.modules.module_currency:refreshRes()
  end
end

function this:initReddot()
  local reddotMap = {
    [L_ReddotManager.DotDef.Friend] = self.bindComponents.redotFriend,
    [L_ReddotManager.DotDef.PlayerInfo] = self.bindComponents.redotPlayerCard,
    [L_ReddotManager.DotDef.Gacha] = self.bindComponents.redotGacha,
    [L_ReddotManager.DotDef.Hero] = self.bindComponents.redotHero,
    [L_ReddotManager.DotDef.Pet] = self.bindComponents.redotPet,
    [L_ReddotManager.DotDef.Achieve] = self.bindComponents.redotAchieve,
    [L_ReddotManager.DotDef.Announcement] = self.bindComponents.redotAnnouncement,
    [L_ReddotManager.DotDef.Home] = self.bindComponents.redotHome,
    [L_ReddotManager.DotDef.HomeCenter] = self.bindComponents.redotHomeCenter,
    [L_ReddotManager.DotDef.Mail] = self.bindComponents.redotMail,
    [L_ReddotManager.DotDef.HomeScience] = self.bindComponents.redotHomeScience,
    [L_ReddotManager.DotDef.SpecialShop] = self.bindComponents.redotSpecialShop,
    [L_ReddotManager.DotDef.PetCatalog] = self.bindComponents.redotPetCatalog
  }
  for key, go in pairs(reddotMap) do
    L_ReddotManager:registerReddot(go, key)
  end
  if not self.petCatalogRedNode or not L_CommonUtil.isValid(self.petCatalogRedNode) then
    self.petCatalogRedNode = AzurWorld.RedDotMgr:RegistRedDot(1001, nil, nil, function()
      return L_CatalogStore:getHasNew()
    end, true)
  end
end

function this:initSystem()
  self.systemToTrans = {
    [L_SystemConst.enum.photo] = {
      self.bindComponents.rTransPhoto
    },
    [L_SystemConst.enum.playerCard] = {
      self.bindComponents.rTransPlayerCard
    },
    [L_SystemConst.enum.friend] = {
      self.bindComponents.rTransFriend
    },
    [L_SystemConst.enum.formation] = {
      self.bindComponents.rTransFormation
    },
    [L_SystemConst.enum.home] = {
      self.bindComponents.rTransHome
    },
    [L_SystemConst.enum.homeCenter] = {
      self.bindComponents.rTransHomeCenter
    },
    [L_SystemConst.enum.pet] = {
      self.bindComponents.rTransPet
    },
    [L_SystemConst.enum.achievement] = {
      self.bindComponents.rTransAchievement
    },
    [L_SystemConst.enum.hero] = {
      self.bindComponents.rTransHero
    },
    [L_SystemConst.enum.gameAct] = {
      self.bindComponents.rTransGameAct
    },
    [L_SystemConst.enum.announcemenet] = {
      self.bindComponents.rTransAnnouncement
    },
    [L_SystemConst.enum.adventure] = {
      self.bindComponents.rTransAdventure
    },
    [L_SystemConst.enum.petCatalog] = {
      self.bindComponents.rTransTujian
    },
    [L_SystemConst.enum.option] = {
      self.bindComponents.rTransOption
    },
    [L_SystemConst.enum.quickRoulette] = {
      self.bindComponents.rTransRoulette
    },
    [L_SystemConst.enum.multiDungeon] = {
      self.bindComponents.rTransMultiDungeon
    },
    [L_SystemConst.enum.homeScience] = {
      self.bindComponents.rTransHomeScience
    },
    [L_SystemConst.enum.mail] = {
      self.bindComponents.rTransMail
    },
    [L_SystemConst.enum.specialShop] = {
      self.bindComponents.rTransSpecialShop
    },
    [L_SystemConst.enum.bag] = {
      self.bindComponents.rTransBag
    },
    [L_SystemConst.enum.entrustTask] = {
      self.bindComponents.rTransEntrustTask
    }
  }
  for systemId, v in pairs(self.systemToTrans) do
    if v ~= nil then
      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
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:checkIsInHome()
  local curModule = L_WorldManager:getCurModule()
  local isHome = curModule == L_Const.worldModule.home
  self.bindComponents.home_txt.gameObject:ActiveTrans(not isHome)
  self.bindComponents.home_exitTxt.gameObject:ActiveTrans(isHome)
end

function this:checkIsInMeeting()
  local curModule = L_WorldManager:getCurModule()
  local isMeeting = curModule == L_Const.worldModule.meeting
  self.bind.go_btn_multiDungeonEntry = false
end

function this:requestAnnouncement()
  if L_AnnouncementStore:getHasRequestAnnouncementData() == false then
    L_LoginManager:getAnnouncementOnLogin(function(data, code)
      L_AnnouncementStore:syncAnnouncementDatasByHttp(data, code)
      L_AnnouncementStore:setHasRequestAnnouncementData()
    end)
  end
end

function this:closePage()
  self.bindComponents.effectAnim:Close()
  L_UI:close(self.pageName)
end

function this:cameraSearchPos()
  local body = self.bindComponents.virtualCam_menuVirtualCamera:GetCinemachineComponent(0)
  if L_PlayerManager:getIsInWater() then
    body.m_TrackedObjectOffset = C_Vector3(0, 0.05, 0)
  else
    body.m_TrackedObjectOffset = C_Vector3(0, 0, 0)
  end
  local mainControlEntity = L_PlayerManager:getMainHeroEntity(true)
  local roleType = L_Const.roleType.hero
  if mainControlEntity:getEntityType() == L_EcsConst.aliveType.pet then
    roleType = L_Const.roleType.hero
  end
  if L_PlayerManager:inKiBoTransformMainControl() then
    roleType = L_Const.roleType.mount
  end
  local configId = mainControlEntity:getEntityData().configId
  local unitId = L_GameUtil.getUnitIdByRole(configId, roleType)
  local node = L_GameUtil.findTranByName(mainControlEntity:getTransform(), _unitTpl:getBloodBarNodeById(unitId))
  local transform = mainControlEntity:getTransform()
  local posOfLookAtPoint = L_Vector3.getPos(transform)
  if node then
    local posOfNodeTransform = L_Vector3.getPos(node)
    self.deltaPosYBetweenEntityAndNode = self.deltaPosYBetweenEntityAndNode or posOfNodeTransform.y - posOfLookAtPoint.y
    posOfLookAtPoint.y = posOfLookAtPoint.y + self.deltaPosYBetweenEntityAndNode
  end
  L_Vector3.setPos(self.bindComponents.trans_lookAtPoint, posOfLookAtPoint)
  L_Vector3.setRot(self.bindComponents.trans_lookAtPoint, L_Vector3.new(0, L_Vector3.getRot(mainControlEntity:getTransform()).y + 180, 0))
  if L_PlayerManager:inKiBoTransformMainControl() then
    L_Vector3.setPos(self.bindComponents.trans_lookAtPoint, L_Vector3.new(L_Vector3.getPos(mainControlEntity:getTransform()).x, L_Vector3.getPos(mainControlEntity:getTransform()).y + 1, L_Vector3.getPos(mainControlEntity:getTransform()).z))
    L_Vector3.setRot(self.bindComponents.trans_lookAtPoint, L_Vector3.new(0, L_Vector3.getRot(mainControlEntity:getTransform()).y + 180, 0))
  end
end

function this:IsOpenShop()
  if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.specialShop) then
    self.bindComponents.rTrans_shopUnlock.transform:ActiveTrans(true)
    self.bindComponents.rTrans_shopLock.transform:ActiveTrans(false)
  end
end

function this:onPagePhotoOpen()
  if L_CommonUtil.isValid(self.bindComponents.virtualCam_menuVirtualCamera.gameObject) then
    self.bindComponents.virtualCam_menuVirtualCamera.gameObject:SetActive(false)
  end
end

function this:refreshUI()
  self.bind.txt_shop_lock_name = L_GameTpl:getWordsTpl():getTplById("ui_main_specialshop_tag")
  self.bind.txt_shop_unlock_name = L_GameTpl:getWordsTpl():getTplById("ui_main_specialshop_tag")
end

return this
