-- 碎片逻辑
local ItemExcel = require("excel.item")
local DropExcel = require("excel.drop")
local HeroExcel = require("excel.hero")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local HeroGrid = require("hero.HeroGrid")
local HeroDefine = require("hero.HeroDefine")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local DropSpecial = require("bag.DropSpecial")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")


SUMMON_TYPE_TARGET    = 1 -- 指定目标兑换
SUMMON_TYPE_DROP      = 2 -- 随机掉落表兑换
SUMMON_TYPE_HERO_SP   = 3 -- 英雄条件兑换
SUMMON_TYPE_SHENQI_SP = 4 -- 神器条件兑换

SUMMON_SUBTYPE_MIN = 1 
SUMMON_SUBTYPE_HERO = 1 -- 英雄
SUMMON_SUBTYPE_EQUIP = 2 -- 装备
SUMMON_SUBTYPE_SKIN = 3  -- 皮肤
SUMMON_SUBTYPE_TALISAM = 4 --秘宝
SUMMON_SUBTYPE_RELIC = 5  --遗物
SUMMON_SUBTYPE_ELF = 6  -- 精灵

SUMMON_SUBTYPE_MAX = 6

SUMMON_HECHENG_TYPE_GOOD = 11 --可合成物品

function SuipianLogic_CanHeCheng(nID)
	if nID == SUMMON_SUBTYPE_HERO or nID == SUMMON_SUBTYPE_SKIN  or SUMMON_SUBTYPE_TALISAM == nID 
		or SUMMON_SUBTYPE_RELIC == nID or SUMMON_SUBTYPE_ELF == nID then
			return true
	end
	
	return false
end

function SuipianLogic_CanDuiHuang(nID)
	return SUMMON_HECHENG_TYPE_GOOD == nID
end

-- 查询
function query(human, itemID)
	local itemConfig = ItemExcel.item[itemID]
	if itemConfig == nil then return end
	if itemConfig.get[1] ~= SUMMON_TYPE_TARGET then
		return
	end 
	if itemConfig.get[2] ~= SUMMON_SUBTYPE_HERO then
		return
	end 
	
	local heroID = itemConfig.get[3]
	local heroGrid = HeroGrid.getCacheHeroGridTujian(heroID)
	if not heroGrid then return end

	local msgRet = Msg.gc.GC_SUIPIAN_HERO_QUERY
	msgRet.id = itemID
	HeroGrid.makeHeroSimple(msgRet.heroSimple, heroGrid, nil, human, nil, true)	
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)	
end

