local FullMoonTaskComp = class("FullMoonTaskComp", LuaComponent)

function FullMoonTaskComp:init()
    local uiMap = self:getUIMap()
    self.listProg = uiMap["task_panel.prog.list_prog"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.content = uiMap["task_panel.prog.list_prog.viewport.content"]
    self.prog = uiMap["task_panel.prog.list_prog.viewport.content.prog"]
    self.imgProg = uiMap["task_panel.prog.list_prog.viewport.content.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.listTask = uiMap["task_panel.list_task"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.spineSkin = uiMap["task_panel.prog.spine_skin"]
    self.imgScore = uiMap["task_panel.prog.score.img_score"]
    self.txScore = uiMap["task_panel.prog.score.tx_score"]
    self.txSkin = uiMap["task_panel.prog.skin_desc.skin.tx_skin"]
    self.txAllFinish = uiMap["task_panel.tx_all_finish"]

    local skinId,heroId
    for id, data in pairs(ConfigManager:getConfig("activity_full_moon_reward")) do
        if ConfigManager:getConfig("item")[data.reward.id].type == GConst.ItemConst.ITEM_TYPE.SKIN then
            skinId = DataManager.SkinData:getSkinIdByItemId(data.reward.id)
            heroId = DataManager.SkinData:getHeroIdBySkinId(skinId)
        end
    end
    if heroId and skinId then
        self.txSkin:setText(DataManager.HeroData:getHeroById(heroId):getName() .. "-" .. DataManager.SkinData:getName(skinId))

        self.spineSkin:getSkeletonGraphic().enabled = false
        self.spineSkin:loadAssetAsync(DataManager.SkinData:getModelId(skinId), function()
            self.spineSkin:getSkeletonGraphic().enabled = true
            self.spineSkin:playAnim("idle", true, true, true)
        end, true)
    end
end

function FullMoonTaskComp:refresh()
    self.txScore:setText(DataManager.FullMoonData:getTaskFinishNum())
    GFunc.centerImgAndTx(self.imgScore, self.txScore)

    -- 任务列表
    local tasks = DataManager.FullMoonData:getTaskListSort()
    if tasks then
        self.txAllFinish:setActive(false)
        self.listTask:addInitCallback(function()
            return "app/ui/activity/full_moon/cell/full_moon_task_cell"
        end)
        self.listTask:addRefreshCallback(function(index, cell)
            cell:refresh(tasks[index].id)
        end)
        self.listTask:clearCells()
        self.listTask:refillCells(#tasks)
    else
        self.txAllFinish:setActive(true)
        self.txAllFinish:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_11))
    end

    -- 任务进度奖励列表
	self.listProg:addInitCallback(function()
		return "app/ui/activity/full_moon/cell/full_moon_task_prog_cell"
	end)
	self.listProg:addRefreshCallback(function(index, cell)
		cell:refresh(DataManager.FullMoonData:getTaskProgressRewardIdByIndex(index))
	end)
	self.listProg:clearCells()
	self.listProg:refillCells(DataManager.FullMoonData:getTaskProgressRewardTotalNum())
    local idx = DataManager.FullMoonData:getCurTaskProgressRewardIndex()
    -- Logger.logHighlight(idx)
    self.listProg:moveToIndex(idx)

    local maxIndex = DataManager.FullMoonData:getTaskProgressRewardTotalNum()
	local cellWidth = self.listProg:getCellWidth()
	self.prog:setSizeDeltaX(cellWidth * maxIndex)
	self.imgProg.value = DataManager.FullMoonData:getTaskFinishNum() / DataManager.FullMoonData:getTaskTotalNum()
end

return FullMoonTaskComp