local SummonManager = class("SummonManager", BaseModule)

-- function SummonManager:showPopUI(params)
--     UIManager:showUI("app/ui/summon/summon_act_pop", params)
-- end
-- function SummonManager:showSummonFundUI(showPage)
--     UIManager:showUI("app/ui/summon/summon_act_ui",{ showPage = showPage })
-- end

function SummonManager:showSummonMainUI(summonId)
	UIManager:showUI("app/ui/summon/summon_main_ui", {summonId = summonId})
end

function SummonManager:showSummonWishUI(summonId)
	UIManager:showUI("app/ui/summon/summon_wish_ui", {summonId = summonId})
end

function SummonManager:showSummonRewardUI(params)
	UIManager:showUI("app/ui/summon/summon_reward_ui", params)
end

function SummonManager:showSummonOddsUI(summonId)
	UIManager:showUI("app/ui/summon/summon_odds_ui", {summonId = summonId})
end

function SummonManager:showSummonUnlockUI(params)
	UIManager:showUI("app/ui/summon/summon_unlock_ui", params)
end

function SummonManager:showSummonShopUI(shopType)
	UIManager:showUI("app/ui/summon/summon_shop_ui", { shopType = shopType })
end

function SummonManager:showSummonShopBoxUI(shopType, cfg)
	UIManager:showUI("app/ui/summon/summon_shop_box_ui", { shopType = shopType, cfg = cfg })
end

--@region 协议
function SummonManager:onForceSummonReq(count, free, summonType)
	local args = {}
	args.times = count
	args.free = free
	args.id = summonType
	self:sendMessage(ProtoMsgType.FromMsgEnum.SummonReq, args, self.onForceSummonRsp, BIReport.ITEM_GET_TYPE.FORCE_SUMMON)
end

function SummonManager:onForceSummonRsp(result)
	if result.status ~= 0 then
		EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.FORCE_SUMMON)
		return
	end
	local newHero = {}
	local guarantee1 = DataManager.SummonData:getSummonWishGuarantee1(result.reqData.id)
	for _, reward in pairs(result.rewards) do
		local cfg = ConfigManager:getConfig("item")[reward.item.id]
		if cfg.type == GConst.ItemConst.ITEM_TYPE.HERO_FRAGMENT then
			local isNew = DataManager.HeroData:getHeroIsNew(reward.item.id)
			if isNew then
				table.insert(newHero, reward.item.id)
			end
			if guarantee1 then
				if DataManager.HeroData:getHeroQlt(reward.item.id) == 5 then
					DataManager.SummonData:setSummonTrigger(result.reqData.id, true)
				else
					DataManager.SummonData:setSummonTrigger(result.reqData.id, false)
				end
			end
		end
	end
	DataManager.SummonData:updateSummonCount(result.reqData.id, result.reqData.times, result.reqData.free)
	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.FORCE_SUMMON, result, newHero)
	BIReport:postForceSummon(result.reqData.free, result.reqData.times, result.rewards, result.costs)

	-- local params = {
	-- 	times = result.reqData.times,
	-- 	rewards = result.rewards,
	-- }
	-- 累计召唤英雄x次
	ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_SUMMON_HERO, result.reqData.times)

	-- if result.first then
	-- 	DataManager.GameSettingData:showRateUIByForceSummon()
	-- end
end

-- 商店
function SummonManager:reqForceSummonShopExchange(id, count)
	self:sendMessage(ProtoMsgType.FromMsgEnum.SummonShopExchangeReq, {id = id, count = count}, self.rspForceSummonShopExchange, BIReport.ITEM_GET_TYPE.FORCE_SUMMON_SHOP_EXCHANGE)
end

