local Const = require("Scene.LWBattle.Const")
local UIMainResourceProgress = require("UI.LWMainUI.Component.UIMainTop.UIMainResourceProgress")
local UIMysteryFeatureProgress = require("UI.UIParkour.MainUI.Component.UIMysteryFeatureProgress")
local Resource = CS.GameEntry.Resource
local ParkourJoystick = require("UI.UIParkour.MainUI.Component.ParkourJoystick")
local ParkourBonusPanel = require("UI.UIParkour.MainUI.Component.ParkourBonus.ParkourBonusPanel")
local TacticalWeaponSkillNodeComponent = require("UI.UIParkour.MainUI.Component.TacticalWeaponSkillNodeComponent")
local ParkourWinIconItemRender = require("UI.UIParkour.MainUI.Component.ParkourWinIconItemRender")
local UIParkourBattleMainView = BaseClass("UIParkourBattleMainView", UIBaseView)
local base = UIBaseView
local Localization = CS.GameEntry.Localization
local UnityLayoutElement = typeof(CS.UnityEngine.UI.LayoutElement)
local speedUpEff = "SafeArea/TopEffect/Eff_UI_suduxian"
local safeArea = "SafeArea/"
local BossNoticeCD = 5
local BossNoticeHideTime = 2
local ParkourBonusPanelPrefabPath = "Assets/Main/Prefabs/UI/ParkourBattle/UIParkourBattleMainBonusPanel.prefab"
local win_condition_hor_layout_path = "SafeArea/WinCondition/WinConditionHorLayout"
local parkour_win_icon_item_render_path = "SafeArea/WinCondition/ParkourWinIconItemRender"
local ResourceArray = {
  ResourceType.Wood,
  ResourceType.Metal,
  ResourceType.Food,
  ResourceType.Gold
}

function UIParkourBattleMainView:OnCreate()
  base.OnCreate(self)
  self:ComponentDefine()
  self:InitWinCondition()
  self:InitAllWinConditionBar()
  self:InitRes()
  self:InitLevelInfo()
  if self:GetUserData().onOpen then
    self:GetUserData().onOpen()
  end
  self.bonusEnter = false
  EventManager:GetInstance():Broadcast(EventId.ParkourBattleMainViewCreated)
end

function UIParkourBattleMainView:OnDestroy()
  self:ComponentDestroy()
  self.bonusEnter = nil
  self.isMultipleWinCondition = nil
  base.OnDestroy(self)
  if self.featureResourceReq ~= nil then
    self.featureResourceReq:Destroy()
    self.featureResourceReq = nil
  end
  self:ClearAllDelayTimers()
end

