local ActSprintShopUI = class("ActSprintShopUI", BaseUI)
local CELL = "app/ui/activity/act_sprint/cell/shop_cell"

function ActSprintShopUI:isFullScreen()
    return false
end

function ActSprintShopUI:onPressBackspace()
    self:onClickClose()
end

function ActSprintShopUI:ctor(params)
    self.params = params or {}
end

function ActSprintShopUI:getPrefabPath()
    return "assets/prefabs/ui/activity/act_sprint/act_sprint_shop_ui.prefab"
end

-- function ActSprintShopUI:currencyParams()
-- 	local params = {}
-- 	params.showType = GConst.CURRENCY_TYPE.HORIZONTAL
-- 	params.itemIds = {}
--     -- 填充活动对应的兑换道具 约定同时开的不会超过4个
--     local itemMap = {}
--     local exchangeIdList = DataManager.ActSprintData:getExchangeIdList()
--     for index, exchangeId in ipairs(exchangeIdList) do
--         local exchangeCfg = DataManager.ActSprintData:getExchangeConfig(exchangeId)
--         if exchangeCfg.cost then
--             itemMap[exchangeCfg.cost.id] = true
--         end
--     end
--     for itemId, _ in pairs(itemMap) do
--         table.insert(params.itemIds, itemId)
--     end
-- 	return params, true
-- end

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

	self:scheduleGlobal(function()
		self:updateTime()
	end, 1)
    self:updateTime()
    self:updateScrollrect()
end

function ActSprintShopUI:_display()
    local uiMap = self.root:genAllChildren()

    self.mask = uiMap["act_sprint_shop_ui.mask"]
    self.titleTx = uiMap["act_sprint_shop_ui.bg.tx_title"]
    self.scrollrect = uiMap["act_sprint_shop_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.closeBtn = uiMap["act_sprint_shop_ui.bg.btn_close"]
    
    self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_3))
end

function ActSprintShopUI:_addListeners()
    self.closeBtn:addClickListener(function()
        self:closeUI()
    end)
end

function ActSprintShopUI:_bind()
    self:bind(DataManager.ActSprintData, "isDirty", function()
		self:updateScrollrect()
        -- self:updateCurrencyBar()
	end)
end

function ActSprintShopUI:updateTime()
	if not DataManager.ActSprintData:getIsOpen() then
		self:closeUI()
	end
end

function ActSprintShopUI:updateData()
    self.shopList = DataManager.ActSprintData:getExchangeIdList()
end

function ActSprintShopUI:updateScrollrect()
    self:updateData()
    
    if not self.initScrollrect then
        self.initScrollrect = true
        self.scrollrect:addInitCallback(function()
            return CELL
        end)
        self.scrollrect:addRefreshCallback(function(index, cell)
            cell:refresh(self.shopList[index])
        end)
        self.scrollrect:clearCells()
        self.scrollrect:refillCells(#self.shopList)
    else
        if self.scrollrect:getTotalCount() == #self.shopList then
            self.scrollrect:updateAllCell()
        else
            self.scrollrect:refillCells(#self.shopList)
        end
    end
end

return ActSprintShopUI