function SummonManager:rspForceSummonShopExchange(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	GFunc.showRewardBox(result.rewards)
    DataManager.SummonData:shopSuccess(result.reqData.id, result.reqData.count)
	BIReport:postSummonShopExchange(BIReport.SUMMON_OP_TYPE.SUMMON_SHOP, result.reqData.id, result.reqData.count, result.rewards)
end

function SummonManager:reqSummonShopMarkUnlockTip(shopType)
    -- local ids = DataManager.SummonData:getUnlockShopId(shopType)
	-- self:sendMessage(ProtoMsgType.FromMsgEnum.SummonShopMarkUnlockTipReq, {ids = ids}, self.rspSummonShopMarkUnlockTip, BIReport.ITEM_GET_TYPE.SUMMON_OPEN_EXCHANGED_UI)
end

function SummonManager:rspSummonShopMarkUnlockTip(result)
    -- DataManager.SummonData:onSummonShopSoulOpen(result.reqData.ids)
end
--@endregion

function SummonManager:onSummonWishSetReq(summonType, heroId)
	self:sendMessage(ProtoMsgType.FromMsgEnum.SummonWishSetReq, {id = summonType, hero = heroId}, self.onSummonWishSetRsp,
		BIReport.ITEM_GET_TYPE.FORCE_SUMMON_WISH_HREO_ID)
end

function SummonManager:onSummonWishSetRsp(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	DataManager.SummonData:updateSummonWishHeroId(result.reqData.id, result.reqData.hero)
	EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.FORCE_SUMMON_WISH_HERO_ID)
end

function SummonManager:onSummonWishClaimReq(summonType)
	self:sendMessage(ProtoMsgType.FromMsgEnum.SummonWishClaimReq, {id = summonType}, self.onSummonWishClaimRsp,
		BIReport.ITEM_GET_TYPE.FORCE_SUMMON_WISH_HREO_ID)
end

function SummonManager:onSummonWishClaimRsp(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	DataManager.SummonData:summonWishClaim()
	GFunc.showRewardBox(result.rewards)
	-- local newForce = {}
	-- for _, reward in pairs(result.rewards) do
	-- 	local isNew = DataManager.ForceData:getForceIsNew(reward.item.id)
	-- 	if isNew then
	-- 		table.insert(newForce, reward.item.id)
	-- 		DataManager.ForceData:setForceIsNew(reward.item.id, false)
	-- 	end
	-- end
	-- EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.FORCE_SUMMON_WISH_CLAIM, result,newForce)
end


--region卡池活动
function SummonManager:onBuyBoundyGift(id)
	PayManager:purchasePackage(id, PayManager.PURCHARSE_TYPE.ACT_GIFT, true)
end

--1=签到, 2=英雄战令1, 3=英雄战令2
--act_bounty表ID, 0表示一键领取
--grade  领取的档位 0.免费档 1.付费档
function SummonManager:activityHeroBountyClaimedReq(type, lv, grade)
	self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityHeroBountyClaimedReq, { type = type, lv = lv, grade = grade }, self.activityHeroBountyClaimedRsp,
		GConst.SummonConst.ACT_COMP_GER_REWARD_BI[type])
end

function SummonManager:activityHeroBountyClaimedRsp(result)
	if result.err_code ~= GConst.ERROR_STR.SUCCESS then
		return
	end
	GFunc.showRewardBox(result.rewards)
	local activityId = DataManager.SummonActData:getActId()
	if result.reqData.type == 1 then
		local curDay = DataManager.SummonActData.SummonActLoginData:getCurLv()
		local maxIndex, minIndex = DataManager.SummonActData.SummonActLoginData:getMinAndMaxUnclaimedRewardId()
		DataManager.SummonActData.SummonActLoginData:initActivityInfo(result.info)
		BIReport:postActSummonLogin(activityId,minIndex, maxIndex, curDay, result.rewards)
	elseif result.reqData.type == 2 then
		local heroId = DataManager.SummonActData.SummonActUpgradeData:getHeroId(1)
		local heroLv = DataManager.ForceData:getForceLevel(heroId)
		local maxIndex, minIndex = DataManager.SummonActData.SummonActUpgradeData:getMinAndMaxUnclaimedRewardId(1)
		DataManager.SummonActUpgradeData:initActivityInfo(result.info)
		BIReport:postActSummonUpgrade("HeroPage_1", activityId,minIndex, maxIndex, heroId, heroLv,result.rewards)
	elseif result.reqData.type == 3 then
		local heroId = DataManager.SummonActData.SummonActUpgradeData:getHeroId(2)
		local heroLv = DataManager.ForceData:getForceLevel(heroId)
		local maxIndex, minIndex = DataManager.SummonActData.SummonActUpgradeData:getMinAndMaxUnclaimedRewardId(2)
		DataManager.SummonActData.SummonActUpgradeData:initActivityInfo(result.info)
		BIReport:postActSummonUpgrade("HeroPage_2", activityId,minIndex, maxIndex, heroId, heroLv,result.rewards)
	end
