local FourteenDayGiftUI = class("FourteenDayGiftUI", BaseUI)

function FourteenDayGiftUI:isFullScreen()
    return false
end

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

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

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

function FourteenDayGiftUI:ctor()
end

function FourteenDayGiftUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.txTitle = uiMap["fourteen_day_gift_ui.content.img_title.tx_title"]
    self.btnClose = uiMap["fourteen_day_gift_ui.content.btn_close"]
    self.txCountdown = uiMap["fourteen_day_gift_ui.content.countdown.tx_countdown"]
    self.scrollrectComp = uiMap["fourteen_day_gift_ui.content.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_4))

    self.scrollrectComp:addInitCallback(function()
		return "app/ui/activity/fourteen_day/cell/fourteen_day_gift_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.giftList[index].id)
	end)

    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:bind(DataManager.FourteenDayData, "isDirty", function()
        self:onRefresh()
	end)
    self:bind(DataManager.ShopData, "isDirty", function()
        self:onRefresh()
	end)
end

function FourteenDayGiftUI:onRefresh()
    self.giftList = DataManager.FourteenDayData:getGiftIdsSort()
    self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#self.giftList)

    self:refreshCountdown()
end

-- 刷新倒计时
function FourteenDayGiftUI: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 FourteenDayGiftUI