local FourteenDayManager = class("FourteenDayManager", BaseModule)

function FourteenDayManager:showFourteenDayUI()
    if DataManager.FourteenDayData:getNeedSyncData() then
        self:reqSyncData()
    end

    local time = DataManager.FourteenDayData:getEndRemainTime()
    if time > 0 then
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_entrance_ui")
    else
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_task_ui")
    end
end

-- 购买活动战令
function FourteenDayManager:buyBounty()
    PayManager:purchasePackage(GConst.FourteenDayConst.BOUNTY_GIFT_ID, PayManager.PURCHARSE_TYPE.ACT_GIFT)
end

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

function FourteenDayManager:rspTaskReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FourteenDayData:onReceivedTaskReward(result.reqData.id)
        GFunc.showRewardBox(result.rewards)
    end
end

-- 请求战令奖励
function FourteenDayManager:reqBountyReward(id, grade)
    local list = {}
    table.insert(list, {id = id, grade = grade})
    self:sendMessage(ProtoMsgType.FromMsgEnum.FourteenBountyAwardReq, {id_with_lv = list}, {}, self.rspBountyReward, BIReport.ITEM_GET_TYPE.ACT_FOURTEEN_DAY_BOUNTY)
end

function FourteenDayManager:rspBountyReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FourteenDayData:onReceivedBountyReward(result.id_with_lv)

        GFunc.showRewardBox(result.rewards)
    end
end

-- 请求兑换奖励
function FourteenDayManager:reqExchangeReward(id, count)
    self:sendMessage(ProtoMsgType.FromMsgEnum.FourteenBountyExchangeAwardReq, {id = id, count = count or 0}, {}, self.rspExchangeReward, BIReport.ITEM_GET_TYPE.ACT_FOURTEEN_DAY_EXCHANGE)
end

function FourteenDayManager:rspExchangeReward(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        if result.reqData.count then
            DataManager.FourteenDayData:onReceivedExchangeReward(result.reqData.id, result.reqData.count)
        end

        GFunc.showRewardBox(result.rewards)
    end
end

function FourteenDayManager:reqSyncData()
    self:sendMessage(ProtoMsgType.FromMsgEnum.FourteenDayInfoReq, {}, {}, self.rspSyncData)
end

function FourteenDayManager:rspSyncData(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.FourteenDayData:setSyncData()
        DataManager.FourteenDayData:initData(result.info)
        DataManager.FourteenDayData:setDirty()
    end
end

return FourteenDayManager