local Msg = require("core.Msg")
local Util = require("common.Util")
local CombatPosLogic = require("combat.CombatPosLogic")
local HeroGrid = require("hero.HeroGrid")
local BeSkill = require("combat.BeSkill")
local CombatDefine = require("combat.CombatDefine")
local JibanExcel = require("excel.jiban")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local HeroLogic = require("hero.HeroLogic")
local GuideLogic = require("guide.GuideLogic")
local CombatExcel = require("excel.combat")

CMD1 = 1	-- 主线通关
CMD2 = 2	-- 获得英雄
CMD3 = 3	-- 通关通天塔
CMD4 = 4	-- 等级
FATHER2ID = nil
HERO2ID = nil
CMD2POS = nil


local function getDB(human)
	human.db.jiban = human.db.jiban or {[1] = 1}
	return human.db.jiban
end

local function fontJibanInfo(net,id)
	local conf = JibanExcel.jiban[id]
	net.id = id
	net.name = conf.name
	net.desc = conf.desc
	net.quality = conf.quality
	local len = 0
	for _,v in ipairs(conf.hero) do
		len = len + 1
		HeroGrid.makeHeroSimpleByID(net.heroList[len], v)
	end
	net.heroList[0] = len
end

function initAfterHot()
	FATHER2ID = {}
	HERO2ID = {}
	local temp = {}


	for k,v in ipairs(JibanExcel.jiban) do
		temp[#temp + 1] = {v.quality,k}
		for _,v1 in ipairs(v.hero) do
			HERO2ID[v1] = HERO2ID[v1] or {}
			HERO2ID[v1][#HERO2ID[v1] + 1] = k
		end
	end

	table.sort(temp,function(a,b)
		if a[1] > b[1] then
			return true
		elseif a[1] == b[1] and a[2] < b[2] then
			return true
		else
			return false
		end
	end)

	local msgRet = Msg.gc.GC_JIBAN_PREVIEW
	local len = 0
	for k,v in ipairs(temp) do
		local conf = JibanExcel.jiban[v[2]]
		FATHER2ID[conf.father] = FATHER2ID[conf.father] or {}
		FATHER2ID[conf.father][#FATHER2ID[conf.father] + 1] = v[2]
		len = len + 1
		local net = msgRet.list[len]
		fontJibanInfo(net,k)
	end
	msgRet.list[0] = len

	CMD2POS = {}
	for k,v in ipairs(JibanExcel.pos) do
		if v.cmd > 0 then
			CMD2POS[v.cmd] = k
		end
	end
end

function query(human,combatType,heroID)
	local flag = RoleSystemLogic.isOpen(human,RoleSystemDefine.ROLE_SYS_ID_207)
	if flag ~= true then
		return
	end

	local msgRet = Msg.gc.GC_JIBAN_QUERY
	local len = 0
	if HERO2ID[heroID] then
		for k,v in ipairs(HERO2ID[heroID]) do
			len = len + 1
			local net = msgRet.jibanList[len]
			fontJibanInfo(net,v)
		end
	end
	msgRet.jibanList[0] = len
	len = 0
	local db = getDB(human)
	local combatHeroDB = CombatPosLogic.getCombatHeroDB(human,combatType)
	local jibanDB = combatHeroDB.jiban or {}
	for k,v in ipairs(JibanExcel.pos) do
		len = len + 1
		local net = msgRet.posList[len]
		net.id = k
		net.tip = v.tip
		net.isOpen = db[k] or 0
        net.sort = v.sort
		local heroGrid
		if jibanDB[k] and jibanDB[k] ~= "" and jibanDB[k] ~= "0" then
			heroGrid = HeroLogic.getHeroGridByUuid(human, jibanDB[k])
		end
        index = heroGrid and heroGrid.bagIndex or 0
		HeroGrid.makeHeroSimple(net.hero, heroGrid, index)
	end
	msgRet.posList[0] = len
	Msg.send(msgRet,human.fd)
end

--
function sendQuery(human,combatType, teamType)
	local msgRet = Msg.gc.GC_JIBAN_QUERY_ALL
	local len = 0
	local db = getDB(human)
    msgRet.type = teamType
	local combatHeroDB = CombatPosLogic.getCombatHeroDB(human,combatType)
	local jibanDB = combatHeroDB.jiban or {}
	for k,v in ipairs(JibanExcel.pos) do
		len = len + 1
		local net = msgRet.posList[len]
		net.id = k
		net.tip = v.tip
		net.isOpen = db[k] or 0
        net.sort = v.sort
		local heroGrid
		if jibanDB[k] and jibanDB[k] ~= "" and jibanDB[k] ~= "0" then
			heroGrid = HeroLogic.getHeroGridByUuid(human, jibanDB[k])
		end
        index = heroGrid and heroGrid.bagIndex or 0
		HeroGrid.makeHeroSimple(net.hero, heroGrid, index)
	end
	msgRet.posList[0] = len
	Msg.send(msgRet,human.fd)
end

function preview(human)
	local msgRet = Msg.gc.GC_JIBAN_PREVIEW
	Msg.send(msgRet,human.fd)
end

function getPosList(human)
	return getDB(human)
end

function onCallback(human,cmd,arg)
	local pos = CMD2POS[cmd]
	local db = getDB(human)
	if db[pos] then
		return
	end
	local conf = JibanExcel.pos[pos]
	if arg >= conf.arg then
		db[pos] = 1
	end
end

function getJiban(objList,jibanList)
	local objHero = {}
	local jibanHero = {}
	for i = 1,CombatDefine.COMBAT_HERO_CNT - 1 do
		local obj = objList[i]
        -- 不是玩家不计算羁绊
		if obj and obj.type == CombatDefine.COMBAT_OBJ_TYPE1 then
			objHero[obj.id] = objHero[obj.id] or 0
			objHero[obj.id] = objHero[obj.id] + 1
		end
	end
	if jibanList then
		for k,v in pairs(jibanList) do
			if v > 0 then
				objHero[v] = objHero[v] or 0
				objHero[v] = objHero[v] + 1
			end
		end
	end
	local jiban = {}
	for _,v in pairs(FATHER2ID) do
		for _,v1 in ipairs(v) do
			local conf = JibanExcel.jiban[v1]
			local canUse = true
			for _,v2 in ipairs(conf.hero) do
				if not objHero[v2] then
					canUse = false
					break
				end
			end
			if canUse then
				jiban[v1] = 1
				break
			end
		end
	end
	return jiban
end

function getJibanHero(human,combatHeroDB)
	local jibanHero = {}
	if combatHeroDB.jiban then
		for k, uuid in pairs(combatHeroDB.jiban) do
			if uuid ~= "0" and uuid ~= "" then
				heroGrid = HeroLogic.getHeroGridByUuid(human, uuid)
				if heroGrid then
					jibanHero[k] =  heroGrid.id
				end
			end
		end
	end
	return jibanHero
end

function getDesc(jiban)
    local desc = {}
    local len = 0
	for k,v in pairs(jiban) do
		local conf = JibanExcel.jiban[k]
        if len > 0 then
           len = len + 1
           desc[len] = "|"
        end
        len = len + 1
        desc[len] = conf.name
        len = len + 1
        desc[len] = ","
        len = len + 1
        desc[len] = conf.desc
        len = len + 1
        desc[len] = ","
        len = len + 1
        desc[len] = conf.quality
	end

    local descStr = table.concat(desc)
	return descStr
end


function setBeSkill(objList,jiban)
	if not jiban then return end
	local skillList = {}
	for k in pairs(jiban) do
		local conf = JibanExcel.jiban[k]
		local skillID = conf.skill
		if skillID > 0 then
			for i = 1,CombatDefine.COMBAT_HERO_CNT - 1 do
				local obj = objList[i]
				if obj then
					BeSkill.setBeSkillID(obj,skillID)
				end
			end
		end
	end
end

function update(human,combatType,jibanListStr)
    local conf = CombatExcel.combat[combatType]
    if not conf then return end

	local posList = getPosList(human)
	local jibanList = Util.split(jibanListStr,",")
	local combatHeroDB = CombatPosLogic.getCombatHeroDB(human,combatType)
	for k,v in ipairs(jibanList) do
		local uuid = v or "0"
		if uuid ~= "" and uuid ~= "0" then
            --检测有没有再阵容上
            for _, combatUuid in ipairs(combatHeroDB.list) do
               if uuid == combatUuid then return end
            end

			if not posList[k] then return end
			local heroGrid = HeroLogic.getHeroGridByUuid(human, uuid)
            if not heroGrid then return end
		end
	end
	combatHeroDB.jiban = jibanList
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_JIBAN_GROUP)

    sendQuery(human,combatType, conf.teamType)
end