-- 十星英雄置换 神将置换
-- 拒绝潜规则

local HeroExcel = require("excel.hero")
local UpNeedExcel = require("excel.upNeed")
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 BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local Broadcast = require("broadcast.Broadcast")
local HeroLogic = require("hero.HeroLogic")
local HeroGrid = require("hero.HeroGrid")
local HeroEquip = require("hero.HeroEquip")
local HeroDefine = require("hero.HeroDefine")
local FenjieLogic = require("hecheng.FenjieLogic")
local FuwenLogic = require("fuwen.FuwenLogic")
local SkinLogic = require("skin.SkinLogic")

MATERIAL1_NEED_STAR = 10                -- 材料1，需要10星以上的英雄
MATERIAL2_NEED_STAR = 5                 -- 材料2，需要5星的英雄

local MAX_LEN = 100

-- 可否作为材料1
-- 得大于10星
function canBeMaterial1(heroID, star)
    if not heroID or not star then return end
    local heroConfig = HeroExcel.hero[heroID]
    if not heroConfig then return end

    if star < MATERIAL1_NEED_STAR then 
        return 
    end  

    local attrConfig = HeroDefine.getAttrConfig(heroID, star)
    return attrConfig
end


-- 可否作为材料2
function canBeMaterial2(heroID)
    if not heroID then return end
    local heroConfig = HeroExcel.hero[heroID]
    if not heroConfig then return end
    if heroConfig.star ~= MATERIAL2_NEED_STAR then 
        return 
    end

    -- 能否找到10-13星的目标英雄
    for starI = MATERIAL1_NEED_STAR, HeroLogic.HERO_MAX_STAR do
        local attrConfig = HeroDefine.getAttrConfig(heroID, starI) 
        if not attrConfig then
            return
        end
    end
    return heroConfig
end


-- 发送魂匣信息给前端
function query(human)
    local material2List = {}
    for index,heroGrid in pairs(human.db.heroBag) do
        if index ~= 0 and canBeMaterial2(heroGrid.id) then
            material2List[heroGrid.id] = true
        end
    end

    local msgRet = Msg.gc.GC_HERO_TEN_ZHIHUAN_QUERY
    Grid.makeItem(msgRet.needItem, ItemDefine.ITEM_MONTHACT_TENSTAR_ID, 1)
    msgRet.canHeroId[0] = 0
    for heroID in pairs(material2List) do
        if msgRet.canHeroId[0] >= MAX_LEN then
            break
        end
        msgRet.canHeroId[0] = msgRet.canHeroId[0] + 1
        msgRet.canHeroId[msgRet.canHeroId[0]] = heroID
    end
    msgRet.material1Star = MATERIAL1_NEED_STAR
    msgRet.material2Star = MATERIAL2_NEED_STAR
    msgRet.needMaterial2Cnts[0] = 0
    msgRet.needItemCnts[0] = 0
    for i = MATERIAL1_NEED_STAR, HeroLogic.HERO_MAX_STAR do
        local config = UpNeedExcel.zhihuan[i]
        -- 消耗5星英雄数量
        msgRet.needMaterial2Cnts[0] = msgRet.needMaterial2Cnts[0] + 1
        msgRet.needMaterial2Cnts[msgRet.needMaterial2Cnts[0]] = config and config.needHeroCnt or 0
        -- 消耗道具数量
        msgRet.needItemCnts[0] = msgRet.needItemCnts[0] + 1
        msgRet.needItemCnts[msgRet.needItemCnts[0]] = config and config.useItem[2] or 0
    end
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end 


