
-- 统一英雄合成和英雄升星的入口，统一使用HeroLogic.heroJueXingDo()

local UpNeedExcel = require("excel.upNeed")
local HeroExcel = require("excel.hero")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local HeroGrid = require("hero.HeroGrid")
local HeroLogic = require("hero.HeroLogic")
local HeroDefine = require("hero.HeroDefine")
local HeroAttrExcel = require("excel.hero").attr
local CombatPosLogic = require("combat.CombatPosLogic")

COND_TYPE_HEROID = 1 		-- 指定id
COND_TYPE_CAMPSTAR = 2 		-- 指定阵营和星级

local NORMAL_COND = {1,0,1}
local NORMAL_CONDS = {}
function getHechengConds(heroID, conds)
	Util.cleanTable(NORMAL_CONDS)
	NORMAL_COND[2] = heroID
	NORMAL_CONDS[1] = NORMAL_COND
	for _, cond in ipairs(conds) do
		NORMAL_CONDS[#NORMAL_CONDS + 1] = cond
	end
	return NORMAL_CONDS
end

-- 合成查询
function hechengQuery(human, camp)
	local msgRet = Msg.gc.GC_HECHENG_QUERY
	msgRet.camp = camp
    local upStar = HeroDefine.ATTRHEROS
    local len = 0
	for heroID, v in pairs(upStar) do 
		local mHeroConfig = HeroExcel.hero[heroID]
		if mHeroConfig and (mHeroConfig.camp == camp or camp == 0) then
            local nextAttrConfig = HeroDefine.getNextAttrConfig(heroID, mHeroConfig.star)
                if nextAttrConfig ~= nil then
                for _, attrID in pairs(v) do
                    local attrConfig = HeroAttrExcel[attrID]
                    if attrConfig then
                        if attrConfig.jinjieshi < 1 and #attrConfig.cond > 0 then
                            len = len + 1
		        	        local net = msgRet.list[len]	
                            
		        	        local conds = getHechengConds(heroID, attrConfig.cond)

		        	        HeroGrid.makeHeroSimpleByID(net.heroSimple, nextAttrConfig.heroID)

                            net.heroSimple.star = nextAttrConfig.star

		        	        net.conditionList[0] = #conds
                          
		        	        for i = 1, #conds do
		        	    	    HeroLogic.makeUpStarCond(net.conditionList[i], conds[i])		
		        	        end
                          
                            if len >= 10 then
                               msgRet.isEnd = 0
                               msgRet.list[0] = len
	                           Msg.send(msgRet, human.fd)
                               len = 0
                            end
                        end
                    end
		        end
            end
        end
	end

    msgRet.isEnd = 1
	msgRet.list[0] = len
	Msg.send(msgRet, human.fd)
    
	sendHechengReds(human)
end

-- 检查材料符合不
function checkCond(human, inputIDList, inputIndexList, condList)
	if inputIDList[0] ~= inputIndexList[0] then
		return 
	end

    local indexTable = {}
    local indexStar = {}
    for i = 1, inputIDList[0] do
		local heroIndex = inputIndexList[i]
		local heroID = inputIDList[i]
		local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
		if not heroGrid then -- 不存在英雄
			return
		end
		if indexTable[heroIndex] then -- 重复id
			return
		end
		indexTable[heroIndex] =  true
        if CombatPosLogic.isLastInCombat(human, heroGrid.uuid) then
            return
        end
        indexStar[i] = heroGrid.star
	end
        
	local judgeIndex = 0
	for _, cond in ipairs(condList) do
		local ctype = cond[1] 
		if ctype == COND_TYPE_HEROID then
			local needHeroID = cond[2]
			local needHeroCnt = cond[3]
            local needHeroStar = cond[4] or HeroExcel.hero[needHeroID].star
			for i = 1, needHeroCnt do
				judgeIndex = judgeIndex + 1				
				if judgeIndex > inputIDList[0] then
					return Broadcast.sendErr(human, Lang.HERO_HECHENG_ERR_MATERIAL)
				end

				local inputTargetID = inputIDList[judgeIndex]
                local inputTargetStar = indexStar[judgeIndex]
				if inputTargetID ~= needHeroID or inputTargetStar ~= needHeroStar then
					return Broadcast.sendErr(human, Lang.HERO_HECHENG_ERR_COND)
				end
			end			
		elseif ctype == COND_TYPE_CAMPSTAR then
			local needHeroCamp = cond[2]
			local needHeroStar = cond[3]
			local needHeroCnt = cond[4]
			for j = 1, needHeroCnt do 
				judgeIndex = judgeIndex + 1				
				if judgeIndex > inputIDList[0] then
					return Broadcast.sendErr(human, Lang.HERO_HECHENG_ERR_MATERIAL)
				end				
				local inputTargetID = inputIDList[judgeIndex]
				local inputHeroConfig = HeroExcel.hero[inputTargetID]
				if needHeroCamp ~= 0 and inputHeroConfig.camp ~= needHeroCamp then 
					return Broadcast.sendErr(human, Lang.HERO_HECHENG_ERR_COND)
				end

				if indexStar[judgeIndex] ~= needHeroStar then 
					return Broadcast.sendErr(human, Lang.HERO_HECHENG_ERR_COND)
				end
			end			
		else
			assert(nil)
		end
	end
	return true
end

function isHeroRed(human, heroID, config)
	local conds = getHechengConds(heroID, config)
	local without = {}
	for _, cond in ipairs(conds) do
		local ctype = cond[1] 
		local needCnt = cond[3] 
		if not needCnt then return end
		if ctype == COND_TYPE_CAMPSTAR then
			needCnt = cond[4]
		end
		for i = 1, needCnt do
			local index = HeroLogic.selectJuexingGrid(human, ctype, cond, without) 
			if not index then return end
			without[index] = true
		end
	end
	return true
end

function isCampRed(human, camp)
   local upStar = HeroDefine.ATTRHEROS
   for heroID, v in pairs(upStar) do 
       local mHeroConfig = HeroExcel.hero[heroID]
	   if mHeroConfig and mHeroConfig.camp == camp then
          for _, attrID in pairs(v) do
              local attrConfig = HeroAttrExcel[attrID]
              if attrConfig and attrConfig.jinjieshi < 1 and #attrConfig.cond > 0 and 
                 isHeroRed(human, heroID, attrConfig.cond) then
		         return true
              end
          end
	   end
   end
end

-- 红点
function sendHechengReds(human)
	local msgRet = Msg.gc.GC_HECHENG_REDS
	msgRet.reds[0] = #HeroExcel.camp
	for camp = 1, msgRet.reds[0] do
		msgRet.reds[camp] = isCampRed(human, camp) and 1 or 0
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