function UIParkourBattleMainView:ComponentDefine()
  if CS.CommonUtils.IsDebug() then
    self.GMText = self:AddComponent(UIText, "GmWinBtn/GMText")
    local logInfo = DataCenter.LWBattleManager:GetCurBattleLogic():GetStageId()
    if LuaEntry and LuaEntry.Player then
      local player = LuaEntry.Player
      if player.uid then
        logInfo = logInfo .. [[

 uid:]] .. player.uid
      end
      if player.abTest then
        if player.abTest == ABTestType.B then
          logInfo = logInfo .. [[

 abTest:B]]
        elseif player.abTest == ABTestType.A then
          logInfo = logInfo .. [[

 abTest:A]]
        end
        logInfo = logInfo .. [[

 gpuskin:]] .. tostring(DataCenter.FunctionOnManager:IsServerSwitchOn(ServerSwitch.PveUseGpuSkin))
      end
    end
    self.GMText:SetText(logInfo)
    self.gm_win_btn = self:AddComponent(UIButton, "GmWinBtn")
    self.gm_win_btn:SetOnClick(function()
      DataCenter.LWBattleManager:GetCurBattleLogic():OnBattleWin()
    end)
    self.jump_guide_btn = self:AddComponent(UIButton, "SafeArea/Guide/jumpGuide")
    self.jump_guide_btn:SetOnClick(function()
      DataCenter.LWGuideManager:ClearData()
      DataCenter.LWBattleManager:SetGamePause(false)
      DataCenter.LWBattleManager:GetCurBattleLogic():OnBattleWin()
      local CanvasNormal = UIManager:GetInstance():GetLayer(UILayer.Scene.Name)
      if CanvasNormal then
        CanvasNormal.gameObject:SetActive(true)
      end
      SFSNetwork.SendMessage(MsgDefines.LWSaveGuide, GuideState.Over)
    end)
    self.jump_guide_btn:SetActive(false)
  else
    self.transform:Find("SafeArea/Guide/jumpGuide").gameObject:SetActive(false)
    self.transform:Find("GmWinBtn").gameObject:SetActive(false)
  end
  self.safeArea = self:AddComponent(UIBaseContainer, "SafeArea")
  self.winConditionNode = self:AddComponent(UIBaseComponent, "SafeArea/WinCondition")
  self.winConditionNode:SetActive(true)
  self.winConditionArea = self.transform:Find("SafeArea/WinCondition")
  self.winConditionArea:Set_localScale(0, 0, 0)
  self.winConditionIcon = self:AddComponent(UIImage, "SafeArea/WinCondition/WinIcon")
  self.winConditionSlider = self:AddComponent(UISlider, "SafeArea/WinCondition/WinConditionHorLayout/Slider")
  self.winConditionSliderEff = self:AddComponent(UIBaseComponent, "SafeArea/WinCondition/WinConditionHorLayout/Slider/Eff_ui_beizengmen_guanka_jindu")
  self.winConditionSliderEff:SetActive(false)
  self.winConditionBarImg = self:AddComponent(UIImage, "SafeArea/WinCondition/WinConditionHorLayout/Slider/FillArea/Fill")
  self.winConditionText = self:AddComponent(UIText, "SafeArea/WinCondition/WinBarText")
  self.winConditionIconView = self:AddComponent(UIBaseContainer, "SafeArea/WinCondition/WinConditionHorLayout/WinIconList")
  self.winConditionIconListCell = {}
  self.winConditionIconItemPrefab = self.transform:Find(parkour_win_icon_item_render_path).gameObject
  self.winConditionIconItemPrefab:GameObjectCreatePool()
  self.winConditionHorLayout = self:AddComponent(UIBaseContainer, win_condition_hor_layout_path)
  local hLayout = typeof(CS.BidirectionalHorizontalLayoutGroup)
  self.winConditionIconListHorLayout = self.winConditionIconView.gameObject:GetComponent(hLayout)
  self.winConditionSliderLayoutElement = self.winConditionSlider.gameObject:GetComponent(UnityLayoutElement)
  self.animator = self:AddComponent(UIAnimator, "")
  self.winBanner = self:AddComponent(UIBaseComponent, "WinBanner")
  self.winBanner:SetActive(false)
  self.winBannerTxt = self:AddComponent(UIText, "WinBanner/WinBannerText")
  self.winBannerTxt:SetLocalText(GameDialogDefine.MISSION_COMPLETE)
  self.speedUpEffect = self:AddComponent(UIBaseContainer, speedUpEff)
  self.speedUpEffect:SetActive(false)
  self.joystick = self:AddComponent(ParkourJoystick, "SafeArea/Joystick")
  self.joystick:SetActive(true)
  self.joystick:ReInit()
  DataCenter.LWBattleManager.logic:SetJoystick(self.joystick)
  self.back_btn = self:AddComponent(UIButton, "SafeArea/BackBtn")
  self.back_btn:SetActive(true)
  self.back_btn:SetOnClick(function()
    self:OnExitBtnClick()
  end)
  self.map_btn = self:AddComponent(UIButton, "Map")
  self.startGame_Btn = self:AddComponent(UIButton, "SafeArea/Guide/btnContent/startGameBtn")
  self.startGame_text = self:AddComponent(UIText, "SafeArea/Guide/btnContent/startGameBtn/Text")
  self.startGame_text:SetLocalText(100833)
  self.map_btn:SetOnClick(function()
    self:OnMapClick()
  end)
  self.startGame_Btn:SetOnClick(function()
    self:OnStartGameClick()
  end)
  self.loginGameBtn = self:AddComponent(UIButton, "SafeArea/Guide/btnContent/loginGameBtn")
  self.loginBtnText = self:AddComponent(UIText, "SafeArea/Guide/btnContent/loginGameBtn/loginBtnText")
  self.loginBtnText:SetLocalText(110008)
  self.loginGameBtn:SetOnClick(function()
    self:OnLoginGameClick()
  end)
  local state = DataCenter.AccountManager:GetAccountBindState()
  self.loginGameBtn:SetActive(state ~= AccountBandState.Band)
  if CS.GameEntry.Setting.IsReview then
    self.loginGameBtn:SetActive(false)
  end
  self.guide = self.transform:Find("SafeArea/Guide").gameObject
  self.resContent = self:AddComponent(UIBaseContainer, "SafeArea/ResNode")
  self.resContent:SetActive(true)
  self.resPrefab = self.transform:Find("SafeArea/ResNode/UIMainTopResourceCell").gameObject
  self.resPrefab:GameObjectCreatePool()
  self.resPrefab:SetActive(false)
  self.resCells = {}
  self.punchOK = 0
  self.missionBar = self:AddComponent(UISimpleAnimation, safeArea .. "Mission")
  self.missionText = self:AddComponent(UIText, safeArea .. "Mission/hengfu/Text_0")
  self.missionTarText = self:AddComponent(UIText, safeArea .. "Mission/hengfu/Text_1")
  self.missionBar:SetActive(false)
  self.bossNoticeContainer = self:AddComponent(UIBaseContainer, "BossComing")
  self.bossNotice = self:AddComponent(UIAnimator, "BossComing")
  self.bossNotice.gameObject:SetActive(false)
  self.bossNoticeText = self.bossNoticeContainer:AddComponent(UIText, "Text_0")
  self.lastBossNoticeTime = Time.realtimeSinceStartup
  self.tacticalWeaponSkillNodeComponent = self:AddComponent(TacticalWeaponSkillNodeComponent, "SafeArea/TacticalWeaponSkillNode")
  self.tacticalWeaponSkillNodeComponent:SetActive(true)
  self.levelNameText = self:AddComponent(UIText, "SafeArea/LevelNameText")
  self.levelNameText.transform:Set_localScale(0, 0, 0)
  local param = self:GetUserData()
  if param and param.dashBonus then
    self:GetParkourBonusDash()
  end
  self.btnContentContainer = self:AddComponent(UIBaseContainer, "SafeArea/Guide/btnContent")
  self.btnContentContainer:SetAnchorMinXY(0.5, 0.5)
  self.btnContentContainer:SetAnchorMaxXY(0.5, 0.5)
  self.btnContentContainer:SetPivotXY(0.5, 0.5)
  self.btnContentContainer:SetAnchoredPositionXY(0, -360)
end

function UIParkourBattleMainView:ComponentDestroy()
  self.safeArea = nil
  self.back_btn = nil
  self.winConditionIconListCell = nil
  self.winConditionIconView:RemoveComponents(ParkourWinIconItemRender)
  if self.winConditionIconItemPrefab then
    self.winConditionIconItemPrefab:GameObjectRecycleAll()
  end
  self:ClearResContent()
  self.resContent = nil
  self.resPrefab = nil
  self.resCells = nil
  self.joystick = nil
  if self.showTimer then
    self.showTimer:Stop()
    self.showTimer = nil
  end
  if self.showCondition then
    self.showCondition:Stop()
    self.showCondition = nil
  end
  if self.bossNoticeExitTimer then
    self.bossNoticeExitTimer:Stop()
    self.bossNoticeExitTimer = nil
  end
  if IsNotNull(self.winConditionAreaTweener) then
    self.winConditionAreaTweener:Kill()
  end
  self.winConditionAreaTweener = nil
  if IsNotNull(self.levelNameTextTweener) then
    self.levelNameTextTweener:Kill()
  end
  self.levelNameTextTweener = nil
  if IsNotNull(self.winConditionTextTweener) then
    self.winConditionTextTweener:Kill()
  end
  self.winConditionTextTweener = nil
  self.parkourBonusPanel = nil
  self.bonusDash = nil
  self.bossHpBar = nil
  if self.openEffectTimer then
    self.openEffectTimer:Stop()
    self.openEffectTimer = nil
  end
  self.winConditionHorLayout = nil
  self.winConditionIconListHorLayout = nil
  self.winConditionSliderLayoutElement = nil
