local MainCompBaseCell = require "app/ui/main_city/component/main_comp_base_cell"
local ArenaComp = class("ArenaComp", MainCompBaseCell)

local GIFT_CELL = "app/ui/main_city/cell/side_bar_arena_gift_cell"

function ArenaComp:getIsOpen()
    return DataManager.ArenaData:isOpen(true)
end

function ArenaComp:getEntranceName()
    return I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_1)
end

function ArenaComp:getFightDesc()
    return I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_2)
end

function ArenaComp:onClickFight()
    -- 赛季结算
    if DataManager.ArenaData:isInSeasonSettlementTime() then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_29))
        return 
    end
    UIManager:showUI("app/ui/arena/arena_match_ui")
end

function ArenaComp:getShowEntranceRedPoint()
    return DataManager.ArenaData:hasEntranceRedDot()
end

function ArenaComp:ctor()
end

function ArenaComp:onClose()
    if self.spineGrading then
		self.spineGrading:destroy()
		self.spineGrading = nil
	end
end

function ArenaComp:init()
    local uiMap = self:getBaseObject():genAllChildren()

    self.txSeason = uiMap["arena_comp.tx_season"]
    self.txTitle = uiMap["arena_comp.tx_title"]
    self.txCountdown = uiMap["arena_comp.countdown.tx_time"]
    self.txGarding = uiMap["arena_comp.tx_garding"]
    self.spineRoot = uiMap["arena_comp.spine_root"]
    self.imgProg = uiMap["arena_comp.prog.img_prog"]
    self.txProg = uiMap["arena_comp.prog.tx_prog"]
    self.btnHelp = uiMap["arena_comp.tx_season.btn_help"]
    self.btnRecord = uiMap["arena_comp.btn_record"]
    self.txRecord = uiMap["arena_comp.btn_record.tx_record"]
    self.btnFormation = uiMap["arena_comp.btn_formation"]
    self.txFormation = uiMap["arena_comp.btn_formation.tx_formation"]
    self.btnReward = uiMap["arena_comp.btn_reward"]
    self.txReward = uiMap["arena_comp.btn_reward.tx_reward"]
    self.btnRank = uiMap["arena_comp.btn_rank"]
    self.txRank = uiMap["arena_comp.btn_rank.tx_rank"]
    self.btnGradingReward = uiMap["arena_comp.btn_grading_reward"]
    self.txGradingReward = uiMap["arena_comp.btn_grading_reward.tx_grading_reward"]

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_4))
    self.txRecord:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_7))
    self.txFormation:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_9))
    self.txReward:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_41))
    self.txRank:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_5))
    self.txGradingReward:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_40))

    self.btnHelp:addClickListener(function()
        local params = {
            type = GConst.TipsConst.HELP_TIPS_TYPE.ARENA,
            title = I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_4),
            desc = I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_HELP),
        }
        ModuleManager.TipsManager:showHelpTips(params)
    end)
    self.btnRecord:addClickListener(function()
        UIManager:showUI("app/ui/arena/arena_recent_battle_ui")
    end)
    self.btnFormation:addClickListener(function()
        UIManager:showUI("app/ui/arena/arena_formation_ui", {type = GConst.BattleConst.FORMATION_TYPE.ARENA_DEFEND})
    end)
    self.btnReward:addClickListener(function()
        UIManager:showUI("app/ui/arena/arena_season_reward_ui")
    end)
    self.btnRank:addClickListener(function()
        UIManager:showUI("app/ui/arena/arena_rank_ui")
    end)
    self.btnGradingReward:addClickListener(function()
        UIManager:showUI("app/ui/arena/arena_grading_reward_ui")
    end)
    self:initBounty()
    self:refreshShow()
    self:initRightBtns()
end

function ArenaComp:initBounty()
    local uiMap = self:getBaseObject():genAllChildren()

    self.bountyNode = uiMap["arena_comp.bounty_node"]
	self.bountyBanner = uiMap["arena_comp.bounty_node.banner"]
    self.bountyBanner:addClickListener(function()
        ModuleManager.ArenaBountyManager:showBountyMainUI()
    end)
    self.bountyRewardSpine = uiMap["arena_comp.bounty_node.banner.spine"]
    self.bountyBannerInfo = uiMap["arena_comp.bounty_node.banner.info"]
    self.bountyBannerDescTx = uiMap["arena_comp.bounty_node.banner.desc"]
    self.bountyBannerSlider = uiMap["arena_comp.bounty_node.banner.info.progress_bg.slider"]
    self.bountyBannerSliderTx = uiMap["arena_comp.bounty_node.banner.info.progress_bg.slider_tx"]
    self.bountyBannerLvTx = uiMap["arena_comp.bounty_node.banner.info.progress_bg.lv_tx"]
    uiMap["arena_comp.bounty_node.banner.info.name"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_BATTLEPASS_1))
end

function ArenaComp:refreshBounty()
    if not DataManager.ArenaBountyData:getIsOpen() or GFunc.isShenhe() then
        self.bountyNode:setVisible(false)
        return
    end

    self.bountyNode:setVisible(true)
    if DataManager.ArenaBountyData:getIfCanClaimReward() then
        self.bountyRewardSpine:setVisible(true)
        self.bountyRewardSpine:playAnim("idle", true)
        self.bountyRewardSpine:getSkeletonGraphic().enabled = true
        self.bountyBannerInfo:setVisible(false)
        self.bountyBannerDescTx:setText(I18N:getGlobalText(I18N.GlobalConst.COLLET_REWARDS))
    else
        self.bountyRewardSpine:setVisible(false)
        self.bountyRewardSpine:getSkeletonGraphic().enabled = false
        local bannerName = DataManager.ArenaBountyData:getBannerName()
	    self.bountyBanner:setSprite(GConst.ATLAS_PATH.BOUNTY, bannerName)
        self.bountyBannerInfo:setVisible(true)
        self.bountyBannerDescTx:setText(GConst.EMPTY_STRING)
        local exp = DataManager.ArenaBountyData:getExp()
	    local lvUpExp = DataManager.ArenaBountyData:getLvUpExp()
	    local level = DataManager.ArenaBountyData:getLevel()
	    local maxLevel = DataManager.ArenaBountyData:getMaxLevel()
        self.bountyBannerSliderTx:setText(exp .. "/" .. lvUpExp)
	    self.bountyBannerSlider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = exp / lvUpExp
        if level >= maxLevel then
            self.bountyBannerLvTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_MAX))
        else
            self.bountyBannerLvTx:setText(tostring(level))
        end
    end