function zhihuan(human, material2ID, heroIndexList)
    local heroConfig = canBeMaterial2(material2ID)
    if not heroConfig then         
        return Broadcast.sendErr(human, Lang.TENSTAR_ERR_NOTENSTAR) 
    end

    local materialList = {}
    local material2Cnt = 0
    local material1Star = 0
    for i = 1, heroIndexList[0] do    
        local heroIndex = heroIndexList[i]
        local heroID = HeroLogic.getHeroIdByIndex(human, heroIndex)
        local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
        if not heroID or not heroGrid or materialList[heroIndex] then -- 不存在或重复材料不允许！
            return Broadcast.sendErr(human, Lang.TENSTAR_ERR_MATERIAL)
        end

        materialList[heroIndex] = true
        if heroID == material2ID then
            material2Cnt = material2Cnt + 1
        else
            local material1Config = canBeMaterial1(heroID, heroGrid.star)
            if not material1Config then
                return Broadcast.sendErr(human, Lang.TENSTAR_ERR_MATERIAL)
            end

            local materialHeroConfig = HeroExcel.hero[material1Config.heroID] 
            if materialHeroConfig.camp ~= materialHeroConfig.camp then
                return Broadcast.sendErr(human, Lang.TENSTAR_ERR_CAMP)
            end
            material1Star = math.max(material1Star, material1Config.star)
        end
    end

    local config = UpNeedExcel.zhihuan[material1Star]
    if not config then 
        return Broadcast.sendErr(human, Lang.TENSTAR_ERR_SIZE)
    end 
    -- 物品检查
    local useItemID = config.useItem[1]
    local useItemCnt = config.useItem[2]
    if BagLogic.getItemCnt(human, useItemID) < useItemCnt then
        local itemName = ItemDefine.getValue(useItemID, "name")
        return Broadcast.sendErr(human, Util.format(Lang.COMMON_NO_ITEM, itemName))
    end 
    local needMaterial2Cnt = config.needHeroCnt
    if material2Cnt ~= needMaterial2Cnt then
        return Broadcast.sendErr(human, Lang.TENSTAR_ERR_SIZE)
    end 

    local attrConfig = HeroDefine.getAttrConfig(material2ID, material1Star) 
    if not attrConfig then return end

    -- 扣物品
    BagLogic.delItem(human, useItemID, useItemCnt, "tenStar_displace")

    local fuwenTb = nil
    local equipTb = nil
    local newHeroGrid = nil
    local outItems = {}
    for heroIndex in pairs(materialList) do
        local heroID = HeroLogic.getHeroIdByIndex(human, heroIndex)
        if heroID == material2ID then
            HeroEquip.putOffQuick(human, heroID, heroIndex, true)
            FuwenLogic.putOffQuick(human, heroID, heroIndex, true)
            local inputIDList = {[0]=1, [1]=heroID}
            local inputIndexList = {[0]=1, [1]=heroIndex}
            outItems[#outItems + 1] = FenjieLogic.fenjie(human, FenjieLogic.FENJIE_DO_TENSTAR, inputIDList, inputIndexList, nil, "tenStar_displace")
        else 
            equipTb,fuwenTb = HeroEquip.getEquipAndFuwenByHeroIndex(human,heroID,heroIndex)
            newHeroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
            HeroLogic.delHeroByIndex(human, heroIndex, "tenStar_displace")
        end
    end
    BagLogic.sendItemGetList3(human, outItems, "tenStar_displace")

    newHeroGrid.id = attrConfig.heroID
    local tHeroCof = HeroExcel.hero[attrConfig.heroID]
    if tHeroCof then
        newHeroGrid.head = tHeroCof.head
        newHeroGrid.body = tHeroCof.body
        print("[zhihuan] 置换新英雄先更换为原先的皮肤 heroID = "..attrConfig.heroID.." head = ".. tHeroCof.head.." body = "..tHeroCof.body)
    end
    local newIndex = HeroLogic.addHeroByGrid(human, newHeroGrid, "tenStar_displace")

    if equipTb ~= nil then
        for i = 1,ItemDefine.EQUIP_MAX_CNT do
            if i ~= ItemDefine.EQUIP_SUBTYPE_SHUIJIN and equipTb[i] ~= nil then
                HeroEquip.putOn(human, attrConfig.heroID, newIndex, equipTb[i])
            end
        end
    end

    if fuwenTb ~= nil then
        for i = 1,2 do
            if fuwenTb[i] ~= nil then
                FuwenLogic.putOn(human, attrConfig.heroID, newIndex, fuwenTb[i],i)
            end
        end
    end

    SkinLogic.OnLoginCheckEquipSkin(human, true)

    -- 发送给前端用来显示获得的英雄
    local msgRet = Msg.gc.GC_HERO_TEN_ZHIHUAN
    HeroGrid.makeHeroSimple(msgRet.heroSimple, newHeroGrid, newIndex, human)
    Msg.send(msgRet, human.fd)
end



