local ActSprintManager = class("ActSprintManager", BaseModule)

local CLIENT_MODE = false

function ActSprintManager:showActSprintListUI(params)
	UIManager:showUI("app/ui/activity/act_sprint/act_sprint_list_ui", params)
end

function ActSprintManager:showActSprintShopUI(params)
	UIManager:showUI("app/ui/activity/act_sprint/act_sprint_shop_ui", params)
end

function ActSprintManager:showActSprintMainUI(params)
	UIManager:showUI("app/ui/activity/act_sprint_summon_all/act_main_ui", params)
end

-- 冲刺活动 商店兑换
function ActSprintManager:reqExchange(id, count)
	if EDITOR_MODE and CLIENT_MODE then
		self:rspExchange({
			err_code = GConst.ERROR_STR.SUCCESS,
			reqData = {
				id = id,
				count = count,
			}
		})
		return
	end
    self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityRushExchangeReq, {id = id, count = count}, self.rspExchange, BIReport.ITEM_GET_TYPE.ACT_SPRINT_EXCHANGE)
end
function ActSprintManager:rspExchange(result)
    if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end

	DataManager.ActSprintData:onExchangeSuccess(result.reqData.id, result.reqData.count)
	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end
end

-- 冲刺活动 达标奖励
function ActSprintManager:reqStandardReward(id)
	if EDITOR_MODE and CLIENT_MODE then
		self:rspStandardReward({
			err_code = GConst.ERROR_STR.SUCCESS,
			reqData = {
				activity_id = id,
			}
		})
		return
	end
	self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityScoreTaskRoundClaimReq, {activity_id = id}, self.rspStandardReward, BIReport.ITEM_GET_TYPE.ACT_SPRINT_STANDARD)
end
function ActSprintManager:rspStandardReward(result)
    if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end

	local sprintData = DataManager.ActSprintData:getSprintDetailData(result.reqData.activity_id)
	-- sprintData:onGetStandardTurnReward(result.reqData.id)
	DataManager.ActTaskData:onGetTurnRewardScore(result.reqData.activity_id)
	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end
	-- bi
	local biType = sprintData:getBIType()
	local taskFinishNum, taskTotalNum = sprintData:getBISprintTaskNumInfo()
	local sprintTaskRound = DataManager.ActTaskData:getTurnScore(result.reqData.activity_id)
	local sprintRewardRound = DataManager.ActTaskData:getClaimedTurnScore(result.reqData.activity_id)
	BIReport:postActSprintClaimSprintReward(result.reqData.activity_id, biType, taskFinishNum, taskTotalNum, sprintTaskRound, sprintRewardRound)
end

-- 冲刺活动 章节礼包
function ActSprintManager:reqGift(id)
	if EDITOR_MODE and CLIENT_MODE then
		local giftData = {}
        giftData.gift_type = PayManager.PURCHARSE_TYPE.ACT_GIFT
        giftData.id = id
        giftData.buy_count = DataManager.PaymentData:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, id) + 1
        giftData.latest_buy_at = Time:getServerTime() -- * 1000 -- 服务器都是毫秒
        DataManager.PaymentData:updateGiftInfo(giftData, {})

		return
	end
	PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
end

-- 冲刺活动 达标/任务奖励
function ActSprintManager:reqTask(actId, id)
    self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityTaskClaimReq, {activity_id = actId, task_id = id}, self.rspTask, BIReport.ITEM_GET_TYPE.ACT_SPRINT_TASK)
end

function ActSprintManager:rspTask(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end

	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end

	DataManager.ActTaskData:onTaskClaimed(result.reqData.activity_id, result.reqData.task_id)

	-- bi
	local sprintData = DataManager.ActSprintData:getSprintDetailData(result.reqData.activity_id)
	local biType = sprintData:getBIType()
	local taskFinishNum, taskTotalNum = sprintData:getBINormalTaskNumInfo()
	local sprintTaskRound = DataManager.ActTaskData:getTurnScore(result.reqData.activity_id)
	local sprintRewardRound = DataManager.ActTaskData:getClaimedTurnScore(result.reqData.activity_id)
	BIReport:postActSprintClaimNormalTask(result.reqData.activity_id, biType, result.reqData.task_id, taskFinishNum, taskTotalNum, sprintTaskRound, sprintRewardRound)
end

function ActSprintManager:reqStandardTask(actId, id)
    self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityScoreTaskClaimReq, {activity_id = actId, task_id = id}, self.rspStandardTask, BIReport.ITEM_GET_TYPE.ACT_SPRINT_TASK)
end

function ActSprintManager:rspStandardTask(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end

	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end

	DataManager.ActTaskData:onTaskSuccessScore(result.reqData.activity_id)
	local sprintData = DataManager.ActSprintData:getSprintDetailData(result.reqData.activity_id)
	local canGetRewardStandardTaskList = sprintData:getCanGetRewardStandardTaskList()
	-- bi
	local biType = sprintData:getBIType()
	local taskFinishNum, taskTotalNum = sprintData:getBISprintTaskNumInfo()
	local sprintTaskRound = DataManager.ActTaskData:getTurnScore(result.reqData.activity_id)
	local sprintRewardRound = DataManager.ActTaskData:getClaimedTurnScore(result.reqData.activity_id)
	BIReport:postActSprintClaimSprintTask(result.reqData.activity_id, biType, canGetRewardStandardTaskList, taskFinishNum, taskTotalNum, sprintTaskRound, sprintRewardRound)
end

function ActSprintManager:reqSprintBountySign(actType, actId, grade)
	local sprintData = DataManager.ActSprintData:getSprintDetailData(actId)
	local day = sprintData:getBountyCurrDay()
	local actId = sprintData:getActId()
	local args = {
		act_type = actType,
		bi_act_type = sprintData:getBIType(),
		lv = 0,
		grade = grade,
		day = day,
		act_id = actId
	}
	self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityRushSignReq, args, self.rspSprintBountySign, BIReport.ITEM_GET_TYPE.ACT_SPRINT_SIGN_BOX)
