local TournWaveUI = class("TournWaveUI", BaseUI)

local COMP_BOUNTY = "app/ui/activity/tourn_wave/tourn_wave_bounty_comp"
local COMP_RANK = "app/ui/activity/tourn_wave/tourn_wave_rank_comp"
local COMP_GIFT = "app/ui/activity/tourn_wave/tourn_wave_gift_comp"

function TournWaveUI:isFullScreen()
    return false
end

function TournWaveUI:getPrefabPath()
    return "assets/prefabs/ui/activity/tourn_wave/tourn_wave_ui.prefab"
end

function TournWaveUI:onPressBackspace()
	self:closeUI()
end

function TournWaveUI:onClose()
    if self.compRank ~= nil then
        self.compRank:refresh()
    end
    if self.countdownSid then
		self.txCountdown:unscheduleGlobal(self.countdownSid)
	end
end

function TournWaveUI:ctor(param)
    if param then
        self.panelType = param
    else
        self.panelType = GConst.TournWaveConst.PANEL_TYPE.BOUNTY -- 默认进入任务页签
    end
end

function TournWaveUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()

    -- 通用
    self.txTitle = uiMap["tourn_wave_ui.common.img_title.tx_title"]
    self.btnClose = uiMap["tourn_wave_ui.common.btn_close"]
    self.bg = uiMap["tourn_wave_ui.common.bg"]
    self.countdown = uiMap["tourn_wave_ui.common.countdown"]
    self.txCountdown = uiMap["tourn_wave_ui.common.countdown.tx_countdown"]
    -- 子界面
    self.bountyPanel = uiMap["tourn_wave_ui.bounty_panel"]
    self.rankPanel = uiMap["tourn_wave_ui.rank_panel"]
    self.giftPanel = uiMap["tourn_wave_ui.gift_panel"]
    -- 战令标签
    self.btnBounty = uiMap["tourn_wave_ui.common.btns.btn_bounty"]
    self.txBounty1 = uiMap["tourn_wave_ui.common.btns.btn_bounty.tx_btn"]
    self.txBounty2 = uiMap["tourn_wave_ui.common.btns.btn_bounty.select.tx_select"]
    self.selectBounty = uiMap["tourn_wave_ui.common.btns.btn_bounty.select"]
    -- 排行榜标签
    self.btnRank = uiMap["tourn_wave_ui.common.btns.btn_rank"]
    self.txRank1 = uiMap["tourn_wave_ui.common.btns.btn_rank.tx_btn"]
    self.txRank2 = uiMap["tourn_wave_ui.common.btns.btn_rank.select.tx_select"]
    self.selectRank = uiMap["tourn_wave_ui.common.btns.btn_rank.select"]
    -- 礼包标签
    self.btnGift = uiMap["tourn_wave_ui.common.btns.btn_gift"]
    self.txGift1 = uiMap["tourn_wave_ui.common.btns.btn_gift.tx_btn"]
    self.txGift2 = uiMap["tourn_wave_ui.common.btns.btn_gift.select.tx_select"]
    self.selectGift = uiMap["tourn_wave_ui.common.btns.btn_gift.select"]

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_TOURN_WAVE_TITLE))
    self.txBounty1:setText(I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
    self.txBounty2:setText(I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
    self.txRank1:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_5))
    self.txRank2:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_5))
    self.txGift1:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_TOURN_WAVE_DESC_4))
    self.txGift2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_TOURN_WAVE_DESC_4))

    self.btnBounty:addClickListener(function()
        self.panelType = GConst.TournWaveConst.PANEL_TYPE.BOUNTY
        self:onRefresh()
    end)
    self.btnRank:addClickListener(function()
        self.panelType = GConst.TournWaveConst.PANEL_TYPE.RANK
        self:onRefresh()
    end)
    self.btnGift:addClickListener(function()
        self.panelType = GConst.TournWaveConst.PANEL_TYPE.GIFT
        self:onRefresh()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.ACTIVITY_TOURN_WAVE_END, function()
        self:closeUI()
    end)
    self:bind(DataManager.TournWaveData, "isDirty", function()
        -- 检查更新数据
        if DataManager.TournWaveData:getIsInReset() and self.panelType == GConst.TournWaveConst.PANEL_TYPE.RANK then
            ModuleManager.TournWaveManager:reqActData()
            self:onRefresh()
        else
            self:refreshRedPoint()
        end
	end)