end

function UIParkourBattleMainView:ClearResContent()
  self.resCells = {}
  self.resContent:RemoveComponents(UIMainResourceProgress)
  self.resPrefab:GameObjectRecycleAll()
end

function UIParkourBattleMainView:OnAddListener()
  base.OnAddListener(self)
  self:AddUIListener(EventId.SquadSuperArmorStateChange, self.SquadSuperArmorChange)
  self:AddUIListener(EventId.OnPVEBattleGetGoods, self.OnPVEBattleGetGoods)
  self:AddUIListener(EventId.ParkourWinConditionRefresh, self.UpdateWinConditionBar)
  self:AddUIListener(EventId.ParkourBossEnterBattle, self.OnBossEnter)
  self:AddUIListener(EventId.ParkourBossHpViewChanged, self.OnParkourBossHpViewChanged)
  self:AddUIListener(EventId.ParkourBossHpViewDead, self.OnParkourBossHpViewDead)
end

function UIParkourBattleMainView:OnRemoveListener()
  self:RemoveUIListener(EventId.SquadSuperArmorStateChange, self.SquadSuperArmorChange)
  self:RemoveUIListener(EventId.OnPVEBattleGetGoods, self.OnPVEBattleGetGoods)
  self:RemoveUIListener(EventId.ParkourWinConditionRefresh, self.UpdateWinConditionBar)
  self:RemoveUIListener(EventId.ParkourBossEnterBattle, self.OnBossEnter)
  self:RemoveUIListener(EventId.ParkourBossHpViewChanged, self.OnParkourBossHpViewChanged)
  self:RemoveUIListener(EventId.ParkourBossHpViewDead, self.OnParkourBossHpViewDead)
  base.OnRemoveListener(self)
end

function UIParkourBattleMainView:InitRes()
  self.resData = {}
  local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
  if logic.param.enterType == PVEEnterType.StageFeatureBuilding then
    local feature = DataCenter.StageFeatureBuildingManager:GetStageFeatureBuildingTemplate(logic.param.featureId)
    local index = 1
    if 1 < #feature.winType then
      index = table.indexof(feature.stages, logic.param.levelId)
    end
    local iconUrl
    local resType = 2
    if feature.winType[index] ~= 3 then
      self:CreateDelayTimer(function()
        if self.transform then
          if #feature.winType == 1 then
            if feature.winType[index] == 1 then
              local content = Localization:GetString("newbies_fuben_coin_loading", tostring(feature.winNeedCount[index]))
              self:ShowCondition("", content, 1)
            elseif feature.winType[index] == 2 then
              if #feature.stages == 1 then
                local content = Localization:GetString("newbies_fuben_save_loading", tostring(feature.winNeedCount[index]))
                self:ShowCondition("", content, 1)
              else
                local content = Localization:GetString("newbies_fuben_battlefront_loading", tostring(feature.winNeedCount[index]))
                self:ShowCondition("", content, 1)
              end
            end
          elseif feature.winType[index] == 1 then
            local content = Localization:GetString("newbies_fuben_coin_loading", tostring(feature.winNeedCount[index]))
            self:ShowCondition("", content, 1)
          elseif feature.winType[index] == 2 then
            local content = Localization:GetString("newbies_fuben_save_loading", tostring(feature.winNeedCount[index]))
            self:ShowCondition("", content, 1)
          elseif feature.winType[index] == 0 then
            local content = Localization:GetString("newbies_fuben_attack_loading")
            self:ShowCondition("", content, 1)
          end
        end
      end, 1)
      if feature.winType[index] == 1 or feature.winType[index] == 2 then
        self.featureResourceReq = Resource:InstantiateAsync("Assets/Main/Prefabs/UI/ParkourBattle/GrowResource.prefab")
        self.featureResourceReq:completed("+", function()
          self.resData[resType] = 0
          local item = self.featureResourceReq.gameObject
          item.transform:SetParent(self.resContent.transform)
          item.name = "Res" .. 1
          local cell = self.resContent:AddComponent(UIMysteryFeatureProgress, item.name)
          local param = {}
          if feature.winType[index] == 1 then
            iconUrl = "Assets/Main/Sprites/UI/LWCommon/Sprite/cfm_zhujiemian_tubiao_ziyuan1.png"
            cell:SetActive(false)
          elseif feature.winType[index] == 2 then
            iconUrl = "Assets/Main/Sprites/UI/UIBuildBubble/cfm_zhujiemian_qipao_zaobing.png"
            param.soldier = true
            cell:SetActive(true)
          end
          param.resourceType = resType
          param.iconName = iconUrl
          param.showCount = 0
          param.maxCount = feature.winNeedCount[index]
          cell:SetZero(param)
          self.resCells[resType] = cell
        end)
      end
    end
  elseif logic.data.collectType then
    local resType, iconUrl, isSoldier
    if logic.data.collectType == DetectEventRetryTaskCollectType.Soldier then
      resType = 2
      iconUrl = "Assets/Main/Sprites/UI/UIBuildBubble/cfm_zhujiemian_qipao_zaobing.png"
      isSoldier = true
    else
      resType = DetectEventRetryTaskCollectType2ResourceType[logic.data.collectType]
      iconUrl = DataCenter.ResourceManager:GetResourceIconByType(resType)
      isSoldier = false
    end
    self.featureResourceReq = Resource:InstantiateAsync("Assets/Main/Prefabs/UI/ParkourBattle/GrowResource.prefab")
    self.featureResourceReq:completed("+", function()
      self.resData[resType] = 0
      local item = self.featureResourceReq.gameObject
      item.transform:SetParent(self.resContent.transform)
      item.name = "Res" .. 1
      local cell = self.resContent:AddComponent(UIMysteryFeatureProgress, item.name)
      local param = {}
      param.soldier = isSoldier
      cell:SetActive(isSoldier)
      param.resourceType = resType
      param.iconName = iconUrl
      param.showCount = 0
      param.maxCount = logic.data.collectMaxNum
      cell:SetZero(param)
      self.resCells[resType] = cell
    end)
  else
    for i = 1, 1 do
      local resType = ResourceArray[i]
      self:AddResCell(resType)
    end
  end