end

function ActSprintManager:rspSprintBountySign(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	if result.rewards then
		GFunc.showRewardBox(result.rewards)
	end

	local sprintData = DataManager.ActSprintData:getSprintDetailData(result.reqData.act_id)
	sprintData:onBountySign(result.sign)
	BIReport:postActSprintSignBounty(result.reqData.act_id, result.reqData.bi_act_type, result.reqData.day, result.reqData.grade == 1)
end

function ActSprintManager:onBuyBountyPro(giftId)
	PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
end

--region 打点
BIReport.ACT_SPRINT_OPT_TYPE = {
	SPRINT_REWARD = "SprintReward",
	SPRINT_TASK = "SprintTask",
	NORMAL_TASK = "NormalTask",
	SIGN_BOUNTY = "SignBounty",
}

-- 礼包
BIReport.GIFT_TYPE.ACT_SPRINT_GIFT = "act_sprint_gift"

-- 冲刺活动
BIReport.ITEM_GET_TYPE.ACT_SPRINT_EXCHANGE = "act_sprint_exchange"
BIReport.ITEM_GET_TYPE.ACT_SPRINT_STANDARD = "act_sprint_standard"
BIReport.ITEM_GET_TYPE.ACT_SPRINT_TASK = "act_sprint_task"
BIReport.ITEM_GET_TYPE.ACT_SPRINT_GIFT = "act_sprint_gift"
BIReport.ITEM_GET_TYPE.ACT_SPRINT_SIGN_BOX = "act_sprint_sign_box"

PayManager.BI_GIFT_TYPE[PayManager.PURCHARSE_TYPE.ACT_GIFT][PayManager.PURCHARSE_ACT_TYPE.ACT_COMMON][PayManager.ACT_COMMON_TYPE.ACT_SPRINT_SUMMON_GIFT] = BIReport.GIFT_TYPE.ACT_SPRINT_GIFT
PayManager.BI_ITEM_GET_TYPE[PayManager.PURCHARSE_TYPE.ACT_GIFT][PayManager.PURCHARSE_ACT_TYPE.ACT_COMMON][PayManager.ACT_COMMON_TYPE.ACT_SPRINT_SUMMON_GIFT] = BIReport.ITEM_GET_TYPE.ACT_SPRINT_GIFT

local EVENT_NAME_ACT_SPRINT = "client_act_sprint"

function BIReport:postActSprintClaimSprintReward(actId, actType, curNum, totalNum, sprintTaskRound, sprintRewardRound)
	local args = {
		opt_type = BIReport.ACT_SPRINT_OPT_TYPE.SPRINT_REWARD,
		act_id = actId,
		act_type = actType,
		finish_num = curNum,
		total_num = totalNum,
		sprint_task_round = sprintTaskRound,
		sprint_reward_round = sprintRewardRound,
	}
	self:report(EVENT_NAME_ACT_SPRINT, args)
end

function BIReport:postActSprintClaimSprintTask(actId, actType, taskIds, curNum, totalNum, sprintTaskRound, sprintRewardRound)
	local taskStr = GConst.EMPTY_STRING
	for _, taskId in ipairs(taskIds) do
		taskStr = taskStr .. tostring(taskId) .. "|"
	end
	local args = {
		opt_type = BIReport.ACT_SPRINT_OPT_TYPE.SPRINT_TASK,
		act_id = actId,
		act_type = actType,
		task_str = taskStr,
		finish_num = curNum,
		total_num = totalNum,
		sprint_task_round = sprintTaskRound,
		sprint_reward_round = sprintRewardRound,
	}
	self:report(EVENT_NAME_ACT_SPRINT, args)
end

function BIReport:postActSprintClaimNormalTask(actId, actType, taskId, curNum, totalNum, sprintTaskRound, sprintRewardRound)
	local args = {
		opt_type = BIReport.ACT_SPRINT_OPT_TYPE.NORMAL_TASK,
		act_id = actId,
		act_type = actType,
		task_str = tostring(taskId),
		finish_num = curNum,
		total_num = totalNum,
		sprint_task_round = sprintTaskRound,
		sprint_reward_round = sprintRewardRound,
	}
	self:report(EVENT_NAME_ACT_SPRINT, args)
end

function BIReport:postActSprintSignBounty(actId, actType, signDay, isPro)
	local args = {
		opt_type = BIReport.ACT_SPRINT_OPT_TYPE.SIGN_BOUNTY,
		act_id = actId,
		act_type = actType,
		sign_day = signDay,
		is_pro = isPro
	}
	self:report(EVENT_NAME_ACT_SPRINT, args)
end
--endregion

return ActSprintManager