-- 装备图鉴
local Msg = require("core.Msg")
local EquipExcel = require("excel.equip").equip
local EquipRareExcel = require("excel.equip").rare
local ItemDefine = require("bag.ItemDefine")
local Grid = require("bag.Grid")
local EquipLogic = require("equip.EquipLogic")
local BagLogic = require("bag.BagLogic")
local EquipLogicGrid = require("equip.EquipLogicGrid")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local HeroLogic = require("hero.HeroLogic")
local Util = require("common.Util")
local HeroGrowUp = require("absAct.HeroGrowUp")

-- 取出合适的装备
local function getEquipGrid(human, bagIndex, heroUuid, pos)
    local equipGrid = human.db.equipBag[bagIndex]
    if not equipGrid then 
       -- 取英雄身上的
       local heroGrid = HeroLogic.getHeroGridByUuid(human, heroUuid)
       if not heroGrid then return end

       equipGrid = heroGrid.equip and heroGrid.equip[pos]

       return equipGrid, heroGrid
    end

    return equipGrid
end


-- 查询装备图鉴 全部信息
function fenjieQuey(human, inputList)
    local indexTable = { }
    for i = 1, inputList[0] do
        local index = inputList[i]
        local equipGrid = human.db.equipBag[index]
        if not equipGrid then
            return
        end

        if not EquipRareExcel[equipGrid.quality] then return end

        if indexTable[index] then
            -- 重复id
            return
        end
        indexTable[index] = true
    end

    local itemList = { }
    for i = 1, inputList[0] do
        local index = inputList[i]
        local equipGrid = human.db.equipBag[index]

        local equipConfig = EquipExcel[equipGrid.id]
        if not equipConfig then return end

        for k, v in ipairs(EquipRareExcel[equipGrid.quality].disassembly) do
            itemList[v[1]] = itemList[v[1]] or 0
            itemList[v[1]] = itemList[v[1]] + v[2]
        end
    end

    local msgRet = Msg.gc.GC_EQUIP_FEN_JIE_QUERY
    local len = 0
    for k, v in pairs(itemList) do
       len = len + 1
       Grid.makeItem(msgRet.list[len], k, v)
    end
    msgRet.list[0] = len
    Msg.send(msgRet, human.fd)
end


-- 查询装备图鉴 全部信息
function fenjieDo(human, inputList)
    local indexTable = { }
    for i = 1, inputList[0] do
        local index = inputList[i]
        local equipGrid = human.db.equipBag[index]
        if not equipGrid then
            return
        end

        if not EquipRareExcel[equipGrid.quality] then return end

        if indexTable[index] then
            -- 重复id
            return
        end
        indexTable[index] = true
    end

    local itemList = { }
    for i = 1, inputList[0] do
        local index = inputList[i]
        local equipGrid = human.db.equipBag[index]

        local equipConfig = EquipExcel[equipGrid.id]
        if not equipConfig then return end

        for k, v in ipairs(EquipRareExcel[equipGrid.quality].disassembly) do
            itemList[v[1]] = itemList[v[1]] or 0
            itemList[v[1]] = itemList[v[1]] + v[2]
        end

        EquipLogic.delEquip(human, index, "equip_fenjie")
		HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE21, 1)
    end

    BagLogic.addItemList(human, itemList, "equip_fenjie")

    local msgRet = Msg.gc.GC_EQUIP_FEN_JIE_DO
    Msg.send(msgRet, human.fd)
end

-- 洗练查询
function randomQuery(human, bagIndex, heroUuid, pos)
    local equipGrid = getEquipGrid(human, bagIndex, heroUuid, pos)
    if not equipGrid then return end

    local rareExcel = EquipRareExcel[equipGrid.quality]
    if not rareExcel then return end

    local msgRet = Msg.gc.GC_EQUIP_RAMDOM_QUERY
    Grid.makeItem(msgRet.equipOld, equipGrid.id, 1, nil, equipGrid, bagIndex)

    -- 当前洗练属性
    local washAttr = equipGrid.washAttr
    local washQuality = equipGrid.washQuality
    msgRet.equipNew[0] = 0
    if washAttr and washQuality then
        local washEquipGrid = EquipLogicGrid.createTempGrid(equipGrid.id, equipGrid.washAttr, equipGrid.washQuality)
        Grid.makeItem(msgRet.equipNew[1], washEquipGrid.id, 1, nil, washEquipGrid, bagIndex)
        msgRet.equipNew[0] = 1
    end

    msgRet.needItem[0] = #rareExcel.rebirth
    for i = 1, msgRet.needItem[0] do
       Grid.makeItem(msgRet.needItem[i], rareExcel.rebirth[i][1], rareExcel.rebirth[i][2])
    end

    Msg.send(msgRet, human.fd)