-- 合成
function summon(human, itemID, itemUseCnt)
	if human.db.bag[itemID] == nil or human.db.bag[itemID] < itemUseCnt then
		return
	end
	
	local itemConfig = ItemExcel.item[itemID]
	if itemConfig == nil then
		return
	end
	
	if itemConfig.get[1] ~= SUMMON_TYPE_TARGET 
		and itemConfig.get[1] ~= SUMMON_TYPE_DROP 
		and itemConfig.get[1] ~= SUMMON_TYPE_HERO_SP 
		and itemConfig.get[1] ~= SUMMON_TYPE_SHENQI_SP then
		return
	end

	if itemConfig.get[2] < SUMMON_SUBTYPE_MIN or SUMMON_SUBTYPE_MAX < itemConfig.get[2] then
		return
	end
	
	local leftHeroBox =  HeroLogic.getEmptyCnt(human)
	
    -- if itemConfig.get[2] ~= SUMMON_SUBTYPE_EQUIP then
    --     if leftHeroBox <= 0 then
	-- 		return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
    --     end
    -- end

	--只有英雄碎片合成才判断英雄背包格子是否足够
	if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
        if leftHeroBox <= 0 then
			return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
        end
    end

	local realItemCnt = math.floor(itemUseCnt/itemConfig.fullCnt)
	if realItemCnt < 1 then
		return
	end
	if realItemCnt * itemConfig.fullCnt ~= itemUseCnt then
		return
	end

	local isHero = 0
	local outItemIDList = nil
	local targetID = nil
	if itemConfig.get[1] == SUMMON_TYPE_TARGET then
		--如果是合成英雄才这么处理
		if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
			local leftHeroBox =  HeroLogic.getEmptyCnt(human)
			if realItemCnt > leftHeroBox then
				realItemCnt = leftHeroBox
			end
		end
		-- 指定目标
		targetID = itemConfig.get[3]
	elseif itemConfig.get[1] == SUMMON_TYPE_DROP then
		--如果是合成英雄才这么处理
		if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
			local leftHeroBox =  HeroLogic.getEmptyCnt(human)
			if realItemCnt > leftHeroBox then
				realItemCnt = leftHeroBox
			end
		end

		-- 掉落表随机掉落
		for i = 1, realItemCnt do
			local dropID = itemConfig.get[3]
			targetID = getDropTargetID(dropID)
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	elseif itemConfig.get[1] == SUMMON_TYPE_HERO_SP then
        --如果是合成英雄才这么处理
		if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
			local leftHeroBox = HeroLogic.getEmptyCnt(human)
			if realItemCnt > leftHeroBox then
				realItemCnt = leftHeroBox
			end
		end

		-- 英雄条件兑换
		local dropTable = DropSpecial.getDropHeroTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3])
		for i = 1, realItemCnt do
			local randTemp = math.random(1, #dropTable)
			targetID = dropTable[randTemp]
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	elseif itemConfig.get[1] == SUMMON_TYPE_SHENQI_SP then
		-- 神器条件兑换
		local dropTable = DropSpecial.getDropEquipTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3], itemConfig.get[3][4])
		for i = 1, realItemCnt do
			local randTemp = math.random(1, #dropTable)
			targetID = dropTable[randTemp]
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	end
	
	if targetID == nil and outItemIDList == nil then
		assert(nil)
	end
    local realSuiPianCnt = realItemCnt * itemConfig.fullCnt
	BagLogic.delItem(human, itemID, realSuiPianCnt, "item_summon")
	
	local fenJieList = nil
	local itemList = {}
	if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
		isHero = 1
		if outItemIDList then
			-- 多个id
			for k, v in pairs(outItemIDList) do
				local _, fjlist = HeroLogic.addHero(human, k,nil, v, "item_summon")
                local heroID = k
                local hero = HeroExcel.hero[heroID]
                if fjlist and type(fjlist) == "table" then
                	for fjItemID, fjItemCnt in pairs(fjlist) do
                		fenJieList = fenJieList or {}
                		fenJieList[fjItemID] = (fenJieList[fjItemID] or 0) + fjItemCnt
                	end
                end
			end
		else
			-- 单个id
			local _, fjlist = HeroLogic.addHero(human, targetID,nil, realItemCnt, "item_summon")
            local heroID = targetID
            local hero = HeroExcel.hero[heroID]
            if fjlist and type(fjlist) == "table" then
            	for fjItemID, fjItemCnt in pairs(fjlist) do
            		fenJieList = fenJieList or {}
            		fenJieList[fjItemID] = (fenJieList[fjItemID] or 0) + fjItemCnt
            	end
            end
		end
	else
		if outItemIDList then
			-- 多个id
			for k, v in pairs(outItemIDList) do
				BagLogic.addItem(human, k, v, "item_summon")
				itemList[k] = itemList[k] or 0
				itemList[k] = itemList[k] + v
			end
		else
			-- 单个id
			BagLogic.addItem(human, targetID, realItemCnt, "item_summon")
			itemList[targetID] = itemList[targetID] or 0
			itemList[targetID] = itemList[targetID] + realItemCnt
		end
	end
	
	BagLogic.sendItemGetList(human, itemList, "item_summon")
	local msgRet = Msg.gc.GC_SUIPIAN_SUMMON
	msgRet.isHero = isHero
	msgRet.list[0] = 0
	msgRet.heroList[0] = 0
	msgRet.fenJieList[0] = 0

	local netList = (isHero == 1) and msgRet.heroList or msgRet.list
	
	if outItemIDList then
		-- 多个id
		for k, v in pairs(outItemIDList) do
			netList[0] = netList[0] + 1
			makeResultItemData(netList[netList[0]], k, v, isHero)
		end
	else
		netList[0] = netList[0] + 1
		makeResultItemData(netList[netList[0]], targetID, realItemCnt, isHero)
	end
	if fenJieList then
		for fjItemID, fjItemCnt in pairs(fenJieList) do
			msgRet.fenJieList[0] = msgRet.fenJieList[0] + 1
			Grid.makeItem(msgRet.fenJieList[msgRet.fenJieList[0]], fjItemID, fjItemCnt)
		end
	end

	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

function makeResultItemData(net, id, cnt, isHero)
	if isHero == 1 then
		HeroGrid.makeHeroSimpleByID(net, id)
		net.cnt = cnt
	else
		--Grid.makeItem(net, id, cnt)
		Grid.makeItem(net, id, cnt, nil,nil,nil, Grid.getOpflagAtBag(id))
	end
end

-- 根据掉落id 获取id
function getDropTargetID(dropID)
	local dropConfig = DropExcel.dropSuipian[dropID]
	if dropConfig == nil then
		return
	end
	
	-- 计算总权重
	local totalWeight = 0
    for k, v in ipairs(dropConfig.dropRule) do
        local weight = v[2]
	    totalWeight = totalWeight + weight
    end

    local heroWeight = nil
	local r = math.random(1, totalWeight)
	for k, v in ipairs(dropConfig.dropRule) do
	    local weight = v[2]
		if r <= weight then
		   heroWeight = v[1]
		   break
		else
		    r = r - weight
		end
	end
	
	if heroWeight == nil then
	   assert()
	end

    local len = #dropConfig.camp
    local index = math.random(1, len)
    local heroID  = HeroDefine.getRandHeroByWeightLvAndCamp(heroWeight, dropConfig.camp[index])
    return heroID
end

local function queryBoxList(human, itemConfig)
	local itemList = itemConfig.cmd[2]
	if not itemList then return end

	local msgRet = Msg.gc.GC_SUIPIAN_ITEM_SUMMON_QUERY
	msgRet.items[0] = #itemList
    for i = 1, msgRet.items[0] do
    	local itemID = itemList[i][1]
    	local itemCnt = itemList[i][2]
        local quality = itemList[i][3]
        Grid.makeItem(msgRet.items[i], itemID, itemCnt, nil, nil, nil, nil, quality)
    end 
	Msg.send(msgRet, human.fd)
end

--碎片英雄召唤预览
function CG_SUIPIAN_HERO_SUMMON_QUERY(human, itemID)
	local itemConfig = ItemExcel.item[itemID]
	if not itemConfig then return end
    
    local cmdStr = itemConfig.cmd[1]
	if cmdStr == "box" or cmdStr == "boxAll" then
		return queryBoxList(human, itemConfig) 
	end

	if itemConfig.get[1] ~= SUMMON_TYPE_TARGET 
		and itemConfig.get[1] ~= SUMMON_TYPE_DROP 
		and itemConfig.get[1] ~= SUMMON_TYPE_HERO_SP 
		and itemConfig.get[1] ~= SUMMON_TYPE_SHENQI_SP then	
		return
	end
	if itemConfig.get[2] ~= SUMMON_SUBTYPE_HERO and itemConfig.get[2] ~= SUMMON_SUBTYPE_EQUIP then
		return
	end
	
	local realItemCnt = 1
	if human.db.bag[itemID] ~= nil then
		realItemCnt = math.floor(human.db.bag[itemID])
	end
	
	if realItemCnt < 1 then
		return
	end
   
	local outItemIDList = nil
	local targetID = nil
	if itemConfig.get[1] == SUMMON_TYPE_TARGET then
		-- 指定目标
		targetID = itemConfig.get[3]
	elseif itemConfig.get[1] == SUMMON_TYPE_DROP then
        local dropID = itemConfig.get[3]
        local dropConfig = DropExcel.dropSuipian[dropID]
		if dropConfig == nil then
			return
		end

        local totalCnt = #dropConfig.dropRule
		local heroWeight
       
        local weightLvAndCampHeros = nil
        for i=1,totalCnt do
			targetID = dropConfig.dropRule[i]
            heroWeight = targetID[1]

            for _, camp in ipairs(dropConfig.camp) do
               weightLvAndCampHeros = HeroDefine.getHeroByWeightLvAndCamp(heroWeight, camp)
               for k, v in pairs(weightLvAndCampHeros) do 
			      outItemIDList = outItemIDList or {}
			      outItemIDList[v] = outItemIDList[v] or 0
			      outItemIDList[v] = outItemIDList[v] + 1
			   end
            end
		end
	elseif itemConfig.get[1] == SUMMON_TYPE_HERO_SP then
    
		-- 英雄条件兑换
		local dropTable = DropSpecial.getDropHeroTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3])
		for i = 1, realItemCnt do
			local randTemp = math.random(1, #dropTable)
			targetID = dropTable[randTemp]
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	elseif itemConfig.get[1] == SUMMON_TYPE_SHENQI_SP then
		-- 神器条件兑换
		local dropTable = DropSpecial.getDropEquipTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3], itemConfig.get[3][4])
		for i = 1, realItemCnt do
			local randTemp = math.random(1, #dropTable)
			targetID = dropTable[randTemp]
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	end
	
	if targetID == nil and outItemIDList == nil then
		assert(nil)
	end
        
	if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
		local msgRet = Msg.gc.GC_SUIPIAN_HERO_SUMMON_QUERY
		if outItemIDList then
			local len = 0
			for heroID, _ in pairs(outItemIDList) do
				if heroID and HeroExcel.hero[heroID] == nil then
					assert(nil, "why heroID no config " .. heroID .. " " .. itemID)
				end

				if len >= 50 then
					break
				end

		        len = len + 1
		        HeroGrid.makeHeroSimpleByID(msgRet.heroSimple[len], heroID)
		    end

		    msgRet.heroSimple[0] = len
		else
			local heroID = targetID
			msgRet.heroSimple[0] = 1
			HeroGrid.makeHeroSimpleByID(msgRet.heroSimple[1], heroID)
		end
		Msg.send(msgRet, human.fd)		
	else
		local msgRet = Msg.gc.GC_SUIPIAN_ITEM_SUMMON_QUERY
		local cnt = 0
		if outItemIDList then
		    for k, v in pairs(outItemIDList) do
		        cnt = cnt + 1
		        Grid.makeItem(msgRet.items[cnt], k, v)
		    end

		    if cnt > 0 then
				msgRet.items[0] = cnt
				Msg.send(msgRet, human.fd)
			end
		else
			msgRet.items[0] = 1
			Grid.makeItem(msgRet.items[1], targetID, 1)
			Msg.send(msgRet, human.fd)
		end
	end
end


local HEROID_2_SUIPIANID = nil
function getSuipianIDbyHeroID(heroID)
	if not HEROID_2_SUIPIANID then
		HEROID_2_SUIPIANID = {}
		for itemID, itemConfig in pairs(ItemExcel.item) do
			if itemConfig.get[1] == SUMMON_TYPE_TARGET and 
				itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
				local targetID = itemConfig.get[3]
				HEROID_2_SUIPIANID[targetID] = itemID
			end
		end
	end
	return HEROID_2_SUIPIANID[heroID]
end

-- 碎片合成
-- itemID 合成消耗物品ID
-- itemUseCnt 合成消耗物品当前数量
-- function SUIPIAN_SYNTHESIS(human, itemID, itemUseCnt)
-- 	if human.db.bag[itemID] == nil or human.db.bag[itemID] < itemUseCnt then
-- 		return
-- 	end
	
-- 	local itemConfig = ItemExcel.item[itemID]
-- 	if itemConfig == nil then
-- 		return
-- 	end

-- 	local leftHeroBox =  HeroLogic.getEmptyCnt(human)	
--     if itemConfig.get[2] ~= SUMMON_SUBTYPE_EQUIP then
--         if leftHeroBox <= 0 then
--            return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
--         end
--     end

-- 	local realItemCnt = math.floor(itemUseCnt/itemConfig.fullCnt)
-- 	if realItemCnt < 1 then
-- 		return
-- 	end

-- 	local isHero = 0
-- 	local outItemIDList = nil
-- 	local targetID = nil
-- 	if itemConfig.get[1] == SUMMON_TYPE_TARGET then
--         --local leftHeroBox =  HeroLogic.getEmptyCnt(human)
--         if realItemCnt > leftHeroBox then
--             realItemCnt = leftHeroBox
--         end
-- 		-- 指定目标
-- 		targetID = itemConfig.get[3]
-- 	elseif itemConfig.get[1] == SUMMON_TYPE_DROP then   
-- 		if itemConfig.get[2] ~= SUMMON_SUBTYPE_EQUIP then
-- 			--local leftHeroBox =  HeroLogic.getEmptyCnt(human)
-- 			if realItemCnt > leftHeroBox then
-- 				realItemCnt = leftHeroBox
-- 			end
-- 		end
-- 		-- 掉落表随机掉落
-- 		for i = 1, realItemCnt do
-- 			local dropID = itemConfig.get[3]
-- 			targetID = getDropTargetID(dropID)
-- 			if nil == targetID then
-- 				print("[SUIPIAN_SYNTHESIS] 获取目标ID失败 dropID = "..dropID)
-- 				return
-- 			end

-- 			outItemIDList = outItemIDList or {}
-- 			outItemIDList[targetID] = outItemIDList[targetID] or 0
-- 			outItemIDList[targetID] = outItemIDList[targetID] + 1
-- 		end
-- 	elseif itemConfig.get[1] == SUMMON_TYPE_HERO_SP then
--         --local leftHeroBox =  HeroLogic.getEmptyCnt(human)
--         if realItemCnt > leftHeroBox then
--             realItemCnt = leftHeroBox
--         end
-- 		local dropTable = DropSpecial.getDropHeroTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3])
-- 		for i = 1, realItemCnt do
-- 			local randTemp = math.random(1, #dropTable)
-- 			targetID = dropTable[randTemp]
-- 			outItemIDList = outItemIDList or {}
-- 			outItemIDList[targetID] = outItemIDList[targetID] or 0
-- 			outItemIDList[targetID] = outItemIDList[targetID] + 1
-- 		end
-- 	elseif itemConfig.get[1] == SUMMON_TYPE_SHENQI_SP then
-- 		local dropTable = DropSpecial.getDropEquipTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3], itemConfig.get[3][4])
-- 		for i = 1, realItemCnt do
-- 			local randTemp = math.random(1, #dropTable)
-- 			targetID = dropTable[randTemp]
-- 			outItemIDList = outItemIDList or {}
-- 			outItemIDList[targetID] = outItemIDList[targetID] or 0
-- 			outItemIDList[targetID] = outItemIDList[targetID] + 1
-- 		end
-- 	end	
-- 	if targetID == nil and outItemIDList == nil then
-- 		assert(nil)
-- 	end

--     local realSuiPianCnt = realItemCnt * itemConfig.fullCnt
-- 	BagLogic.delItem(human, itemID, realSuiPianCnt, "item_summon")
-- 	print("[SUIPIAN_SYNTHESIS] 实际上删除的碎片数量 realItemCnt = "
-- 			..realItemCnt.. " realSuiPianCnt = "..realSuiPianCnt.. " itemUseCnt = "..itemUseCnt)

-- 	local fenJieList = nil
-- 	local itemList = {}
-- 	if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
-- 		isHero = 1
-- 		if outItemIDList then
-- 			-- 多个id
-- 			for k, v in pairs(outItemIDList) do
-- 				local _, fjlist = HeroLogic.addHero(human, k,nil, v, "item_summon")
--                 local heroID = k
--                 local hero = HeroExcel.hero[heroID]
--                 if fjlist and type(fjlist) == "table" then
--                 	for fjItemID, fjItemCnt in pairs(fjlist) do
--                 		fenJieList = fenJieList or {}
--                 		fenJieList[fjItemID] = (fenJieList[fjItemID] or 0) + fjItemCnt
--                 	end
--                 end
-- 			end
-- 		else
-- 			-- 单个id
-- 			local _, fjlist = HeroLogic.addHero(human, targetID,nil, realItemCnt, "item_summon")
--             local heroID = targetID
--             local hero = HeroExcel.hero[heroID]
--             if fjlist and type(fjlist) == "table" then
--             	for fjItemID, fjItemCnt in pairs(fjlist) do
--             		fenJieList = fenJieList or {}
--             		fenJieList[fjItemID] = (fenJieList[fjItemID] or 0) + fjItemCnt
--             	end
--             end
-- 		end
-- 	else
-- 		if outItemIDList then
-- 			-- 多个id
-- 			for k, v in pairs(outItemIDList) do
-- 				BagLogic.addItem(human, k, v, "item_summon")
-- 				itemList[k] = itemList[k] or 0
-- 				itemList[k] = itemList[k] + v
-- 			end
-- 		else
-- 			-- 单个id
-- 			BagLogic.addItem(human, targetID, realItemCnt, "item_summon")
-- 			itemList[targetID] = itemList[targetID] or 0
-- 			itemList[targetID] = itemList[targetID] + realItemCnt
-- 		end
-- 	end
	
-- 	BagLogic.sendItemGetList(human, itemList, "item_summon")
-- 	local msgRet = Msg.gc.GC_SUIPIAN_SUMMON
-- 	msgRet.isHero = isHero
-- 	msgRet.list[0] = 0
-- 	msgRet.heroList[0] = 0
-- 	msgRet.fenJieList[0] = 0

-- 	local netList = (isHero == 1) and msgRet.heroList or msgRet.list
	
-- 	if outItemIDList then
-- 		-- 多个id
-- 		for k, v in pairs(outItemIDList) do
-- 			netList[0] = netList[0] + 1
-- 			makeResultItemData(netList[netList[0]], k, v, isHero)
-- 			print("[SUIPIAN_SYNTHESIS] 合成的数量 英雄ID k = "
-- 					..k .. " v ="..v.." cnt = "..netList[netList[0]].cnt)
-- 		end
-- 	else
-- 		netList[0] = netList[0] + 1
-- 		makeResultItemData(netList[netList[0]], targetID, realItemCnt, isHero)
-- 		print("[SUIPIAN_SYNTHESIS] 合成的数量 英雄ID targetID = "
-- 				..targetID .." realItemCnt = "..realItemCnt.. " cnt = "..netList[netList[0]].cnt)
-- 	end
-- 	if fenJieList then
-- 		for fjItemID, fjItemCnt in pairs(fenJieList) do
-- 			msgRet.fenJieList[0] = msgRet.fenJieList[0] + 1
-- 			Grid.makeItem(msgRet.fenJieList[msgRet.fenJieList[0]], fjItemID, fjItemCnt)
-- 		end
-- 	end

-- 	--Msg.trace(msgRet)
-- 	Msg.send(msgRet, human.fd)
-- end

-- 通用合成碎片具体逻辑
function Suipan_BeginSyntheticFragments(human, nItemID, nItemNum, tMsgData)
	if human.db.bag[nItemID] == nil or human.db.bag[nItemID] < nItemNum then
		return false
	end
	
	local itemConfig = ItemExcel.item[nItemID]
	if itemConfig == nil then
		return false
	end

	local leftHeroBox =  HeroLogic.getEmptyCnt(human)
    if itemConfig.get[2] ~= SUMMON_SUBTYPE_EQUIP then
        if leftHeroBox <= 0 and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN then
        	return false
        end
    end

	local realItemCnt = math.floor(nItemNum/itemConfig.fullCnt)
	if realItemCnt < 1 then
		return false
	end

	local isHero = 0
	local outItemIDList = nil
	local targetID = nil
	if itemConfig.get[1] == SUMMON_TYPE_TARGET then
        if realItemCnt > leftHeroBox and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN then
            realItemCnt = leftHeroBox
        end
		-- 指定目标
		targetID = itemConfig.get[3]
	elseif itemConfig.get[1] == SUMMON_TYPE_DROP then
		if itemConfig.get[2] ~= SUMMON_SUBTYPE_EQUIP then
			if realItemCnt > leftHeroBox and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN then
				realItemCnt = leftHeroBox
			end
		end
		-- 掉落表随机掉落
		for i = 1, realItemCnt do
			local dropID = itemConfig.get[3]
			targetID = getDropTargetID(dropID)
			if nil == targetID then
				print("[Suipan_BeginSyntheticFragments] 获取目标ID失败 dropID = "..dropID)
				return false
			end

			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	elseif itemConfig.get[1] == SUMMON_TYPE_HERO_SP then
        if realItemCnt > leftHeroBox and itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN then
            realItemCnt = leftHeroBox
        end
		local dropTable = DropSpecial.getDropHeroTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3])
		for i = 1, realItemCnt do
			local randTemp = math.random(1, #dropTable)
			targetID = dropTable[randTemp]
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	elseif itemConfig.get[1] == SUMMON_TYPE_SHENQI_SP then
		local dropTable = DropSpecial.getDropEquipTable(itemConfig.get[3][1], itemConfig.get[3][2], itemConfig.get[3][3], itemConfig.get[3][4])
		for i = 1, realItemCnt do
			local randTemp = math.random(1, #dropTable)
			targetID = dropTable[randTemp]
			outItemIDList = outItemIDList or {}
			outItemIDList[targetID] = outItemIDList[targetID] or 0
			outItemIDList[targetID] = outItemIDList[targetID] + 1
		end
	end	

	if targetID == nil and outItemIDList == nil then
		-- assert(nil)
		return false
	end

    local realSuiPianCnt = realItemCnt * itemConfig.fullCnt
	BagLogic.delItem(human, nItemID, realSuiPianCnt, "item_summon")
	print("[Suipan_BeginSyntheticFragments] 实际上删除的碎片数量 realItemCnt = "
			..realItemCnt.. " itemID" .. nItemID.. " realSuiPianCnt = "..realSuiPianCnt.. " nItemNum = "..nItemNum)

	local fenJieList = nil
	local itemList = {}
	if itemConfig.get[2] == SUMMON_SUBTYPE_HERO then
		isHero = 1
		if outItemIDList then
			-- 多个id
			for k, v in pairs(outItemIDList) do
				local _, fjlist = HeroLogic.addHero(human, k,nil, v, "item_summon")
                local heroID = k
                local hero = HeroExcel.hero[heroID]
                if fjlist and type(fjlist) == "table" then
                	for fjItemID, fjItemCnt in pairs(fjlist) do
                		fenJieList = fenJieList or {}
                		fenJieList[fjItemID] = (fenJieList[fjItemID] or 0) + fjItemCnt
                	end
                end
			end
		else
			-- 单个id
			local _, fjlist = HeroLogic.addHero(human, targetID,nil, realItemCnt, "item_summon")
            local heroID = targetID
            local hero = HeroExcel.hero[heroID]
            if fjlist and type(fjlist) == "table" then
            	for fjItemID, fjItemCnt in pairs(fjlist) do
            		fenJieList = fenJieList or {}
            		fenJieList[fjItemID] = (fenJieList[fjItemID] or 0) + fjItemCnt
            	end
            end
		end
	else
		if outItemIDList then
			-- 多个id
			for k, v in pairs(outItemIDList) do
				BagLogic.addItem(human, k, v, "item_summon")
				itemList[k] = itemList[k] or 0
				itemList[k] = itemList[k] + v
			end
		else
			-- 单个id
			BagLogic.addItem(human, targetID, realItemCnt, "item_summon")
			itemList[targetID] = itemList[targetID] or 0
			itemList[targetID] = itemList[targetID] + realItemCnt
		end
	end
	
	BagLogic.sendItemGetList(human, itemList, "item_summon")

	local netList = (isHero == 1) and tMsgData.heroList or tMsgData.list
	
	if outItemIDList then
		-- 多个id
		for k, v in pairs(outItemIDList) do
			netList[0] = netList[0] + 1
			makeResultItemData(netList[netList[0]], k, v, isHero)
			print("[Suipan_BeginSyntheticFragments] 合成的数量 道具ID k = "
					..k .. " v ="..v.." cnt = "..netList[netList[0]].cnt)
		end
	else
		netList[0] = netList[0] + 1
		makeResultItemData(netList[netList[0]], targetID, realItemCnt, isHero)
		print("[Suipan_BeginSyntheticFragments] 合成的数量 道具ID targetID = "
				..targetID .." realItemCnt = "..realItemCnt.. " cnt = "..netList[netList[0]].cnt)
	end
	if fenJieList then
		for fjItemID, fjItemCnt in pairs(fenJieList) do
			tMsgData.fenJieList[0] = tMsgData.fenJieList[0] + 1
			Grid.makeItem(tMsgData.fenJieList[tMsgData.fenJieList[0]], fjItemID, fjItemCnt)
		end
	end

	return true
	--Msg.trace(msgRet)
	--Msg.send(msgRet, human.fd)
end

-- 碎片合成
function SUIPIAN_SYNTHESIS(human, tCompositeTable, isHaveHero)
	if not human or nil == tCompositeTable or nil == _G.next(tCompositeTable) then
		return false
	end

	-- 英雄背包空余格子数
	local nLefeHeroNum = HeroLogic.getEmptyCnt(human)
	if 0 >= nLefeHeroNum and isHaveHero then
		Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
		print("[SUIPIAN_SYNTHESIS] 背包格子不够直接返回 nLefeHeroNum = "..nLefeHeroNum)
		return false
	end

	-- 检查一次数量信息
	local nAllnum = 0
	for nID, nNum in pairs(tCompositeTable) do
		if human.db.bag[nID] == nil or human.db.bag[nID] < nNum then
			print("[SUIPIAN_SYNTHESIS] 背包中数量不正确，无法合成直接返回 nID = "..nID.." nNum = "..nNum)
			return false
		end

		local itemConfig = ItemExcel.item[nID]
		if itemConfig == nil then
			print("[SUIPIAN_SYNTHESIS] 不存在对应物品的配置 nID = "..nID.." nNum = "..nNum)
			return false
		end

		local nRealNum = math.floor(nNum / itemConfig.fullCnt)
		if 0 >= nRealNum then
			print("[SUIPIAN_SYNTHESIS] 真实生成的数量不正确 nID = "
					..nID.." nNum = "..nNum.." fullCnt = "..itemConfig.fullCnt)
			return false
		end

		print("[SUIPIAN_SYNTHESIS] 进行合成的碎片信息 nID = "..nID.." nNum = "..nNum)
		nAllnum = nAllnum + 1
	end

	if nAllnum >= 100 then
		print("[SUIPIAN_SYNTHESIS] 真实生成的数量大于了协议生成最大长度 nAllnum = "..nAllnum)
		return false
	end

	local msgRet = Msg.gc.GC_SUIPIAN_SUMMON
	msgRet.isHero = isHaveHero and 1 or 0
	msgRet.list[0] = 0
	msgRet.heroList[0] = 0
	msgRet.fenJieList[0] = 0

	-- 正式开始正式生成
	for nID, nNum in pairs(tCompositeTable) do
		-- 不存在剩余空位
		if 0 >= nLefeHeroNum and isHaveHero then
			break
		end

		local itemConfig = ItemExcel.item[nID]
		if itemConfig then
			if isHaveHero then
				local nCanCreateNum = math.floor(nNum / itemConfig.fullCnt)
				if nCanCreateNum > nLefeHeroNum then
					nCanCreateNum = nLefeHeroNum
				end
				
				nLefeHeroNum = nLefeHeroNum - nCanCreateNum
			end

			local bRet = Suipan_BeginSyntheticFragments(human, nID, nNum, msgRet)
			if false == bRet then
				print("[SUIPIAN_SYNTHESIS] 合成碎片失败 nID = "..nID.." nNum = "..nNum)
				break 
			end
		else
			print("[SUIPIAN_SYNTHESIS] 检测过居然还不存在对应物品的配置 nID = "..nID.." nNum = "..nNum)
		end
	end
	Msg.send(msgRet, human.fd)
end



function CG_SUIPIAN_SYNTHESIS(human, synthesisType)
	if not synthesisType or synthesisType < SUMMON_SUBTYPE_HERO  or synthesisType > SUMMON_SUBTYPE_ELF then
		print("[CG_SUIPIAN_SYNTHESIS] 合成类型错误 synthesisType = "..synthesisType)
		return
	end

	--目前只有三种碎片能合成
	if synthesisType ~= SUMMON_SUBTYPE_HERO and 
		synthesisType ~= SUMMON_SUBTYPE_TALISAM and
		synthesisType ~= SUMMON_SUBTYPE_RELIC and 
		synthesisType ~= SUMMON_SUBTYPE_ELF then
		print("[CG_SUIPIAN_SYNTHESIS] 合成类型错误 synthesisType = "..synthesisType)
		return
	end


	local msgRet = Msg.gc.GC_SUIPIAN_SYNTHESIS
	local status = 1
	local isHaveHero = false
	local tCompositeTable = {}
	-- for itemID,itemCnt in pairs(human.db.bag) do
	-- 	local itemConfig = ItemExcel.item[itemID]
	-- 	if itemConfig ~= nil and
	-- 	   itemConfig.mainType == ItemDefine.MAINTYPE_ITEM and 
	-- 	   (itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN or
	-- 	   itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN_TALISMAM) then
	-- 	   if itemCnt >= itemConfig.fullCnt then
	-- 			tCompositeTable[itemID] = tCompositeTable[itemID] or 0
	-- 			tCompositeTable[itemID] = tCompositeTable[itemID] + itemCnt

	-- 			if itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN then
	-- 				isHaveHero = true
	-- 			end
	-- 	   end
	-- 	end
	-- end
	--后续可以优化下，把值对应起来
	local targetSubType = 0
	if synthesisType == SUMMON_SUBTYPE_HERO then
		targetSubType = ItemDefine.ITEM_SUBTYPE_SUIPIAN
	elseif synthesisType == SUMMON_SUBTYPE_TALISAM then
		targetSubType = ItemDefine.ITEM_SUBTYPE_SUIPIAN_TALISMAM
	elseif synthesisType == SUMMON_SUBTYPE_RELIC then
		targetSubType = ItemDefine.ITEM_SUBTYPE_SUIPIAN_RELIC
	elseif synthesisType == SUMMON_SUBTYPE_ELF then
		targetSubType = ItemDefine.ITEM_SUBTYPE_SUIPIAN_ELF
	end

	for itemID,itemCnt in pairs(human.db.bag) do
		local itemConfig = ItemExcel.item[itemID]
		if itemConfig and itemConfig.mainType == ItemDefine.MAINTYPE_ITEM and itemCnt >= itemConfig.fullCnt then
			if itemConfig.subType == targetSubType then
				tCompositeTable[itemID] = tCompositeTable[itemID] or 0
				tCompositeTable[itemID] = tCompositeTable[itemID] + itemCnt

				if synthesisType == SUMMON_SUBTYPE_HERO then
					isHaveHero = true
				end
			end
		end
	end


	if nil ~= _G.next(tCompositeTable) then
		status = 0
		SUIPIAN_SYNTHESIS(human, tCompositeTable, isHaveHero)
	end

	msgRet.status = status
	Msg.send(msgRet, human.fd)
end


function SuiPianLogic_DuiHuangQuery(human, nID)
	local itemConfig = ItemDefine.getConfig(nID)
	if not itemConfig then
		return
	end

	local tGetInfo = itemConfig.get
	if nil == _G.next(tGetInfo) then
		return
	end

	if tGetInfo[1] ~= SUMMON_TYPE_TARGET or tGetInfo[2] ~= SUMMON_HECHENG_TYPE_GOOD  then
		return
	end

	local tTargetID = tGetInfo[3]
	local nUseNum = itemConfig.nUseNum
	if 0 >= nUseNum then
		return
	end

	local tMsgData = Msg.gc.GC_DUIHUANG_QUERY
	tMsgData.nUseNum = nUseNum
	Grid.makeItem(tMsgData.ItemData, tTargetID, itemConfig.nGetNum)

	Msg.send(tMsgData, human.fd)
end

function SuiPianLogic_DuiHuangDo(human, nID, nTargetNum)
	local itemConfig = ItemDefine.getConfig(nID)
	if not itemConfig then
		return
	end

	local tGetInfo = itemConfig.get
	if nil == _G.next(tGetInfo) then
		return
	end

	if tGetInfo[1] ~= SUMMON_TYPE_TARGET or tGetInfo[2] ~= SUMMON_HECHENG_TYPE_GOOD  then
		return
	end

	local nUseNum = nTargetNum * itemConfig.nUseNum
	if 0 >= nUseNum or nUseNum > BagLogic.getItemCnt(human, nID) then
		return
	end

	local nAddNum = nTargetNum * itemConfig.nGetNum

	-- print("[SuiPianLogic_DuiHuangDo]")
	BagLogic.delItem(human, nID, nUseNum, "item_hecheng")
	BagLogic.addItemList(human, {{tGetInfo[3], nAddNum}}, "item_hecheng")
end