end

function BIReport:postActSummonLogin(activityId, minId, maxId, cur, rewards)
	local args = {
		opt_type = "Reward",
		act_id = activityId,
		max_id = maxId,
		min_id = minId,
		cur_day = cur,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
	}
	self:report(BIReport.EVENT_NAME_ACT_SUMMON_LOGIN, args)
end
function BIReport:postActSummonUpgrade(heroPage, activityId, minId, maxId, heroId, HeroLv, rewards)
	local args = {
		opt_type    = "Reward",
		act_id      = activityId,
		max_id      = maxId,
		min_id      = minId,
		hero_id     = heroId,
		hero_level  = HeroLv,
		hero_page   = heroPage,
		reward_str  = rewards and GFunc.getRewardsStr(rewards) or nil,
	}
	self:report(BIReport.EVENT_NAME_ACT_SUMMON_UPGRADE, args)
end
function BIReport:postActSummonGem(activityId, minId, maxId, cur, rewards)
	local args = {
		opt_type = "Reward",
		act_id = activityId,
		max_id = maxId,
		min_id = minId,
		cur_day = cur,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
	}
	self:report(BIReport.EVENT_NAME_ACT_SUMMON_LOGIN, args)
end
function BIReport:postActSummonRecharge(activityId, minId, maxId, cur, rewards)
	local args = {
		opt_type = "Reward",
		act_id = activityId,
		max_id = maxId,
		min_id = minId,
		cur_day = cur,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
	}
	self:report(BIReport.EVENT_NAME_ACT_SUMMON_LOGIN, args)
end
--endregion



-- region 任务

function SummonManager:reqTaskReward(actId, taskId)
	self:sendMessage(ProtoMsgType.FromMsgEnum.ActivityTaskClaimReq, {activity_id = actId, task_id = taskId}, self.rspTaskReward, BIReport.ITEM_GET_TYPE.ACT_TASK_REWARD)
end

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

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

	BIReport:postActTask(result.reqData.activity_id, result.reqData.task_id, result.rewards)
end

function BIReport:postActTask(actId, taskId, rewards)
	local args = {
		act_id = actId,
		task_id = taskId,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
	}
	self:report(BIReport.EVENT_NAME_ACT_TASK, args)
end

--@endregion

--@region 上报
BIReport.EVENT_NAME_ACT_SUMMON_LOGIN = "client_act_summon_login"
BIReport.EVENT_NAME_ACT_SUMMON_UPGRADE = "client_act_summon_upgrade"
BIReport.EVENT_NAME_ACT_SUMMON_GEM = "client_act_summon_gem"
BIReport.EVENT_NAME_ACT_SUMMON_RECHARGE = "client_act_summon_recharge"
BIReport.EVENT_NAME_ACT_SUMMON = "client_act_summon" -- 念力觉醒
BIReport.EVENT_NAME_FORCE = "client_hero_opt" -- 士兵
BIReport.ITEM_GET_TYPE.FORCE_SUMMON_SHOP_EXCHANGE = "force_summon_shop_exchange"

BIReport.SUMMON_OP_TYPE = {
	REWARD_ROUND = "RewardRound",
	REWARD_ROUND_TASK = "RewardRoundTask",
	REWARD_RECHARGE_TASK = "RewardRechargeTask",
	SUMMON_SHOP = "SummonShop",
}

BIReport.FORCE_OPT_TYPE = {
	UNLOCK = "Unlock",
	LEVELUP = "LevelUp",
	STARUP = "StarUp",
	FORMATION = "Formation",
	SUMMON = "Summon",
}

-- 士兵召唤
function BIReport:postForceSummon(isfree, count, rewards, costs, summonId)
	local args = {
		opt_type = BIReport.FORCE_OPT_TYPE.SUMMON,
		free_summon = isfree,
		count = count,
		summon_id = summonId,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
		cost_str = costs and GFunc.getRewardsStr(costs) or nil,
	}
	self:report(BIReport.EVENT_NAME_FORCE, args)
end

function BIReport:postSummonShopExchange(optType, id, num, rewards)
	local args = {
        optType = optType,
		shop_id = id,
		exchange_num = num,
		reward_str = rewards and GFunc.getRewardsStr(rewards) or nil,
	}
	self:report(BIReport.EVENT_NAME_ACT_SUMMON, args)
end

--@endregion
return SummonManager
