local ActBossRushUI = class("ActBossRushUI", BaseUI)

local RANK_CELL = "app/ui/activity/act_boss_rush/cell/rank_cell"

function ActBossRushUI:getPrefabPath()
    return "assets/prefabs/ui/activity/act_boss_rush/act_boss_rush_ui.prefab"
end

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

function ActBossRushUI:getCurrencyParams()
    if self.currencyParams == nil then
        self.currencyParams = {
            itemIds = {
                GConst.ItemConst.ITEM_ID_GEM,
            },
            showType = GConst.CURRENCY_TYPE.HORIZONTAL
        }
    end
 	return self.currencyParams
end

function ActBossRushUI:ctor()

end

function ActBossRushUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()

    self:refreshTime()
    if self.actCountdownSid then
        self:unscheduleGlobal(self.actCountdownSid)
    end
    self.actCountdownSid = self:scheduleGlobal(function()
        self:refreshTime()
	end, 1)
end

function ActBossRushUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_ui.bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_2))
    uiMap["act_boss_rush_ui.bg.tx_today_rank"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_3))
    uiMap["act_boss_rush_ui.down.formation_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_4))
    uiMap["act_boss_rush_ui.bg.btn_rank.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_10))
    uiMap["act_boss_rush_ui.bg.btn_bounty.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_11))
    uiMap["act_boss_rush_ui.bg.btn_shop.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_12))
    uiMap["act_boss_rush_ui.bg.fight_btn.tx_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE))

    local todayMaxWaveDesc = uiMap["act_boss_rush_ui.bg.tx_today_max_wave_desc"]
    local todayMaxWave = uiMap["act_boss_rush_ui.bg.tx_today_max_wave"]
    local todayMaxWaveImg = uiMap["act_boss_rush_ui.bg.img_today_max_wave"]
    todayMaxWaveDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_4))
    todayMaxWave:setText(DataManager.ActBossRushData:getTodayMaxWave())
    GFunc.centerTxAndImgAndTx(todayMaxWaveDesc, 5, todayMaxWaveImg, 5, todayMaxWave)

    uiMap["act_boss_rush_ui.bg.tx_today_fight_count"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_5, DataManager.ActBossRushData:getTodayFightCount()))

    local cost = uiMap["act_boss_rush_ui.bg.fight_btn.cost"]
    local costIcon = uiMap["act_boss_rush_ui.bg.cost.icon"]
    local costDesc = uiMap["act_boss_rush_ui.bg.cost.tx_cost_desc"]
    local costNum = uiMap["act_boss_rush_ui.bg.cost.tx_cost"]

    local todayFreeFightCount = uiMap["act_boss_rush_ui.bg.tx_count"]
    local count = DataManager.ActBossRushData:getFreeFightCount()
    if count > 0 then
        todayFreeFightCount:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_6, count))
        cost:setVisible(false)
    else
        todayFreeFightCount:setText(GConst.EMPTY_STRING)
        cost:setVisible(true)
        costDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_7))
        costNum:setText(DataManager.ActBossRushData:getFightCost())
        GFunc.centerTxAndImgAndTx(costDesc, 5, costIcon, 5, costNum)
    end

    local rankBtn =  uiMap["act_boss_rush_ui.bg.btn_rank"]
    if DataManager.ActBossRushData:canGetRankReward() then
        rankBtn:addRedPoint(20, 40, 0.8)
    else
        rankBtn:removeRedPoint()
    end

    local bountyBtn =  uiMap["act_boss_rush_ui.bg.btn_bounty"]
    if DataManager.ActBossRushData:getHaveReward() then
        bountyBtn:addRedPoint(20, 40, 0.8)
    else
        bountyBtn:removeRedPoint()
    end

    local shopBtn =  uiMap["act_boss_rush_ui.bg.btn_shop"]
    if not DataManager.ActBossRushData:getTodayExchangeCheck() then
        shopBtn:addRedPoint(20, 40, 0.8)
    else
        shopBtn:removeRedPoint()
    end

    self:refreshRankList()
    self:refreshFormation()
end

function ActBossRushUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_ui.bg.fight_btn"]:addClickListener(function()
        ModuleManager.ActBossRushManager:reqFight()
    end)

    uiMap["act_boss_rush_ui.down.formation_btn"]:addClickListener(function()
        if not DataManager.ActBossRushData:getIsOpen() then
            return
        end
        ModuleManager.CommonManager:showFormationUI(GConst.BattleConst.FORMATION_TYPE.BOSS_RUSH)
    end)

    uiMap["act_boss_rush_ui.down.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["act_boss_rush_ui.bg.btn_rank"]:addClickListener(function()
        ModuleManager.ActBossRushManager:showRankUI()
    end)

    uiMap["act_boss_rush_ui.bg.btn_bounty"]:addClickListener(function()
        ModuleManager.ActBossRushManager:showFundUI()
    end)

    uiMap["act_boss_rush_ui.bg.btn_shop"]:addClickListener(function()
        ModuleManager.ActBossRushManager:showShopUI()
    end)

    uiMap["act_boss_rush_ui.bg.time_bg.btn_help"]:addClickListener(function()
        local params = {
            type = GConst.TipsConst.HELP_TIPS_TYPE.NORMAL,
            title = I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_11),
            desc = I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_31),
        }
        ModuleManager.TipsManager:showHelpTips(params)
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.GO_SHOP, function()
        self:closeUI()
    end)
end

function ActBossRushUI:_bind()
    self:bind(DataManager.ActBossRushData, "isDirty", function()
        self:_display()
    end)

    self:bind(DataManager.FormationData, "dirty", function()
        self:refreshFormation()
    end)

    self:bind(DataManager.HeroData, "isDirty", function()
        self:refreshFormation()
    end)
end

function ActBossRushUI:refreshRankList()
    if not self.rankCells then
        self.rankCells = {}
        local uiMap = self.root:genAllChildren()
        for i = 1, 3 do
            self.rankCells[i] = CellManager:addCellComp(uiMap["act_boss_rush_ui.bg.act_rank_cell_" .. i], RANK_CELL)
        end
    end

    local list = DataManager.ActBossRushData:getTopRankInfo()
    for index, cell in ipairs(self.rankCells) do
        cell:refresh(index, list[index])
    end
end

function ActBossRushUI:refreshTime()
    local uiMap = self.root:genAllChildren()
    local remainTime = DataManager.ActBossRushData:getRemainTime()
    local str
    if remainTime <= 0 then
        str = I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_EDSC)
    else
        str = Time:formatNumTimeStr(remainTime)
    end
    uiMap["act_boss_rush_ui.bg.time_bg.time"]:setText(str)

    local canFight = remainTime > 86400
    if self.canFight ~= canFight then
        self.canFight = canFight
        uiMap["act_boss_rush_ui.bg.fight_btn"]:setActive(canFight)
        uiMap["act_boss_rush_ui.bg.fight_btn.cost"]:setActive(canFight)
        uiMap["act_boss_rush_ui.bg.tx_count"]:setActive(canFight)
    end
end

function ActBossRushUI:refreshFormation()
    if not self.formationComp then
        local uiMap = self.root:genAllChildren()
        self.formationComp = uiMap["act_boss_rush_ui.bg.hero_formation_comp"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_FORMATION_COMP)
    end
    self.formationComp:refreshByFormation(DataManager.FormationData:getBossRushFormation())
end

return ActBossRushUI