local TaskComp = class("TaskComp", LuaComponent)

local TASK_CELL = "app/ui/activity/act_seven_day/cell/task_cell"

function TaskComp:refresh(parentUI, day, period)
    local uiMap = self:getUIMap()
    if not self.rewardCells then
        self.rewardCells = {}
        self.nums = {}
        self.boxIcons = {}
        self.effects = {}
        for i = 1, 7 do
            self.rewardCells[i] = uiMap["task_comp.bg.slider_bg.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
            self.nums[i] = uiMap["task_comp.slider_bg.num_" .. i]
            self.boxIcons[i] = uiMap["task_comp.slider_bg.box_" .. i]
            self.effects[i] = uiMap["task_comp.bg.slider_bg.effect.vfx_ui_b9_daojv_b01_" .. i]
        end
    end

    local curPoint = DataManager.ActSevenDayData:getScore(period)
    local maxPoint = curPoint

    for id, obj in ipairs(self.nums) do
        local scoreId = id
        if period == 2 then
            scoreId = scoreId + 7
        end
        local curCfg = DataManager.ActSevenDayData:getScoreCfg(scoreId)
        if curCfg then
            self.nums[id]:setText(curCfg.num)
            if curCfg.num > maxPoint then
                maxPoint = curCfg.num
            end
            local reward = curCfg.reward[1]
            local canGet = DataManager.ActSevenDayData:canClaimScore(scoreId, period)
            local isReceived = DataManager.ActSevenDayData:isScoreReceived(scoreId)

            if self.rewardCells[id] then
                self.rewardCells[id]:refreshByConfig(reward, isReceived, isReceived)
                if canGet then
                    self.rewardCells[id]:showFrameAnimation()
                    self.rewardCells[id]:addClickListener(function()
                        ModuleManager.ActSevenDayManager:onGetRewardScore(scoreId, period)
                    end)
                else
                    self.rewardCells[id]:hideFrameAnimation()
                    self.rewardCells[id]:setClickShowTips()
                end
            end

            if self.effects[id] then
                self.effects[id]:setVisible(not isReceived)
            end
        end
    end

    if maxPoint <= 0 then
        maxPoint = 1
    end

    local taskRewardList = DataManager.ActSevenDayData:getScoreCfg()
    local step = {}
    for id, info in ipairs(taskRewardList) do
        if info.period == period then
            table.insert(step, info.num)
        end
    end

    local sliderValue = 0
    local scale = 1 / #step
    for i, num in ipairs(step) do
        local lastNum = step[i - 1] or 0
        local offset = curPoint - lastNum
        sliderValue = scale * (i - 1) + scale * (offset / (num - lastNum))
        if curPoint <= num then
            break
        end
    end

    local slider = uiMap["task_comp.slider_bg.slider"]
    slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = sliderValue
    local pointDesc = uiMap["task_comp.slider_bg.point_desc"]
    pointDesc:setText(curPoint)

    local list = DataManager.ActSevenDayData:getSortTaskIdsByDay(day, period) or GConst.EMPTY_TABLE
    self:refreshScrollRect(list, period)
end

function TaskComp:refreshScrollRect(list, period)
    self.taskList = list
    self.period = period
    if self.scrollRect then
        self.scrollRect:refillCells(#self.taskList)
        return
    end
    local uiMap = self:getUIMap()
    self.scrollRect = uiMap["task_comp.scrollrect_task"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
		return TASK_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		cell:refresh(self.taskList[index], self.period)
	end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.taskList)
end

return TaskComp