local TaskCell = class("TaskCell", BaseCell)

function TaskCell:init()
    local uiMap = self.baseObject:genAllChildren()
    self.bg2 = uiMap["task_cell.bg.bg2"]
    self.rewardCell = uiMap["task_cell.bg.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    self.desc = uiMap["task_cell.bg.desc"]
    self.sliderOver = uiMap["task_cell.bg.slider_bg.slider_over"]
    self.sliderComp = uiMap["task_cell.bg.slider_bg.slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.sliderTx = uiMap["task_cell.bg.slider_bg.text"]
    self.cliamBtn = uiMap["task_cell.bg.cliam_btn"]
    self.cliamBtn:addClickListener(function()
        if self.id and DataManager.SevenDayData:canClaimTask(self.id) then
            ModuleManager.SevenDayManager:claimTaskReward(self.id)
        end
    end)
    self.btnGreyImg = uiMap["task_cell.bg.cliam_btn.grey"]
    self.mask = uiMap["task_cell.bg.mask"]
    self.cliamBtnTx = uiMap["task_cell.bg.cliam_btn.text"]
    uiMap["task_cell.bg.mask.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.STR_COMPLETED))
end

function TaskCell:refresh(id)
    self.id = id
    local canClaimTask = DataManager.SevenDayData:canClaimTask(id)
    local collected = DataManager.SevenDayData:taskCollected(id)
    local count = DataManager.SevenDayData:taskCount(id)
    local totalCount = DataManager.SevenDayData:taskTotalCount(id)
    if collected or count > totalCount then
        count = totalCount
    end
    local reward = DataManager.SevenDayData:getTaskReward(id)
    local taskDesc = DataManager.SevenDayData:getSevenDayTaskDesc(id)
    self.desc:setText(taskDesc)

    local isComplete = count >= totalCount
    self.sliderOver:setVisible(isComplete)
    self.bg2:setVisible(isComplete)
    self.sliderComp.value = count / totalCount
    self.rewardCell:refreshByConfig(reward)
    self.sliderTx:setText(count .. "/" .. totalCount)

    self.btnGreyImg:setVisible(not canClaimTask)
    self.mask:setVisible(collected)
    if collected then
        self.cliamBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_DONE))
    else
        self.cliamBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BTN_CLAIM))
    end
end

return TaskCell