local FullMoonManager = class("FullMoonManager", BaseModule)

function FullMoonManager:showActMainUI(panelType)
    UIManager:showUI("app/ui/activity/full_moon/full_moon_ui", panelType)
end

-- 初始化计时器
function FullMoonManager:initTimer()
    self:unscheduleGlobal(self.actSid)

    if DataManager.FullMoonData:isOpenTime() then
        Logger.logHighlight("圆月活动结束倒计时："..DataManager.FullMoonData:getEndRemainTime())
        self.actSid = self:performWithDelayGlobal(function()
            Logger.logHighlight("圆月活动结束")
            EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.ACTIVITY_FULL_MOON_END)
        end, DataManager.FullMoonData:getEndRemainTime())
    elseif DataManager.FullMoonData:getStartRemainTime() > 0 then
        Logger.logHighlight("圆月活动开始倒计时："..DataManager.FullMoonData:getStartRemainTime())
        self.actSid = self:performWithDelayGlobal(function()
            Logger.logHighlight("圆月活动开始")
            self:reqActData()
        end, DataManager.FullMoonData:getStartRemainTime())
    end
end

-- 请求活动数据
function FullMoonManager:reqActData()
    if not DataManager.FullMoonData:isOpen() then
        return
    end
    if not DataManager.FullMoonData:isOpenTime() then
        return
    end
    if self.isReqActData then
        return
    end

    self.isReqActData = true
    self:sendMessage(ProtoMsgType.FromMsgEnum.FullMoonInfoReq, {}, {}, self.rspActData, nil)
end

function FullMoonManager:rspActData(result)
    self.isReqActData = false
    DataManager.FullMoonData:setInReset(false)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FullMoonData:onGetActData(result.full_moon)
    end
end

-- 请求任务奖励
function FullMoonManager:reqTaskReward(id)
    self:sendMessage(ProtoMsgType.FromMsgEnum.FullMoonTaskClaimReq, {id = id}, {}, self.rspTaskReward, BIReport.ITEM_GET_TYPE.FULL_MOON_TASK)
end

function FullMoonManager:rspTaskReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FullMoonData:onReceivedTaskReward(result.reqData.id, result.finished)
        GFunc.showRewardBox(result.rewards)
    end
end

-- 请求任务进度奖励
function FullMoonManager:reqTaskProgressReward(id)
    self:sendMessage(ProtoMsgType.FromMsgEnum.FullMoonRewardClaimReq, {id = id}, {}, self.rspTaskProgressReward, BIReport.ITEM_GET_TYPE.FULL_MOON_TASK_PROG_REWARD)
end

function FullMoonManager:rspTaskProgressReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FullMoonData:onReceivedTaskProgressReward(result.reqData.id)
        GFunc.showRewardBox(result.rewards)
    end
end

-- 自选礼包选择
function FullMoonManager:reqGiftChooseHero(index)
    self:sendMessage(ProtoMsgType.FromMsgEnum.FullMoonCustomGiftChooseReq, {index = index}, {}, self.rspGiftChooseHero, nil)
end

function FullMoonManager:rspGiftChooseHero(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FullMoonData:onChooseHeroSuccess(result.reqData.index)
    end
end

-- 皮肤挑战
function FullMoonManager:skinChallenge(id)
    DataManager.FullMoonData:setCurSkinChallengeId(id)
    ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.FULL_MOON_SKIN, nil, function()
        UIManager:closeAllUI()
        ModuleManager.MaincityManager:showMainCityUI()
        self:showActMainUI(GConst.FullMoonConst.PANEL_TYPE.SKIN)
    end)
end

-- 请求皮肤关卡挑战奖励
function FullMoonManager:reqChapterSkinClaim(id, combatReport)
    if not combatReport.victory or DataManager.FullMoonData:isReceivedSkinFirstChallengeReward(id) then
        ModuleManager.BattleManager:showBattleResultUI(GConst.BattleConst.BATTLE_TYPE.FULL_MOON_SKIN, {}, combatReport or {}, nil, nil, true)
        return
    end

    self:sendMessage(ProtoMsgType.FromMsgEnum.FullMoonChapterSkinClaimReq, {id = id, combatReport = combatReport}, {}, self.rspChapterSkinClaim, BIReport.ITEM_GET_TYPE.FULL_MOON_SKIN_CHALLENGE_REWARD)
end

function FullMoonManager:rspChapterSkinClaim(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FullMoonData:onReceivedSkinChallengeReward(result.reqData.id)
        ModuleManager.BattleManager:showBattleResultUI(GConst.BattleConst.BATTLE_TYPE.FULL_MOON_SKIN, result.rewards, result.reqData and result.reqData.combatReport or {}, nil, nil, true)
    end
end

return FullMoonManager