local UIMainChangeScene = BaseClass("UIMainChangeScene", UIBaseContainer)
local base = UIBaseContainer
local ResourceManager = CS.GameEntry.Resource
local btn_path = "Btn"
local btn_Text_path = "Btn/name"
local btnIcon_path = "Btn"
local OnCreate = function(self)
  base.OnCreate(self)
  self:ComponentDefine()
  self:DataDefine()
end
local OnDestroy = function(self)
  self:ComponentDestroy()
  self:DataDestroy()
  base.OnDestroy(self)
end
local OnEnable = function(self)
  base.OnEnable(self)
  self:CheckImage()
  self:CheckRadarCenterUpgrade()
  self:RefreshData()
end
local OnDisable = function(self)
  base.OnDisable(self)
end
local ComponentDefine = function(self)
  self.btn = self:AddComponent(UIButton, btn_path)
  self.btnIcon = self:AddComponent(UIImage, btnIcon_path)
  self.btnName = self:AddComponent(UIText, btn_Text_path)
  self.btn:SetOnClick(function()
    self:OnClick()
  end)
  self.lod = 1
  self.canClick = true
  self.hasRadar = true
end
local ComponentDestroy = function(self)
  self:ClearTimer()
  self.btn = nil
end
local SetLod = function(self, lod)
  if self.lod ~= lod then
    self.lod = lod
    self:RefreshData()
  end
end
local DataDefine = function(self)
end
local DataDestroy = function(self)
end
local OnAddListener = function(self)
  base.OnAddListener(self)
  self:AddUIListener(EventId.OnEnterCity, self.UpdateBtn)
  self:AddUIListener(EventId.OnEnterWorld, self.UpdateBtn)
end
local OnRemoveListener = function(self)
  base.OnRemoveListener(self)
  self:RemoveUIListener(EventId.OnEnterCity, self.UpdateBtn)
  self:RemoveUIListener(EventId.OnEnterWorld, self.UpdateBtn)
end
local RefreshData = function(self)
end
local OnClick = function(self)
  local curScene = CS.SceneManager.CurrSceneID
  if curScene == SceneManagerSceneID.City then
    local unlock, lockTips = DataCenter.LWFunctionUnlockManager:CheckCanShow(LWFunctionUnlockType.MainUI_WorldBtn)
    if not unlock then
      UIUtil.ShowTipsId(lockTips)
      return
    end
  end
  if LuaEntry.Player:GetMainWorldPos() < 0 then
    SFSNetwork.SendMessage(MsgDefines.MoveCityToWorld)
  end
  if self.canClick == false then
    return
  end
  if self.view and self.view.bottom and self.view.bottom.ClearAlComponent then
    self.view.bottom:ClearAlComponent()
    self.view.bottom:RefreshAlBtns(false)
  end
  local loginServerId = LuaEntry.Player:GetSelfServerId()
  if curScene == SceneManagerSceneID.City or curScene == SceneManagerSceneID.World then
    CS.UIGray.SetGray(self.btnIcon.transform, true, true)
    self.canClick = false
    if curScene == SceneManagerSceneID.World then
      if CrossServerUtil:GetIsCrossServer() then
        if BattleFieldUtil.InBattleField() then
          UIUtil.PlayCutSceneAnim(function()
            CrossServerUtil.OnBackSelfServerFromDragonWorld(SceneType.City)
            SceneUtils.ChangeToCity(function()
              if self.btnIcon ~= nil and not IsNull(self.btnIcon.transform) then
                CS.UIGray.SetGray(self.btnIcon.transform, false, true)
              end
              self.canClick = true
              self:ClearTimer()
              self:AfterChangeToCity()
            end)
            DataCenter.AudioManager:PlayEffect(LWSoundAssets.SwitchScene01)
          end, nil, loginServerId)
          return
        end
        GoToUtil.GotoWorldPos(SceneUtils.TileIndexToWorld(LuaEntry.Player:GetMainWorldPos(), ForceChangeScene.World, loginServerId), nil, 0.02, function()
          SceneUtils.ChangeToCity(function()
            if self.btnIcon ~= nil and not IsNull(self.btnIcon.transform) then
              CS.UIGray.SetGray(self.btnIcon.transform, false, true)
            end
            self.canClick = true
            self:ClearTimer()
            self:AfterChangeToCity()
          end)
          DataCenter.AudioManager:PlayEffect(LWSoundAssets.SwitchScene01)
        end, loginServerId)
      else
        SceneUtils.ChangeToCity(function()
          if self.btnIcon ~= nil and not IsNull(self.btnIcon.transform) then
            CS.UIGray.SetGray(self.btnIcon.transform, false, true)
          end
          self.canClick = true
          self:ClearTimer()
          self:AfterChangeToCity()
        end)
        DataCenter.AudioManager:PlayEffect(LWSoundAssets.SwitchScene01)
      end
    else
      SceneUtils.ChangeToWorld(function()
        CS.UIGray.SetGray(self.btnIcon.transform, false, true)
        self.canClick = true
        self:ClearTimer()
      end)
      DataCenter.AudioManager:PlayEffect(LWSoundAssets.SwitchScene01)
    end
  end
  if not self.canClick then
    if self.fullbackTimer then
      self.fullbackTimer:Stop()
    end
    self.fullbackTimer = TimerManager:GetInstance():DelayInvoke(function()
      self.canClick = true
      if self.btnIcon then
        CS.UIGray.SetGray(self.btnIcon.transform, false, true)
        self:CheckImage()
      end
      self.fullbackTimer = nil
    end, 2)
  end
