local ActBossRushFundUI = class("ActBossRushFundUI", BaseUI)

local FUND_CELL = "app/ui/activity/act_boss_rush/cell/fund_cell"
local BTN_ICON = {"common_btn_green_3", "act_bossrush_board_1"}

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

function ActBossRushFundUI:isFullScreen()
    return false
end

function ActBossRushFundUI:ctor()
    self.rewardList = {}
    for _, id in pairs(DataManager.ActBossRushData:getBountyList()) do
        table.insert(self.rewardList, id)
    end
    table.sort(self.rewardList, function(a, b)
        return a < b
    end)
end

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

function ActBossRushFundUI: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 ActBossRushFundUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_fund_ui.bg.bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_15))
    uiMap["act_boss_rush_fund_ui.bg.level.tx_level"]:setText(DataManager.ActBossRushData:getWaveCount())
    uiMap["act_boss_rush_fund_ui.bg.scrollrect.viewport.content.mask_img.line.btn_buy_level.tx_num"]:setText(GFunc.getRewardNum(DataManager.ActBossRushData:getBuyBountyLevelCost()))
    self.downDesc = uiMap["act_boss_rush_fund_ui.bg.bg.desc"]
    self.downDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_16))

    self:refreshBtns()
    self:_refreshScrollrect()
end

function ActBossRushFundUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_fund_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["act_boss_rush_fund_ui.bg.scrollrect.viewport.content.mask_img.line.btn_buy_level"]:addClickListener(function()
        ModuleManager.ActBossRushManager:reqBuyFundLevel()
    end)
end

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

function ActBossRushFundUI:refreshBtns()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_fund_ui.bg.free.tx_free"]:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE))

    local probtn = uiMap["act_boss_rush_fund_ui.bg.buy1"]
    local proBtntx = uiMap["act_boss_rush_fund_ui.bg.buy1.tx_buyed"]
    local proBuy = uiMap["act_boss_rush_fund_ui.bg.buy1.tx_buy"]
    proBtntx:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
    proBuy:setText(DataManager.ActBossRushData:getProPrice())

    if DataManager.ActBossRushData:getProBought() then
        probtn:setSprite(GConst.ATLAS_PATH.ACT_BOSS_RUSH, BTN_ICON[2])
        probtn:setTouchEnable(false)
        proBtntx:setVisible(true)
        proBuy:setVisible(false)
    else
        probtn:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
        probtn:setTouchEnable(true)
        probtn:addClickListener(function()
            ModuleManager.ActBossRushManager:purcharse(DataManager.ActBossRushData:getProActId())
        end)
        proBtntx:setVisible(false)
        proBuy:setVisible(true)
    end

    local utralbtn = uiMap["act_boss_rush_fund_ui.bg.buy2"]
    local utralBtntx = uiMap["act_boss_rush_fund_ui.bg.buy2.tx_buyed"]
    local utralBuy = uiMap["act_boss_rush_fund_ui.bg.buy2.tx_buy"]
    utralBtntx:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
    utralBuy:setText(DataManager.ActBossRushData:gettUtralPrice())

    if DataManager.ActBossRushData:getUtralBought() then
        utralbtn:setSprite(GConst.ATLAS_PATH.ACT_BOSS_RUSH, BTN_ICON[2])
        utralbtn:setTouchEnable(false)
        utralBtntx:setVisible(true)
        utralBuy:setVisible(false)
    else
        utralbtn:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
        utralbtn:setTouchEnable(true)
        utralbtn:addClickListener(function()
            ModuleManager.ActBossRushManager:purcharse(DataManager.ActBossRushData:getUtralActId())
        end)
        utralBtntx:setVisible(false)
        utralBuy:setVisible(true)
    end
end

function ActBossRushFundUI:_refreshScrollrect()
    local uiMap = self.root:genAllChildren()
    local mask = uiMap["act_boss_rush_fund_ui.bg.scrollrect.viewport.content.mask_img"]
    local level = DataManager.ActBossRushData:getWaveLevel() - DataManager.ActBossRushData:getStartWaveLevel()
    local totalCount = #self.rewardList
    local maskCount = math.max(0, totalCount - level)
    mask:setAnchoredPositionY(-10 - level * 120)
    mask:setSizeDeltaY(maskCount * 120 + 20)
    local progSlider = uiMap["act_boss_rush_fund_ui.bg.scrollrect.viewport.content.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    local y = (totalCount - 1) * 120
    if (level - 1) >= 0 then
        progSlider.value = (level - 1) / (totalCount - 1)
    else
        progSlider.value = 0
    end
    mask:setVisible(level < totalCount)

    if self.scrollRectComp then
        self.scrollRectComp:updateAllCell()
        return
    end

    local prog = uiMap["act_boss_rush_fund_ui.bg.scrollrect.viewport.content.prog"]
    prog:setSizeDeltaY(y)
    self.scrollRectComp = uiMap["act_boss_rush_fund_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return FUND_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.rewardList[index])
        mask:getTransform():SetAsLastSibling()
	end)
    self.scrollRectComp:clearCells()
    self.scrollRectComp:setTotalCount(0)
    if level < 1 then
        level = 1
    end
    self.scrollRectComp:refillCells(totalCount, nil, level)
end

function ActBossRushFundUI: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_fund_ui.bg.countdown.tx_countdown"]:setText(str)

    uiMap["act_boss_rush_fund_ui.bg.scrollrect.viewport.content.mask_img.line.btn_buy_level"]:setActive(remainTime < 86400)
end

return ActBossRushFundUI