-- 众神之战 战斗逻辑 --
local JjcGodWarLogic = require("jjcGodWar.JjcGodWarLogic")
local JjcGodWarDB = require("jjcGodWar.JjcGodWarDB")
local CombatVideo = require("combat.CombatVideo")
local CreateRole = require("role.CreateRole")
local CombatDefine = require("combat.CombatDefine")
local HeroGrid = require("hero.HeroGrid")
local CombatLogic = require("combat.CombatLogic")
local JjcDB = require("jjc.JjcDB")
local WarReportLogic = require("warReport.WarReportLogic")
local Util = require("common.Util")

--竞猜阶段就开始生成战斗数据
function timerCombat()
	local state,round,leftTime = JjcGodWarLogic.getNowGodWarState()
	if state == JjcGodWarLogic.GODWAR_STATE_WAIT or state == JjcGodWarLogic.GODWAR_STATE_END then return end

	if round == JjcGodWarLogic.GODWAR_JIEDUAN_ZHUNBEI then return end

	local paiweiMatch,needCombat = JjcGodWarDB.getNowNeedCombat(state)
	if not paiweiMatch or not needCombat then return end

	for i=1,#needCombat do
		local pairsData = needCombat[i]
		local atkUuid = pairsData.atkUuid
		local defUuid = pairsData.defUuid
		-- local atkRoleData = JjcGodWarDB.getGodWarDataByUuid(atkUuid)
		-- local defRoleData = JjcGodWarDB.getGodWarDataByUuid(defUuid)
		
		-- require("common.Util").printTable(atkRoleData)
		-- require("common.Util").printTable(defRoleData)
		local fakeHuman = {}
		fakeHuman.db = {}
		fakeHuman.db.combatQuick = {}

	    local param = {}
	    local cbParam = {}
		cbParam.atkIndex = 1    
	    cbParam.defIndex = 1
	    cbParam.atkUuid = atkUuid
        cbParam.defUuid = defUuid
        cbParam.fenzuID = pairsData.fenzuID
        cbParam.pairsIndex = pairsData.pairsIndex
        --print("pairsIndex",pairsData.pairsIndex)
		cbParam.combatInfos = {}
		
		param.atkUuid = atkUuid
		param.defUuid = defUuid

		-- require("common.Util").printTable(atkRoleData)
		-- require("common.Util").printTable(defRoleData)
		CombatLogic.combatBegin(fakeHuman, 1001, param, CombatDefine.COMBAT_TYPE4, cbParam, true)
	end
end

function getCombatObjList(human, side, args)
	--print("getCombatObjList(human, side, args)")
	local uuid = args.atkUuid
	if CombatDefine.DEFEND_SIDE == side then
		uuid = args.defUuid
	end
	
	local data = JjcGodWarDB.getGodWarDataByUuid(uuid)
	if JjcDB.isNpc(data) then return end
	--require("common.Util").printTable(data.rolebase)
	return data.objList, data.helpList, data.rolebase, data.formation, data.jiban, data.elfList
 end 

function getCombatMonsterOutID(human, side, args)
	--print("getCombatMonsterOutID(human, side, args)")
	local uuid = args.atkUuid
	if CombatDefine.DEFEND_SIDE == side then
		uuid = args.defUuid
	end
	
	local data = JjcGodWarDB.getGodWarDataByUuid(uuid)
	--require("common.Util").printTable(data)
	if not JjcDB.isNpc(data) then return end
	--print("data.monsterOutID",data.monsterOutID)
	return data.monsterOutID,data.zhandouli
end

local function xuanbaPointCanGet(winUuid,state)
	if state == JjcGodWarLogic.GODWAR_STATE_XUANBA1 then return 20 end

	local list = JjcGodWarDB.getCombatResultListByUuid(winUuid)
	if not list then return 20 end

	local winCnt = 0
	for lastState=1,state-1 do
		if list[lastState] then
			winCnt = winCnt + 1
		else
			winCnt = 0
		end
	end

	return 20 + winCnt * 10
end

function checkHurtResult(combatInfo)
	local atkHurt = 0
	local defHurt = 0
	for pos = 1, CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = combatInfo.objList and combatInfo.objList[pos]
		if obj then
			if pos <= 10 then
				atkHurt = atkHurt + obj.result[1]
			else
				defHurt = defHurt + obj.result[1]
			end
		end
	end

	if atkHurt < defHurt then
		return true
	end
end

-- 战斗结束
function onFightEnd(human, result, type, cbParam, combatInfo)
	--print("result",result)
	--require("common.Util").printTable(cbParam)
	--require("common.Util").printTable(combatInfo)
	--print("atkFormation,defFormation,atkUuid,defUuid",combatInfo.atkFormation,combatInfo.defFormation,cbParam.atkUuid,cbParam.defUuid)
	local state,round,leftTime = JjcGodWarLogic.getNowGodWarState()
	if state == JjcGodWarLogic.GODWAR_STATE_WAIT or state == JjcGodWarLogic.GODWAR_STATE_END then return end

	if round == JjcGodWarLogic.GODWAR_JIEDUAN_ZHUNBEI then return end

	if combatInfo.result.round >= combatInfo.maxRound then
		if CombatLogic.calcWinByHurt(combatInfo) then
			combatInfo.isWin = true
			result = CombatDefine.RESULT_WIN
		else
			combatInfo.isWin = false
			result = CombatDefine.RESULT_FAIL
		end
	end
	local winUuid = nil
	if CombatDefine.RESULT_WIN == result then --atkuuid胜利
		winUuid = cbParam.atkUuid
	else --defUuid胜利
		winUuid = cbParam.defUuid
	end

	if not winUuid then return end

	--选拔赛需要累计积分 赢+20 连胜额外+10 输不掉分
	if state <= JjcGodWarLogic.GODWAR_STATE_XUANBA6 then
		local winRoleData = JjcGodWarDB.getGodWarDataByUuid(winUuid)
		if winRoleData then
			local point = xuanbaPointCanGet(winUuid,state)

			winRoleData.combatResultList = winRoleData.combatResultList or {}
			winRoleData.combatResultList[state] = {}
			winRoleData.combatResultList[state] = point --本轮战斗获得的积分记录
		end
	end

	--更新战斗配对信息
	local fenzuID = cbParam.fenzuID
	local pairsIndex = cbParam.pairsIndex

	local match = JjcGodWarDB.getMatch2Fight(state)
	match[fenzuID][pairsIndex].winUuid = winUuid
	--保存战斗记录	
	local vestID = JjcGodWarDB.addCombatRecord(cbParam,combatInfo,result)
	match[fenzuID][pairsIndex].vestID = vestID

end