end

function ArenaComp:refreshShow()
    if not DataManager.ArenaData:isOpen(true) then
        return
    end

    local gradingId = DataManager.ArenaData:getGradingId()
    local totalScore = DataManager.ArenaData:getScore()
    local maxScore = DataManager.ArenaData:getGradingScoreTotal(totalScore)
    local curScore = DataManager.ArenaData:getGradingScore(totalScore)
    local gradingIconName = DataManager.ArenaData:getGradingIconName(gradingId)
    local gradingNumName = DataManager.ArenaData:getGradingNumName(gradingId)
    self.txSeason:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_3, DataManager.ArenaData:getSeason()))
    self.txGarding:setText(DataManager.ArenaData:getGradingName(gradingId))
    if maxScore then
        self.txProg:setText(curScore.."/"..maxScore)
        self.imgProg:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = curScore/maxScore
    else
        self.txProg:setText(curScore)
        self.imgProg:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = 1
    end

    if DataManager.ArenaData:hasSeasonReward() then
        self.btnReward:addRedPoint(30, 25, 0.6)
    else
        self.btnReward:removeRedPoint()
    end

    self.spineRoot:removeAllChildren()
    SpineManager:loadUISpineWidgetAsync(gradingIconName.."_spine", self.spineRoot, function(spineObject)
        self.spineGrading = spineObject
        self.spineGrading:setSkin(gradingNumName)
        self.spineGrading:playAnim("idle", true)
    end)

    self:refreshBounty() -- 刷新战令
    self:updateTimer()
    self:refreshGradingRewardRedPoint()
end

function ArenaComp:updateTimer()
    if not DataManager.ArenaData:isOpen(true) then
        return
    end

    if self.seasonSettlementSid then
        self:getBaseObject():unscheduleGlobal(self.seasonSettlementSid)
        self.seasonSettlementSid = nil
    end
    if self.seasonEndSid then
        self:getBaseObject():unscheduleGlobal(self.seasonEndSid)
        self.seasonEndSid = nil
    end

    if not DataManager.ArenaData:isInSeasonSettlementTime() then
        -- 结算计时器
        if EDITOR_MODE then
            Logger.logHighlight("赛季锁定倒计时："..DataManager.ArenaData:getRemainSeasonSettlementTime())
        end
        self.seasonSettlementSid = self:getBaseObject():performWithDelayGlobal(function()
            ModuleManager.ArenaManager:onSeasonSettlement()
        end, DataManager.ArenaData:getRemainSeasonSettlementTime())
    else
        -- 结束计时器
        if EDITOR_MODE then
            Logger.logHighlight("赛季结束倒计时："..DataManager.ArenaData:getRemainSeasonTime())
        end
        self.seasonEndSid = self:getBaseObject():performWithDelayGlobal(function()
            ModuleManager.ArenaManager:onSeasonChanged()
        end, DataManager.ArenaData:getRemainSeasonTime())
    end

    -- 显示倒计时
	if self.seasonChangeSid then
		self:getBaseObject():unscheduleGlobal(self.seasonChangeSid)
	end
    self.txCountdown:setText(Time:formatNumTimeStr(DataManager.ArenaData:getRemainSeasonTime()))
	self.seasonChangeSid = self:getBaseObject():scheduleGlobal(function()
        self.txCountdown:setText(Time:formatNumTimeStr(DataManager.ArenaData:getRemainSeasonTime()))
	end, 1)
end

function ArenaComp:refreshTime()
    self:refreshRightBtns()
end

function ArenaComp:initRightBtns()
    if self.rightBtnCells then
        return
    end
    local uiMap = self:getUIMap()
    self.rightArrow = uiMap["arena_comp.right_node.arrow_node"]
    self.rightArrow:setVisible(false)
    self.rightNode = uiMap["arena_comp.right_node"]
    self.rightBtnCells = {}
    self.rightBtnCells.giftCell = CellManager:addCellComp(uiMap["arena_comp.right_node.side_bar.side_bar_cell"], GIFT_CELL)
    self:refreshRightBtns()
end

function ArenaComp:refreshRightBtns()
    local y = 0
    if self.rightBtnCells.giftCell:checkIsOpen() then
        y = y - 60
        self.rightBtnCells.giftCell:setActive(true)
        self.rightBtnCells.giftCell:setVisible(true)
        self.rightBtnCells.giftCell:getBaseObject():setAnchoredPositionY(y)
        self.rightBtnCells.giftCell:refresh()
    else
        self.rightBtnCells.giftCell:setVisible(false)
    end

    -- 暂不处理箭头的问题
    self.rightNode:setVisible(y < 0)
end

-- 刷新段位奖励红点
function ArenaComp:refreshGradingRewardRedPoint()
    if DataManager.ArenaData:hasGradingRewardRedDot() then
        self.btnGradingReward:addRedPoint(50, 50, 0.6)
    else
        self.btnGradingReward:removeRedPoint()
    end
end

return ArenaComp