local ActSprintListUI = class("ActSprintListUI", BaseUI)
local ACTIVITY_CELL = "app/ui/activity/act_sprint/cell/activity_cell"

function ActSprintListUI:isFullScreen()
    return false
end
function ActSprintListUI:onPressBackspace()
    self:closeUI()
end

function ActSprintListUI:ctor(params)
    self.params = params or {}
    self.autoPop = self.params.autoPop
    self.showToday = self.params.showToday
    self.activityList = DataManager.ActSprintData:getActSprintIdList()
end

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

-- function ActSprintListUI: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 ActSprintListUI:onLoadRootComplete()
    if self.autoPop and self.showToday then -- 今天内不在提示
		local time = LocalData:getMessageBoxShowTodayTime(self.showToday)
		local todayTime = Time:getBeginningOfServerToday()
		if time > 0 and time == todayTime then
            self:closeUI()
			return
        else
            LocalData:setMessageBoxShowTodayTime(self.showToday, todayTime)
		end
	end

    self:_display()
    self:_addListeners()
    self:_bind()

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

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

    self.mask = uiMap["act_sprint_list_ui.mask"]
    self.closeBtn = uiMap["act_sprint_list_ui.bg.close_btn"]
    self.titleTx = uiMap["act_sprint_list_ui.bg.title_tx"]
    self.scrollrect = uiMap["act_sprint_list_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.shopBtn = uiMap["act_sprint_list_ui.bg.shop_btn"]
    self.shopTx = uiMap["act_sprint_list_ui.bg.shop_btn.tx"]

    self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_1))
    self.shopTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_GOGOGO_DESC_3))

    -- 屏蔽兑换
    self.shopBtn:setActive(DataManager.ActSprintData:isShopOpen())
end

function ActSprintListUI:_addListeners()
    -- self.mask:addClickListener(function()
    --     self:closeUI()
    -- end)
    self.closeBtn:addClickListener(function()
        self:closeUI()
    end)
    
    self.shopBtn:addClickListener(function()
        ModuleManager.ActSprintManager:showActSprintShopUI()
    end)

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

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

function ActSprintListUI:updateTime()
    self:updateScrollrect()

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

function ActSprintListUI:updateScrollrect()
    self.activityList = DataManager.ActSprintData:getActSprintIdList()

    if not self.initScrollrect then
        self.initScrollrect = true
        self.scrollrect:addInitCallback(function()
            return ACTIVITY_CELL
        end)
        self.scrollrect:addRefreshCallback(function(index, cell)
            cell:refresh(self.activityList[index], function() self:closeUI() end)
        end)
        self.scrollrect:clearCells()
        self.scrollrect:refillCells(#self.activityList)
    else
        if self.scrollrect:getTotalCount() == #self.activityList then
            self.scrollrect:updateAllCell()
        else
            self.scrollrect:refillCells(#self.activityList)
        end
    end
end

function ActSprintListUI:updateRp()
    if DataManager.ActSprintData:hasExchangeRp() then
        self.shopBtn:addRedPoint(40, 30, 0.6)
    else
        self.shopBtn:removeRedPoint()
    end
end

return ActSprintListUI