end
local ClearTimer = function(self)
  if self.fullbackTimer then
    self.fullbackTimer:Stop()
    self.fullbackTimer = nil
  end
end
local UpdateBtn = function(self)
  self.canClick = true
  if self.btnIcon then
    CS.UIGray.SetGray(self.btnIcon.transform, false, true)
    self:CheckImage()
  end
end
local OnChangeSceneEnd = function(self)
  if self.uiPveLoading then
    self.uiPveLoading:Quit()
    self.uiPveLoading = nil
  end
end
local CheckImage = function(self)
  if SceneUtils.GetIsInCity() then
    self.btnIcon:LoadSprite("Assets/Main/Sprites/UI/UIMain/LWMainUI/cfm_zhujiemian_anniu_shijie")
    self.btnName:SetLocalText(100171)
  elseif SceneUtils.GetIsInWorld() then
    self.btnIcon:LoadSprite("Assets/Main/Sprites/UI/UIMain/LWMainUI/cfm_zhujiemian_anniu_daben")
    self.btnName:SetLocalText(100618)
  end
end
local CheckRadarCenterUpgrade = function(self)
  local canShow = true
  if canShow == false then
    CS.UIGray.SetGray(self.btnIcon.transform, true, true)
    self.canClick = false
    self.hasRadar = false
  else
    CS.UIGray.SetGray(self.btnIcon.transform, false, true)
    self.canClick = true
    self.hasRadar = true
  end
end

function UIMainChangeScene:AfterChangeToCity()
  local monoMgr = DataCenter.MonopolyManager
  if monoMgr then
    monoMgr:TryMoveCameraEnterCity()
  end
end

UIMainChangeScene.OnCreate = OnCreate
UIMainChangeScene.OnDestroy = OnDestroy
UIMainChangeScene.OnEnable = OnEnable
UIMainChangeScene.OnDisable = OnDisable
UIMainChangeScene.ComponentDefine = ComponentDefine
UIMainChangeScene.ComponentDestroy = ComponentDestroy
UIMainChangeScene.DataDefine = DataDefine
UIMainChangeScene.DataDestroy = DataDestroy
UIMainChangeScene.OnAddListener = OnAddListener
UIMainChangeScene.OnRemoveListener = OnRemoveListener
UIMainChangeScene.OnChangeSceneEnd = OnChangeSceneEnd
UIMainChangeScene.RefreshData = RefreshData
UIMainChangeScene.OnClick = OnClick
UIMainChangeScene.SetLod = SetLod
UIMainChangeScene.CheckImage = CheckImage
UIMainChangeScene.CheckRadarCenterUpgrade = CheckRadarCenterUpgrade
UIMainChangeScene.ClearTimer = ClearTimer
UIMainChangeScene.UpdateBtn = UpdateBtn
return UIMainChangeScene