end

function TournWaveUI:onRefresh()
    -- 倒计时
	if self.countdownSid then
		self.txCountdown:unscheduleGlobal(self.countdownSid)
	end
    self.txCountdown:setText(Time:formatNumTimeStr(DataManager.TournWaveData:getEndRemainTime()))
	self.countdownSid = self.txCountdown:scheduleGlobal(function()
        local time = DataManager.TournWaveData:getEndRemainTime()
        if time >= 0 then
            self.txCountdown:setText(Time:formatNumTimeStr(time))
        end
	end, 1)

    if self.panelType == GConst.TournWaveConst.PANEL_TYPE.BOUNTY then
        self:showBountyInfo()
    elseif self.panelType == GConst.TournWaveConst.PANEL_TYPE.RANK then
        self:showRankInfo()
    elseif self.panelType == GConst.TournWaveConst.PANEL_TYPE.GIFT then
        self:showGiftInfo()
    end
    self:refreshRedPoint()
end

-- 刷新标签红点
function TournWaveUI:refreshRedPoint()
    if DataManager.TournWaveData:hasBountyRedPoint() then
        self.btnBounty:addRedPoint(-75, -5, 0.6)
    else
        self.btnBounty:removeRedPoint()
    end
    if DataManager.TournWaveData:hasRankRedPoint() then
        self.btnRank:addRedPoint(-75, -5, 0.6)
    else
        self.btnRank:removeRedPoint()
    end
    if DataManager.TournWaveData:hasGiftRedPoint() then
        self.btnGift:addRedPoint(-75, -5, 0.6)
    else
        self.btnGift:removeRedPoint()
    end
end

function TournWaveUI:showBountyInfo()
    self.bountyPanel:setActive(true)
    self.selectBounty:setActive(true)
    self.rankPanel:setActive(false)
    self.selectRank:setActive(false)
    self.giftPanel:setActive(false)
    self.selectGift:setActive(false)
    self.countdown:setActive(true)
    self.bg:setActive(true)

    if not self.compBounty then
        self.bountyPanel:initPrefabHelper()
        self.bountyPanel:genAllChildren()
        self.compBounty = self.bountyPanel:addLuaComponent(COMP_BOUNTY)
    end

    self.compBounty:refresh()
end

function TournWaveUI:showRankInfo()
    self.bountyPanel:setActive(false)
    self.selectBounty:setActive(false)
    self.rankPanel:setActive(true)
    self.selectRank:setActive(true)
    self.giftPanel:setActive(false)
    self.selectGift:setActive(false)
    self.countdown:setActive(false)
    self.bg:setActive(false)

    local curStep = DataManager.TournWaveData:getCurRankStep()
    ModuleManager.TournWaveManager:reqRankList(curStep > 0 and curStep or DataManager.TournWaveData:getTotalTurnCount())
    if not self.compRank then
        self.rankPanel:initPrefabHelper()
        self.rankPanel:genAllChildren()
        self.compRank = self.rankPanel:addLuaComponent(COMP_RANK)
    end

    self.compRank:refresh()
end

function TournWaveUI:showGiftInfo()
    self.bountyPanel:setActive(false)
    self.selectBounty:setActive(false)
    self.rankPanel:setActive(false)
    self.selectRank:setActive(false)
    self.giftPanel:setActive(true)
    self.selectGift:setActive(true)
    self.countdown:setActive(true)
    self.bg:setActive(false)

    if not self.compGift then
        self.giftPanel:initPrefabHelper()
        self.giftPanel:genAllChildren()
        self.compGift = self.giftPanel:addLuaComponent(COMP_GIFT)
    end

    self.compGift:refresh()
end

return TournWaveUI