end

function UIParkourBattleMainView:AddResCell(resType)
  self.resData[resType] = 0
  local item = self.resPrefab:GameObjectSpawn(self.resContent.transform)
  item.name = "Res" .. resType
  local cell = self.resContent:AddComponent(UIMainResourceProgress, item.name)
  local param = {}
  param.resourceType = resType
  param.iconName = DataCenter.ResourceManager:GetResourceIconByType(resType)
  param.showCount = 0
  cell:SetZero(param)
  cell:SetActive(false)
  self.resCells[resType] = cell
end

function UIParkourBattleMainView:OnPVEBattleGetGoods(param)
  if self.bonusEnter then
    self:OnBonusRefresh(param)
    return
  end
  if self.resCells[param.goodsId] == nil then
    self:AddResCell(param.goodsId)
  end
  self.resCells[param.goodsId]:SetActive(true)
  local pic = DataCenter.ResourceManager:GetResourceIconByType(param.goodsId)
  local srcPos = CS.CSUtils.WorldPositionToUISpacePosition(param.worldPosition)
  local targetPos = self.resCells[param.goodsId]:GetResourcePos()
  local FlyParkourPath = "Assets/_Art/Effect/prefab/ui/Common/FlyParkour.prefab"
  DataCenter.FlyController.DoFlyForLua(pic, nil, 1, srcPos, targetPos, 40, 40, function()
    self:AddRes(param.goodsId, param.goodsCount)
  end, FlyParkourPath, nil, -50, nil, nil)
end

function UIParkourBattleMainView:AddRes(goodsId, goodsCount)
  local param = {}
  param.resourceType = goodsId
  self.resData[goodsId] = self.resData[goodsId] + goodsCount
  param.showCount = self.resData[goodsId]
  self.resCells[goodsId]:SetData(param)
end

function UIParkourBattleMainView:OnStartGameClick()
  DataCenter.LWSoundManager:PlaySound(10002)
  self.winConditionArea.gameObject:SetActive(true)
  self.guide:SetActive(false)
  DataCenter.LWGuideManager:GuideStartGame()
end

function UIParkourBattleMainView:OnLoginGameClick()
  UIManager:GetInstance():OpenWindow(UIWindowNames.UIChooseSwitchAccount, 110008)
end

function UIParkourBattleMainView:OnExitBtnClick()
  if DataCenter.LWBattleManager.logic.winTimer or DataCenter.LWBattleManager.gameOver then
    return
  end
  if DataCenter.LWBattleManager:IsFromActBreakSunday() then
    DataCenter.LWBattleManager:SetGamePause(true)
    UIUtil.ShowMessage(Localization:GetString("activity_breakthrough_tips_20"), 2, "breakthough_button_04", "breakthough_button_03", function()
      self:OnExit()
      local firstActId = DataCenter.ActFrontBreakSundayDataManager:GetFirstActId()
      if firstActId ~= -1 then
        GoToUtil.GotoOpenView(UIWindowNames.UIActivityCenterTable, firstActId)
      end
    end, function()
      DataCenter.LWBattleManager:SetGamePause(false)
    end, function()
      DataCenter.LWBattleManager:SetGamePause(false)
    end)
    return
  end
  local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
  if logic.param.enterType == PVEEnterType.StageFeatureBuilding then
    DataCenter.StageFeatureBuildingManager:OnExitBattle(logic.param.buildUuid)
  end
  self:OnExit()
end

function UIParkourBattleMainView:OnMapClick()
end

function UIParkourBattleMainView:OnExit()
  PostEventLog.BattleResultLog(PVEType.Parkour, 2)
  local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
  if logic and logic.param and logic.param.enterType == PVEEnterType.TowerupJeepAdventure then
    DataCenter.LWBattleManager:SetBattleExitFlag(true)
  end
  self.ctrl:CloseSelf()
  DataCenter.LWBattleManager:Exit(nil, "quit")
end

local GetWinConditionIcon = function(winType)
  local spriteName = ""
  if winType == Const.ParkourWinType.KillTargetMonster then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_1.png"
  elseif winType == Const.ParkourWinType.KillMonster then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_1"
  elseif winType == Const.ParkourWinType.FinishPoint then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_3"
  elseif winType == Const.ParkourWinType.Time then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_2"
  elseif winType == Const.ParkourWinType.KillBoss then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_4"
  elseif winType == Const.ParkourWinType.SaveWorker then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/cfm_guanqia_tubiao_yingxiong.png"
  elseif winType == Const.ParkourWinType.BlastStandingWaterBottle then
    spriteName = "Assets/Main/Sprites/UI/UIZombieBattleMain/lyt_guanqia_tubiao_pingzi"
  end
  return spriteName
