local ActSevenDayManager = class("ActSevenDayManager", BaseModule)

function ActSevenDayManager:showMainUI()
    UIManager:showUI("app/ui/activity/act_seven_day/act_seven_day_ui")
end

function ActSevenDayManager:showMainS2UI()
    UIManager:showUI("app/ui/activity/act_seven_day/act_seven_day_s2_ui")
end

function ActSevenDayManager:onGetRewardTask(id, period)
    period = period or 1
    if not DataManager.ActSevenDayData:isOpen(period) then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ADVENTURE_CLOSED))
        return
    end

    -- 一键领取处理
    -- id = nil

    local itemGetType = period == 1 and BIReport.ITEM_GET_TYPE.SEVEN_DAY_TASK or BIReport.ITEM_GET_TYPE.SEVEN_DAY_TASK_2
    -- self:sendMessage(ProtoMsgType.FromMsgEnum.SevenDayTaskRewardReq, {id = id, period = period}, self.onGetRewardTaskSuccess, itemGetType)
    self:sendMessage(ProtoMsgType.FromMsgEnum.SevenDayTaskRewardReq, {ids = {id}}, self.onGetRewardTaskSuccess, itemGetType)
end

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

    local ids = DataManager.ActSevenDayData:getAllCanClaimTaskIds(result.reqData.period)
    DataManager.ActSevenDayData:setTaskClaimed(result.reqData.id, ids)

    if result.reqData.id then
        BIReport:postSevenDayTask(BIReport.SEVEN_DAY_OPT.TASK_REWARD, {result.reqData.id}, result.rewards, result.reqData.period)
    else
        BIReport:postSevenDayTask(BIReport.SEVEN_DAY_OPT.TASK_REWARD, ids, result.rewards, result.reqData.period)
    end
end

function ActSevenDayManager:onGetRewardScore(id, period)
    period = period or 1
    if not DataManager.ActSevenDayData:isOpen(period) then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ADVENTURE_CLOSED))
        return
    end

    -- 一键领取处理
    -- id = nil

    local itemGetType = period == 1 and BIReport.ITEM_GET_TYPE.SEVEN_DAY_SCORE or BIReport.ITEM_GET_TYPE.SEVEN_DAY_SCORE_2
    -- self:sendMessage(ProtoMsgType.FromMsgEnum.SevenDayRewardReq, {id = id, period = period}, self.onGetRewardScoreSuccess, itemGetType)
    self:sendMessage(ProtoMsgType.FromMsgEnum.SevenDayRewardReq, {id = id}, self.onGetRewardScoreSuccess, itemGetType)
end

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

    local ids = DataManager.ActSevenDayData:getAllCanClaimScoreIds(result.reqData.period)
    DataManager.ActSevenDayData:setScoreReceived(result.reqData.id, ids)

    if result.reqData.id then
        BIReport:postSevenDayTask(BIReport.SEVEN_DAY_OPT.SCORE_REWARD, {result.reqData.id}, result.rewards, result.reqData.period)
    else
        BIReport:postSevenDayTask(BIReport.SEVEN_DAY_OPT.SCORE_REWARD, ids, result.rewards, result.reqData.period)
    end
end

function ActSevenDayManager:onGiftBuy(giftId)
    PayManager:purchasePackage(giftId, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
end

--region 上报
BIReport.SEVEN_DAY_OPT = {
	TASK_REWARD = "TaskReward",
	SCORE_REWARD = "ScoreReward",
}

-- 七日乐
BIReport.ITEM_GET_TYPE.ACT_SEVEN_DAY_SIGN = "seven_day_sign"
BIReport.ITEM_GET_TYPE.SEVEN_DAY_RESIGN = "seven_day_resign"
BIReport.ITEM_GET_TYPE.SEVEN_DAY_TASK = "seven_day_task"
BIReport.ITEM_GET_TYPE.SEVEN_DAY_TASK_2 = "seven_day_task_2"
BIReport.ITEM_GET_TYPE.SEVEN_DAY_SCORE = "seven_day_score"
BIReport.ITEM_GET_TYPE.SEVEN_DAY_SCORE_2 = "seven_day_score_2"

local EVENT_NAME_SEVEN_DAY_OPT = "client_seven_day_opt" -- 七天乐任务
local EVENT_NAME_SEVEN_DAY_GIFT_OPT = "client_seven_day_gift_opt" -- 七天乐礼包
local EVENT_NAME_SEVEN_DAY_LOOP_OPT = "client_seven_day_loop_opt" -- 循环七天乐任务
local EVENT_NAME_SEVEN_DAY_LOOP_GIFT_OPT = "client_seven_day_loop_gift_opt" -- 循环七天乐任务

function BIReport:postSevenDayTask(optType, ids, rewards, period)
	local idsStr = ""
	if ids then
		for i, id in ipairs(ids) do
			idsStr = idsStr .. id .. ";"
		end
	end
	local args = {
		opt_type = optType,
		period = period,
		ids = idsStr,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
		task_score = DataManager.ActSevenDayData:getScore(period),
	}
	self:report(EVENT_NAME_SEVEN_DAY_OPT, args)
end

function BIReport:postSevenDayGift(optType, id, giftType, rewards, dayn)
	local args = {
		opt_type = optType,
		id = id,
		type = giftType,
		reward_str = rewards,
		dayn = dayn,
	}
	self:report(EVENT_NAME_SEVEN_DAY_GIFT_OPT, args)
end

function BIReport:postSevenDayLoopTask(optType, ids, rewards, taskScore, actId)
	local idsStr = ""
	if ids then
		for i, id in ipairs(ids) do
			idsStr = idsStr .. id .. ";"
		end
	end
	local args = {
		act_id = actId,
		opt_type = optType,
		ids = idsStr,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
		task_score = taskScore,
	}
	self:report(EVENT_NAME_SEVEN_DAY_LOOP_OPT, args)
end

function BIReport:postSevenDayLoopGift(optType, id, giftType, rewards, dayn)
	local args = {
		opt_type = optType,
		id = id,
		type = giftType,
		reward_str = rewards,
		dayn = dayn,
	}
	self:report(EVENT_NAME_SEVEN_DAY_LOOP_GIFT_OPT, args)
end

--endregion
return ActSevenDayManager