local TaskCell = class("TaskCell", BaseCell)

function TaskCell:refresh(id, period)
    local uiMap = self:getUIMap()
    local cfg = ConfigManager:getConfig("act_sevenday_quest")[id]
    local taskInfo = DataManager.ActSevenDayData:getTaskData(id)
    local slider = uiMap["task_cell.slider_bg.slider"]
    local progress = math.max(taskInfo.progress, 0)
    local targetCount = cfg.number
    if cfg.type == GConst.TaskConst.TASK_TYPE.X_ARENA_GRADING then
        targetCount = DataManager.ArenaData:getGradingScore(targetCount - 1) + 1
        progress = DataManager.ArenaData:getGradingScore(progress)
    end
    progress = math.min(progress, targetCount)
    progress = math.max(progress, 0)

    slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = progress / targetCount
    uiMap["task_cell.progress"]:setText(progress .. "/" .. targetCount)
    local desc = uiMap["task_cell.desc"]
    local condition = targetCount
    local str = GFunc.getTaskDesc(cfg.type, condition)
    desc:setText(str)
    if not self.rewardCell then
        self.rewardCell = CellManager:addCellComp(uiMap["task_cell.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    end
    self.rewardCell:refreshByConfig(cfg.reward[1], false, taskInfo.collect)
    uiMap["task_cell.num"]:setText(cfg.reward[2].num)
    uiMap["task_cell.mask"]:setVisible(taskInfo.collect)
    -- uiMap["task_cell.check_img"]:setVisible(taskInfo.collect)
    self:getBaseObject():addClickListener(function()
        self:onClick(id, period)
    end)

    if DataManager.ActSevenDayData:canClaimTask(id) then
        uiMap["task_cell.bg.red_point"]:setVisible(true)
        self.rewardCell:showFrameAnimation()
        self.rewardCell:addClickListener(function()
            self:onClick(id, period)
        end)
    else
        self.rewardCell:hideFrameAnimation()
        uiMap["task_cell.bg.red_point"]:setVisible(false)
    end
end

function TaskCell:onClick(id, period)
    local cfg = ConfigManager:getConfig("act_sevenday_quest")[id]
    local taskInfo = DataManager.ActSevenDayData:getTaskData(id)
    if taskInfo.collect then
        return
    end
    if taskInfo.progress >= cfg.number then
        ModuleManager.ActSevenDayManager:onGetRewardTask(id, period)
    else
        if cfg.type then
            ModuleManager.TaskManager:taskGoto(cfg.type)
        end
    end
end

return TaskCell