end
local GetWinConditionShowSliderStyle = function(winType)
  if winType == Const.ParkourWinType.KillTargetMonster or winType == Const.ParkourWinType.KillMonster or winType == Const.ParkourWinType.FinishPoint or winType == Const.ParkourWinType.Time or winType == Const.ParkourWinType.SaveWorker then
    return true
  end
  return false
end

function UIParkourBattleMainView:InitWinCondition()
  local isShowSliderStyleWinType = self:InitWinConditionShowStyle()
  if isShowSliderStyleWinType then
    local spriteName = GetWinConditionIcon(isShowSliderStyleWinType)
    self.winConditionIcon:LoadSprite(spriteName)
    if isShowSliderStyleWinType == Const.ParkourWinType.FinishPoint or isShowSliderStyleWinType == Const.ParkourWinType.Time then
      self.winConditionBarImg:LoadSprite("Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_jindutiao_3")
    else
      self.winConditionBarImg:LoadSprite("Assets/Main/Sprites/UI/UIZombieBattleMain/guanqia_cfm_tubiao_jindutiao_2")
    end
  end
  local isLevelOneGuide = DataCenter.LWGuideManager:GetCurGuideId() == GuideState.LevelOne
  self.winConditionArea.gameObject:SetActive(not isLevelOneGuide)
  self:SetWinConditionBoss()
  local param = self:GetUserData()
  if param.showGuide then
    self.back_btn:SetActive(false)
    self.guide:SetActive(true)
    self.winConditionArea.gameObject:SetActive(false)
    DataCenter.LWBattleManager:SetGamePause(true)
  else
    self.back_btn:SetActive(true)
    if DataCenter.LWGuideManager:GetIsStart() and DataCenter.LWGuideManager:GetCurGuideId() == GuideState.LevelOne then
      self.back_btn:SetActive(false)
    end
    self.guide:SetActive(false)
  end
  if isLevelOneGuide then
    if DataCenter.AccountManager:IsShumeiCreateAccountRisk() then
      local isOn = CS.ClientSwitch.IsOn(CS.ClientSwitch.ENABLE_SHUMEI_CREATE_ACCOUNT_RISK_BAN)
      if isOn then
        self.startGame_Btn:SetActive(false)
      end
      PostEventLog.Track(PostEventLog.Defines.ShumeiCreateAccountRiskBan, {emulator_baned = "hide_start"})
    elseif CS.ShumeiSdkManager.Instance:IsForbidCreateRole() then
      self.startGame_Btn:SetActive(false)
      PostEventLog.Track("s_shu_mei_create_role_ban", {s_para1 = "hide_start"})
    elseif CS.ClientSwitch.IsOn(CS.ClientSwitch.ENABLE_ACCOUNT_SELECT_STATE) and CS.GameEntry.GlobalData.LoginServerError == false then
      self:RemoveBtnJustStartGame()
    end
  end
  local isShowLevelNameText = self:IsShowLevelNameText()
  if isShowLevelNameText then
    self.winConditionArea:Set_anchoredPosition(0, -77)
  else
    self.winConditionArea:Set_anchoredPosition(0, 0)
  end
end

function UIParkourBattleMainView:InitWinConditionShowStyle()
  local winConditionCount = table.count(DataCenter.LWBattleManager.logic.winConditions)
  self.isMultipleWinCondition = 1 < winConditionCount
  local isShowSliderStyleWinType
  for winType, v in pairs(DataCenter.LWBattleManager.logic.winConditions) do
    local showSliderStyle = GetWinConditionShowSliderStyle(winType)
    if showSliderStyle then
      isShowSliderStyleWinType = winType
      break
    end
  end
  if winConditionCount == 1 then
    if isShowSliderStyleWinType then
      self.winConditionHorLayout:SetAnchoredPositionXY(0, -32)
      self:SetWinConditionSliderStyleShowState(true)
      self:SetWinConditionIconListStyleShowState(false)
      self.winConditionIcon:SetAnchoredPositionXY(0, 50)
      self.winConditionText:SetAnchoredPositionXY(0, -30)
      self.winConditionText:SetSizeDeltaX(270)
      self.winConditionSliderLayoutElement.minWidth = 270
      self.winConditionSliderLayoutElement.preferredWidth = 270
    else
      self.winConditionHorLayout:SetAnchoredPositionXY(0, 18)
      self:SetWinConditionSliderStyleShowState(false)
      self:SetWinConditionIconListStyleShowState(true)
      self.winConditionIconListHorLayout.childAlignment = CS.UnityEngine.TextAnchor.MiddleCenter
    end
  else
    self.winConditionHorLayout:SetAnchoredPositionXY(0, 0)
    if isShowSliderStyleWinType then
      self:SetWinConditionSliderStyleShowState(true)
      self:SetWinConditionIconListStyleShowState(true)
      self.winConditionIcon:SetAnchoredPositionXY(-120, 35)
      self.winConditionText:SetAnchoredPositionXY(-30, 1)
      self.winConditionText:SetSizeDeltaX(160)
      self.winConditionSliderLayoutElement.minWidth = 157
      self.winConditionSliderLayoutElement.preferredWidth = 157
      self.winConditionIconListHorLayout.childAlignment = CS.UnityEngine.TextAnchor.MiddleLeft
    else
      self:SetWinConditionSliderStyleShowState(false)
      self:SetWinConditionIconListStyleShowState(true)
      self.winConditionIconListHorLayout.childAlignment = CS.UnityEngine.TextAnchor.MiddleCenter
    end
  end
  return isShowSliderStyleWinType
end

function UIParkourBattleMainView:SetWinConditionSliderStyleShowState(show)
  self.winConditionSlider:SetActive(show)
  self.winConditionIcon:SetActive(show)
  self.winConditionText:SetActive(show)
end

function UIParkourBattleMainView:SetWinConditionIconListStyleShowState(show)
  self.winConditionIconView:SetActive(show)
end