end

-- 已穿戴的装备
function putList(human, pos)
    local msgRet = Msg.gc.GC_EQUIP_PUT_LIST
    local cnt = 0
    for index = 1, human.db.heroBag[0] do
        local heroGrid = human.db.heroBag[index]
        if heroGrid and heroGrid.lv >= 2 then
            -- 所有部位
            if pos == 0 then
                for i = 1, ItemDefine.EQUIP_MAX_CNT do
                    local equipGrid = heroGrid.equip and heroGrid.equip[i]
                    if equipGrid then
                        cnt = cnt + 1
                        local net = msgRet.putList[cnt]
                        if i ~= ItemDefine.EQUIP_SUBTYPE_SHUIJIN then
                            Grid.makeItem(net.equip, equipGrid.id, 1, nil, equipGrid)
                            net.pos = i
                        end

                        if cnt >= 10 then
                            msgRet.isEnd = 0
                            msgRet.putList[0] = cnt
                            Msg.send(msgRet, human.fd)
                            cnt = 0
                        end
                    end
                end
            else
                local equipGrid = heroGrid.equip and heroGrid.equip[pos]
                if equipGrid then
                    cnt = cnt + 1
                    local net = msgRet.putList[cnt]
                    if i ~= ItemDefine.EQUIP_SUBTYPE_SHUIJIN then
                        Grid.makeItem(net.equip, equipGrid.id, 1, nil, equipGrid)
                        net.pos = pos
                    end

                    if cnt >= 10 then
                        msgRet.isEnd = 0
                        msgRet.putList[0] = cnt
                        Msg.send(msgRet, human.fd)
                        cnt = 0
                    end
                end
            end
        end
    end
    msgRet.isEnd = 1
    msgRet.putList[0] = cnt
    Msg.send(msgRet, human.fd)
end

-- 洗练
function randomDo(human, bagIndex, heroUuid, pos)
    local equipGrid = getEquipGrid(human, bagIndex, heroUuid, pos)
    if not equipGrid then return end

    local rareExcel = EquipRareExcel[equipGrid.quality]
    if not rareExcel then return end

    for i = 1, #rareExcel.rebirth do
       local needItemID = rareExcel.rebirth[i][1]
       local needItemCnt = rareExcel.rebirth[i][2]
       local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
	   if nowItemCnt < needItemCnt then 
	   	  return Broadcast.sendErr(human, Util.format(Lang.XIANZHI_NO_ITEM, ItemDefine.getValue(needItemID,"name")))
	   end
    end

    for i = 1, #rareExcel.rebirth do
       local needItemID = rareExcel.rebirth[i][1]
       local needItemCnt = rareExcel.rebirth[i][2]
       BagLogic.delItem(human, needItemID, needItemCnt, "equip_wash")
    end
    

    local attr, quality = EquipLogic.randomAttr(equipGrid.id, true, equipGrid.quality)
    equipGrid.washAttr = attr
    equipGrid.washQuality = quality

    HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE19, 1)

    Broadcast.sendErr(human, Lang.EQUIP_WASH_RANDOM_SUC)
    randomQuery(human, bagIndex, heroUuid, pos)
end

-- 洗练查询
function randomSave(human, bagIndex, heroUuid, pos)
    local equipGrid, heroGrid = getEquipGrid(human, bagIndex, heroUuid, pos)
    if not equipGrid then return end

    local washAttr = equipGrid.washAttr
    local washQuality = equipGrid.washQuality
    if not washAttr or not washQuality then return end

    equipGrid.attr = washAttr
    equipGrid.quality = washQuality
    equipGrid.washAttr = nil
    equipGrid.washQuality = nil
    EquipLogic.checkAttr(equipGrid)
    -- 更新背包的
    if not heroGrid then   
       EquipLogic.sendEquipChange(human, bagIndex, equipGrid, EquipLogic.EQUIP_BAG_OP_CHANGE)
    end

    Broadcast.sendErr(human, Lang.EQUIP_WASH_SAVE_SUC)
    randomQuery(human, bagIndex, heroUuid, pos)
end