local FourteenDayExchangeUI = class("FourteenDayExchangeUI", BaseUI)

function FourteenDayExchangeUI:isFullScreen()
    return false
end

function FourteenDayExchangeUI:getPrefabPath()
    return "assets/prefabs/ui/activity/fourteen_day/fourteen_day_exchange_ui.prefab"
end

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

function FourteenDayExchangeUI:onClose()
    if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
end

function FourteenDayExchangeUI:ctor()
end

function FourteenDayExchangeUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.txTitle = uiMap["fourteen_day_exchange_ui.content.img_title.tx_title"]
    self.btnClose = uiMap["fourteen_day_exchange_ui.content.btn_close"]
    self.txCountdown = uiMap["fourteen_day_exchange_ui.content.countdown.tx_countdown"]
    self.txDesc = uiMap["fourteen_day_exchange_ui.content.tx_desc"]
    self.scrollrectComp = uiMap["fourteen_day_exchange_ui.content.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.txNum = uiMap["fourteen_day_exchange_ui.content.item.tx_num"]
    self.btnAdd = uiMap["fourteen_day_exchange_ui.content.item"]

    self.exchangeList = DataManager.FourteenDayData:getExchangeShowIdsSort()
    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_3))
    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_9))

    self.scrollrectComp:addInitCallback(function()
		return "app/ui/activity/fourteen_day/cell/fourteen_day_exchange_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.exchangeList[index].id)
	end)
    self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#self.exchangeList)

    self.btnAdd:addClickListener(function()
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_task_ui")
        self:closeUI()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:bind(DataManager.FourteenDayData, "isDirty", function()
        self:onRefresh()
	end)
end

function FourteenDayExchangeUI:onRefresh()
    DataManager.FourteenDayData:setTodayExchangeCheck()
    self.exchangeList = DataManager.FourteenDayData:getExchangeShowIdsSort()

    self.txNum:setText(DataManager.FourteenDayData:getExchangeItemNum())
    self.scrollrectComp:updateAllCell()
    self:refreshCountdown()
end

-- 刷新倒计时
function FourteenDayExchangeUI:refreshCountdown()
    if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
	self.countdownSid = self:scheduleGlobal(function()
        local time = DataManager.FourteenDayData:getEndRemainTime()
        if time < 0 then
            self:closeUI()
            return
        end
        self.txCountdown:setText(Time:formatNumTimeStr(time))
	end, 1)
    self.txCountdown:setText(Time:formatNumTimeStr(DataManager.FourteenDayData:getEndRemainTime()))
end

return FourteenDayExchangeUI