function UIParkourBattleMainView:RemoveBtnJustStartGame()
  self.winConditionArea.gameObject:SetActive(true)
  self.guide:SetActive(false)
  DataCenter.LWGuideManager:GuideStartGame()
end

function UIParkourBattleMainView:InitLevelInfo()
  local isShowLevelNameText = self:IsShowLevelNameText()
  self.levelNameText:SetActive(isShowLevelNameText)
  if not isShowLevelNameText then
    return
  end
  local param = DataCenter.LWBattleManager.param
  local fromActFrontBreakSunday = param.fromActFrontBreakSunday
  local stageId = param.levelId
  local levelTitle = ""
  if fromActFrontBreakSunday then
    local stageIdIndex = DataCenter.ActFrontBreakSundayDataManager:GetActData(param.frontBreakSundayActId):GetStageIndex(stageId)
    local stagesCount = #DataCenter.ActFrontBreakSundayDataManager:GetActData(param.frontBreakSundayActId).stageIds
    levelTitle = Localization:GetString("activity_breakthrough_tips_19", stageIdIndex, stagesCount)
  else
    local levelTitlePrefixKey = GetTableData(LuaEntry.Player:GetABTestTableName(TableName.LW_Stage_Feature), stageId, "name")
    local order = GetTableData(LuaEntry.Player:GetABTestTableName(TableName.LW_Stage_Feature), stageId, "order")
    levelTitle = Localization:GetString(levelTitlePrefixKey, order)
  end
  self.levelNameText:SetText(levelTitle)
end

function UIParkourBattleMainView:IsShowLevelNameText()
  local isLevelOneGuide = DataCenter.LWGuideManager:GetCurGuideId() == GuideState.LevelOne
  if isLevelOneGuide then
    return false
  end
  local param = DataCenter.LWBattleManager.param
  local fromActFrontBreakSunday = param.fromActFrontBreakSunday
  if fromActFrontBreakSunday then
    return true
  end
  local stageId = param.levelId
  local titleType = tonumber(GetTableData(LuaEntry.Player:GetABTestTableName(TableName.LW_Stage_Feature), stageId, "battle_title_type"))
  if titleType ~= 1 then
    return false
  end
  return true
end

function UIParkourBattleMainView:ShowWinCondition(callback, hideMission)
  local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
  if logic.param.enterType == PVEEnterType.StageFeatureBuilding then
    return
  end
  if hideMission then
    self.winConditionArea:Set_localScale(1, 1, 1)
    self.levelNameText.transform:Set_localScale(1, 1, 1)
    return
  end
  if not self.missionBar then
    self.winConditionArea:Set_localScale(1, 1, 1)
    self.levelNameText.transform:Set_localScale(1, 1, 1)
    return
  end
  local data = DataCenter.LWBattleManager.logic.data
  if not data then
    self.winConditionArea:Set_localScale(1, 1, 1)
    self.levelNameText.transform:Set_localScale(1, 1, 1)
    return
  end
  if string.IsNullOrEmpty(data.target) then
    self.winConditionArea:Set_localScale(1, 1, 1)
    self.levelNameText.transform:Set_localScale(1, 1, 1)
    return
  end
  DataCenter.LWSoundManager:PlaySound(10015)
  self.missionBar:SetActive(true)
  self.missionBar.simpleAnimation:Play("Default")
  self.missionText:SetLocalText(data.title)
  local languageKey, keyParam = data:GetTargetDesData()
  if not string.IsNullOrEmpty(languageKey) then
    if table.IsNullOrEmpty(keyParam) or table.count(keyParam) == 0 then
      self.missionTarText:SetLocalText(languageKey)
    else
      self.missionTarText:SetLocalText(languageKey, table.unpack(keyParam))
    end
  end
  self.missionBar.simpleAnimation:Play("Default")
  self.showTimer = TimerManager:GetInstance():DelayInvoke(function()
    self.showTimer = nil
    self.missionBar.simpleAnimation:Play("Close")
    self.winConditionAreaTweener = self.winConditionArea:DOScale(Vector3.New(1, 1, 1), 0.3)
    self.winConditionAreaTweener:Delay(1)
    self.levelNameTextTweener = self.levelNameText.transform:DOScale(Vector3.New(1, 1, 1), 0.3)
    self.levelNameTextTweener:Delay(1)
    callback()
  end, 2.5)
end

function UIParkourBattleMainView:ShowCondition(title, content, delay)
  DataCenter.LWSoundManager:PlaySound(10015)
  self.missionBar:SetActive(true)
  self.missionBar.simpleAnimation:Play("Default")
  self.missionText:SetLocalText(title)
  self.missionTarText:SetLocalText(content)
  self.missionBar.simpleAnimation:Play("Default")
  self.showCondition = TimerManager:GetInstance():DelayInvoke(function()
    self.showCondition = nil
    self.missionBar.simpleAnimation:Play("Close")
    self.winConditionAreaTweener = self.winConditionArea:DOScale(Vector3.New(1, 1, 1), 0.3)
    self.winConditionAreaTweener:Delay(1)
    self.levelNameTextTweener = self.levelNameText.transform:DOScale(Vector3.New(1, 1, 1), 0.3)
    self.levelNameTextTweener:Delay(1)
  end, delay)
end

function UIParkourBattleMainView:ShowBossHpBar()
  self.winConditionArea.gameObject:SetActive(true)
end

function UIParkourBattleMainView:InitAllWinConditionBar()
  for winType, v in pairs(DataCenter.LWBattleManager.logic.winConditions) do
    self:UpdateWinConditionBar(winType)
  end
end

