local FourteenDayTaskUI = class("FourteenDayTaskUI", BaseUI)

function FourteenDayTaskUI:isFullScreen()
    return false
end

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

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

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

function FourteenDayTaskUI:ctor()
    self.curStage = DataManager.FourteenDayData:getTaskShowStage()
end

function FourteenDayTaskUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.txTitle = uiMap["fourteen_day_task_ui.content.img_title.tx_title"]
    self.txCountdown = uiMap["fourteen_day_task_ui.content.countdown.tx_countdown"]
    self.txDesc = uiMap["fourteen_day_task_ui.content.stage.tx_desc"]
    self.rewardsNode = uiMap["fourteen_day_task_ui.content.stage.rewards"]
    self.rewards = {}
    for i = 1, 4 do
        table.insert(self.rewards, uiMap["fourteen_day_task_ui.content.stage.rewards.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
    end
    self.btnRight = uiMap["fourteen_day_task_ui.content.stage.select.btn_right"]
    self.btnLeft = uiMap["fourteen_day_task_ui.content.stage.select.btn_left"]
    self.txStage = uiMap["fourteen_day_task_ui.content.stage.select.tx_stage"]
    self.txSelect = uiMap["fourteen_day_task_ui.content.stage.select.tx_select"]
    self.scrollrectComp = uiMap["fourteen_day_task_ui.content.list_task"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.btnClose = uiMap["fourteen_day_task_ui.content.btn_close"]

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_2))
    self.txStage:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_6))

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

    self.btnRight:addClickListener(function()
        if self.curStage + 1 > DataManager.FourteenDayData:getActiveDays() then
            GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_7))
            return
        end
        self.curStage = self.curStage + 1
        self:onRefresh()
    end)
    self.btnLeft:addClickListener(function()
        self.curStage = self.curStage - 1
        self:onRefresh()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:bind(DataManager.FourteenDayData, "isDirty", function()
        self:onRefresh()
	end)
end

function FourteenDayTaskUI:onRefresh()
    self.taskStage = DataManager.FourteenDayData:getStageTaskId(self.curStage)
    self.taskList = DataManager.FourteenDayData:getStageSubTaskIdsSort(self.curStage)
    self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#self.taskList)

    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_8))
    self.txSelect:setText(self.curStage .. "/" .. DataManager.FourteenDayData:getTaskMaxStage())
    self.btnRight:setActive(self.curStage < DataManager.FourteenDayData:getTaskMaxStage())
    self.btnLeft:setActive(self.curStage > 1)

    self:refreshStageRewards()
    self:refreshCountdown()
end

-- 刷新阶段奖励显示
function FourteenDayTaskUI:refreshStageRewards()
    local stageRewards = DataManager.FourteenDayData:getTaskRewards(self.taskStage)
    local canGet = DataManager.FourteenDayData:canGetTaskReward(self.taskStage)
    local isReceived = DataManager.FourteenDayData:isReceivedTaskReward(self.taskStage)
    for idx, reward in ipairs(self.rewards) do
        reward:hideFrameAnimation()
        if stageRewards[idx] then
            reward:setVisible(true)
            reward:refreshByConfig(stageRewards[idx], isReceived, isReceived)
            if canGet then
                reward:addClickListener(function()
                    ModuleManager.FourteenDayManager:reqTaskReward(self.taskStage)
                end)
                reward:showFrameAnimation()
            else
                reward:addClickListener(nil)
            end
        else
            reward:setVisible(false)
        end
    end
end

-- 刷新倒计时
function FourteenDayTaskUI:refreshCountdown()
    if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
	self.countdownSid = self:scheduleGlobal(function()
        self.txCountdown:setText(Time:formatNumTimeStr(DataManager.FourteenDayData:getEndRemainTime()))
	end, 1)
    self.txCountdown:setText(Time:formatNumTimeStr(DataManager.FourteenDayData:getEndRemainTime()))
end

return FourteenDayTaskUI