local ActBossRushShopUI = class("ActBossRushShopUI", BaseUI)

local SHOP_CELL = "app/ui/activity/act_boss_rush/cell/shop_cell"

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

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

function ActBossRushShopUI:isFullScreen()
    return false
end

function ActBossRushShopUI:ctor()
    self:dealList()
end

function ActBossRushShopUI:dealList()
    self.shopList = {}
    self.costId = 1
    for _, id in ipairs(DataManager.ActBossRushData:getExchangeList()) do
        local canExchange = DataManager.ActBossRushData:getExchangeRemainCount(id) > 0
        table.insert(self.shopList, {id = id, canExchange = canExchange})
        if self.costId == 1 then
            local cost = DataManager.ActBossRushData:getShopCost(id)
            self.costId = GFunc.getRewardId(cost)
        end
    end

    table.sort(self.shopList, function(a, b)
        if a.canExchange == b.canExchange then
            return a.id < b.id
        else
            return a.canExchange
        end
    end)
end

function ActBossRushShopUI: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)

    DataManager.ActBossRushData:setTodayExchangeCheck()
end

function ActBossRushShopUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_shop_ui.bg.desc_bg.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_17))
    uiMap["act_boss_rush_shop_ui.bg.score.tx_score"]:setText(DataManager.BagData.ItemData:getItemNumById(self.costId))
    uiMap["act_boss_rush_shop_ui.bg.score.img"]:setSprite(ModuleManager.ItemManager:getItemIcon(self.costId))

    self:_refreshScrollrect()
end

function ActBossRushShopUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_boss_rush_shop_ui.bg.score"]:addClickListener(function()
        ModuleManager.ActBossRushManager:showFundUI()
        self:closeUI()
    end)

    uiMap["act_boss_rush_shop_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)
end

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

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

function ActBossRushShopUI:_refreshScrollrect()
    if self.scrollRectComp then
        self.scrollRectComp:updateAllCell()
        return
    end

    local uiMap = self.root:genAllChildren()
    self.scrollRectComp = uiMap["act_boss_rush_shop_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return SHOP_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.shopList[index].id)
	end)
    self.scrollRectComp:clearCells()
    self.scrollRectComp:refillCells(#self.shopList)
end

return ActBossRushShopUI