function UIParkourBattleMainView:UpdateWinConditionBar(winType)
  local battleMgr = DataCenter.LWBattleManager.logic
  local winCondition = DataCenter.LWBattleManager.logic:GetWinConditionDataByWinType(winType)
  if winCondition == nil then
    return
  end
  if winType == Const.ParkourWinType.KillTargetMonster then
    local needKillTarget = winCondition.needKillTarget
    local curValue = 0
    local needValue = 0
    for k, v in pairs(needKillTarget) do
      curValue = curValue + v.finish
      needValue = needValue + v.need
    end
    self:SetWinConditionBar(math.max(0, needValue - curValue) / needValue)
    self:SetWinConditionText(string.format("%d", math.max(0, needValue - curValue)), true)
    self:CheckIsHideSliderStyle(curValue >= needValue, winType)
  elseif winType == Const.ParkourWinType.KillMonster then
    local curValue = battleMgr.killNum
    local needValue = winCondition.needKillNum
    self:SetWinConditionBar(math.max(0, needValue - curValue) / needValue)
    self:SetWinConditionText(string.format("%d", math.max(0, needValue - curValue)), true)
    self:CheckIsHideSliderStyle(curValue >= needValue, winType)
  elseif winType == Const.ParkourWinType.SaveWorker then
    local curValue = battleMgr.saveNum
    local needValue = winCondition.needSaveNum
    self:SetWinConditionBar(curValue / needValue)
    self:SetWinConditionText(string.format("%d", math.min(curValue, needValue)), true)
    self:CheckIsHideSliderStyle(curValue >= needValue, winType)
  elseif winType == Const.ParkourWinType.FinishPoint then
    local finalPos = battleMgr.data.endLine
    local startPos = 0
    local curPos = battleMgr.team:GetPositionZ()
    self:SetWinConditionBar((curPos - startPos) / (finalPos - startPos))
    local remain = math.floor(finalPos - curPos)
    remain = remain < 0 and 0 or remain
    self:SetWinConditionText(string.format("%d m", remain))
    self:CheckIsHideSliderStyle(remain <= 0, winType)
  elseif winType == Const.ParkourWinType.Time then
    local curValue = battleMgr.useTime or 0
    local needValue = winCondition.needTime or 1
    self:SetWinConditionBar((needValue - curValue) / needValue)
    self:SetWinConditionText(UITimeManager:GetInstance():SecondToFmtStringWithoutHour(needValue - curValue))
    self:CheckIsHideSliderStyle(curValue >= needValue, winType)
  elseif (winType == Const.ParkourWinType.KillBoss or winType == Const.ParkourWinType.BlastStandingWaterBottle) and (0 < battleMgr.killBossNum or 0 < battleMgr.blastStandingWaterBottleNum) then
    for i = 1, table.count(self.winConditionIconListCell) do
      local cell = self.winConditionIconListCell[i]
      if cell then
        if winType == Const.ParkourWinType.KillBoss then
          cell:RefreshKillEffectShowState(Const.ParkourWinType.KillBoss, battleMgr.killBossNum)
        else
          cell:RefreshKillEffectShowState(Const.ParkourWinType.BlastStandingWaterBottle, battleMgr.blastStandingWaterBottleNum)
        end
      end
    end
  end
end

function UIParkourBattleMainView:SetWinConditionBar(percent)
  if not self.transform then
    return
  end
  self.winConditionSlider:SetValue(Mathf.Clamp(percent, 0, 1))
  if 1 <= percent then
    self.winConditionSliderEff:SetActive(true)
  end
end

function UIParkourBattleMainView:SetWinConditionText(txt, punch)
  if not self.transform then
    return
  end
  if self.winConditionText then
    self.winConditionText:SetText(txt)
    if punch then
      local now = UITimeManager:GetInstance():GetServerTime()
      if now > self.punchOK then
        self.punchOK = now + PUNCH_CD * 1000
        self.winConditionText.transform:DOKill()
        self.winConditionText.transform:Set_localScale(1, 1, 1)
        self.winConditionTextTweener = self.winConditionText.transform:DOPunchScale(Vector3.New(1, 1, 1), PUNCH_CD, 1, 0.4)
      end
    end
  end
end

function UIParkourBattleMainView:SetWinConditionBoss(needKillBossNum)
  self.winConditionIconView:RemoveComponents(ParkourWinIconItemRender)
  self.winConditionIconItemPrefab:GameObjectRecycleAll()
  self.winConditionIconListCell = {}
  local index = 0
  for winType, v in pairs(DataCenter.LWBattleManager.logic.winConditions) do
    local showSliderStyle = GetWinConditionShowSliderStyle(winType)
    if not showSliderStyle then
      local showCount = v.needKillNum and v.needKillNum or 0
      for j = 1, showCount do
        index = index + 1
        local go = self.winConditionIconItemPrefab:GameObjectSpawn(self.winConditionIconView.transform)
        go.name = "Icon" .. tostring(index)
        go:SetActive(true)
        local itemRender = self.winConditionIconView:AddComponent(ParkourWinIconItemRender, go.name)
        local icon = GetWinConditionIcon(winType)
        itemRender:InitData(icon, winType, j)
        self.winConditionIconListCell[index] = itemRender
      end
    end
  end
end

function UIParkourBattleMainView:CheckIsHideSliderStyle(finish, winType)
  if finish then
    local showSliderStyle = GetWinConditionShowSliderStyle(winType)
    if self.isMultipleWinCondition and showSliderStyle then
      self:SetWinConditionSliderStyleShowState(false)
      self.winConditionIconListHorLayout.childAlignment = CS.UnityEngine.TextAnchor.MiddleCenter
    end
  end
end

function UIParkourBattleMainView:SquadSuperArmorChange()
  local isSuperArmor = DataCenter.LWBattleManager.logic.team:IsSuperArmor()
  self.speedUpEffect:SetActive(isSuperArmor)
  if isSuperArmor then
    DataCenter.LWBattleManager:AutoZoom(40)
    local rate = Screen.height / Screen.width / (DefaultScreenHeight / DefaultScreenWidth)
    self.speedUpEffect.transform:Set_localScale(ResetScale.x, ResetScale.y * rate, ResetScale.z * rate)
  else
    DataCenter.LWBattleManager:AutoZoom(20)
  end
end

function UIParkourBattleMainView:OnParkourBattleWin()
  self.back_btn:SetActive(false)
  self.winBanner:SetActive(true)
  self.animator:Play("Eff_ui_beizengmen_mubiao_wancheng", 0, 0)
  DataCenter.LWSoundManager:PlaySound(10037)
  self:CreateDelayTimer(function()
    if self.winBanner then
      self.winBanner:SetActive(false)
    end
    if self.winConditionNode then
      self.winConditionNode:SetActive(false)
    end
    if self.levelNameText then
      self.levelNameText:SetActive(false)
    end
  end, 1.8)
end

function UIParkourBattleMainView:OnBossEnter()
  if self.bonusEnter then
    return
  end
  local time = Time.realtimeSinceStartup
  if time - self.lastBossNoticeTime < BossNoticeCD then
    return
  end
  self.lastBossNoticeTime = time
  self.bossNotice.gameObject:SetActive(false)
  self.bossNotice.gameObject:SetActive(true)
  self.bossNoticeText:SetLocalText(800328)
  DataCenter.LWSoundManager:PlaySound(10033)
end

function UIParkourBattleMainView:CreateDelayTimer(callback, delay, timerName)
  if self.delayTimers == nil then
    self.delayTimers = {}
  end
  timerName = timerName or "timer_" .. tostring(#self.delayTimers + 1)
  local timer = TimerManager:GetInstance():DelayInvoke(function()
    if self.delayTimers[timerName] then
      self.delayTimers[timerName] = nil
    end
    callback()
  end, delay)
  self.delayTimers[timerName] = timer
  return timer
end

function UIParkourBattleMainView:ClearAllDelayTimers()
  if self.delayTimers then
    for name, timer in pairs(self.delayTimers) do
      if timer then
        timer:Stop()
      end
    end
    self.delayTimers = {}
  end
end

function UIParkourBattleMainView:OnBonusEnter(param)
  self.bonusEnter = true
  self.winConditionNode:SetActive(false)
  if param and param.bonusType == Const.ParkourBattleBonusType.Dash then
    local dashBonus = self:GetParkourBonusDash()
    dashBonus:SetActive(true)
    dashBonus:SetData(param)
  else
    local parkourBonusPanel = self:GetParkourBonusPanel()
    parkourBonusPanel:SetActive(true)
    parkourBonusPanel:SetData(param)
  end
end

function UIParkourBattleMainView:OnBonusRefresh(param)
  local parkourBonusPanel = self:GetParkourBonusPanel()
  parkourBonusPanel:Refresh(param)
end

function UIParkourBattleMainView:OnBonusWinConditionRefresh(param)
  local parkourBonusPanel = self:GetParkourBonusPanel()
  parkourBonusPanel:RefreshConditions(param)
end

function UIParkourBattleMainView:GetParkourBonusPanel()
  if self.parkourBonusPanel == nil then
    local siblingIndex = self.winConditionArea.transform:GetSiblingIndex()
    self.parkourBonusPanel = self:LoadComponentAsync(ParkourBonusPanel, ParkourBonusPanelPrefabPath, self.safeArea.transform, function(view, go, comp)
      comp:SetSiblingIndex(siblingIndex + 1)
      comp:SetOffsetMinXY(0, 0)
      comp:SetOffsetMaxXY(0, 0)
    end)
  end
  return self.parkourBonusPanel
end

function UIParkourBattleMainView:GetParkourBonusDash()
  if self.bonusDash == nil then
    local bonusDashScriptPath = "UI/UIParkour/MainUI/Component/UIParkourBonusDash"
    local bonusDashPrefabPath = "Assets/Main/Prefabs/UI/ParkourBattle/UIParkourBonusDash.prefab"
    self.bonusDash = UIBaseComponent.LoadComponentAsync(self, bonusDashScriptPath, bonusDashPrefabPath, self.safeArea.transform, function(view, go, lua, callback_param)
      if self.bonusDash then
        self.bonusDash:SetActive(false)
      end
    end)
  end
  return self.bonusDash
end

function UIParkourBattleMainView:GetBossHpBar()
  if self.bossHpBar == nil then
    local barNum = 1
    local bossIcon
    local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
    if logic and logic.boss then
      local boss = logic.boss[1]
      if boss then
        barNum = boss.monsterMeta.hp_bar_num
        bossIcon = boss.monsterMeta.icon
      end
    end
    local bossHpBarScriptPath = "UI.UIParkour.MainUI.Component.ParkourBossHpBarComponent"
    local bossHpBarPrefabPath = "Assets/Main/Prefabs/UI/ParkourBattle/ParkourBossHpBar.prefab"
    self.bossHpBar = UIBaseComponent.LoadComponentAsync(self, bossHpBarScriptPath, bossHpBarPrefabPath, self.safeArea.transform, function(view, go, lua, callback_param)
      self.bossHpBar:SetAnchoredPositionXY(0, -300)
      self.bossHpBar:InitBossIcon(bossIcon)
    end)
    self.bossHpBar:InitBarNum(barNum)
  end
  return self.bossHpBar
end

function UIParkourBattleMainView:OnParkourBossHpViewChanged()
  local bossHpBar = self:GetBossHpBar()
  local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
  if logic and logic.boss then
    local boss = logic.boss[1]
    if boss then
      bossHpBar:SetHp(boss.curBlood, boss.maxBlood)
    end
  end
end

function UIParkourBattleMainView:OnParkourBossHpViewDead()
  local bossHpBar = self:GetBossHpBar()
  local logic = DataCenter.LWBattleManager:GetCurBattleLogic()
  if logic and logic.boss then
    local boss = logic.boss[1]
    if boss then
      bossHpBar:SetHp(boss.curBlood, boss.maxBlood)
    else
      bossHpBar:SetDead()
    end
  end
end

return UIParkourBattleMainView
