--------------------------------
-- 文件名       ：  RecommendLineup.lua
-- 文件说明     ：  推荐阵容
-- 创建时间     :   2025/1/15
-- 创建人       :   FC
--------------------------------

local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local HeroGrid = require("hero.HeroGrid")
local CommonDB = require("common.CommonDB")
local Msg = require("core.Msg")
local CombatPosLogic = require("combat.CombatPosLogic")
local CombatDefine = require("combat.CombatDefine")
local HeroExcel = require("excel.hero")
local tTuiJian = require("excel.tuijian")


local nRecommendLineupLen = 30		 -- 阵容记录推荐数量
local tRecommendLineupData = nil
local nSendLen = 10                     -- 一次性发送数据长度

---------------------------------------- 内部函数 ------------------------------------
local function RecommendLineup_GetData()
    if not tRecommendLineupData then
        tRecommendLineupData = CommonDB.getRecommendLineUp()
        table.sort(tRecommendLineupData, function (l, r)
            return l.nPower > r.nPower
        end
        )
    end

    return tRecommendLineupData
end

local function RecommendLineup_SetData(tData)
    table.sort(tData, function (l, r)
        return l.nPower > r.nPower
    end
    )
    tRecommendLineupData = tData
    CommonDB.SetRecommendLineUp(tRecommendLineupData)
end

-- 获取小于该战力的下表
local function RecommendLineup_GetIndex(nNewPower, human)
    local nIndex = -1
    local tDBData = RecommendLineup_GetData()
    local nKey = human.db._id

    if nil == _G.next(tDBData) then
        nIndex = 1
        return nIndex
    end

    local nNowLen = #tDBData
    if nNowLen < nRecommendLineupLen then
        for i, value in ipairs(tDBData) do
            -- 更新阵容
            if value.key and value.key == nKey then
                -- 大于等于才更新
                if nNewPower >= value.nPower then
                    print("[RecommendLineup_GetIndex] 已存在 nIndex = "..i)
                    nIndex = i
                end
                
                -- 直接返回
                return nIndex
            end
        end

        if -1 == nIndex then
            nIndex = nNowLen + 1
            return nIndex
        end
    end

    for i, value in ipairs(tDBData) do
        local nPower = value.nPower
        if nNewPower > nPower then
            nIndex = i
            break
        end
    end

    return nIndex
end

-- 插入最新的表
local function RecommendLineup_UpDateData(human, nIndex, tHeroData)
    local tOldData = RecommendLineup_GetData()
    local nOldLen = #tOldData
    local tNewData = {}

    if nil == _G.next(tOldData) then
        table.insert(tNewData, tHeroData)
        RecommendLineup_SetData(tNewData)
        return
    end

    -- 已经存在
    if nIndex <= nOldLen then
        tNewData = tOldData
        tNewData[nIndex] = tHeroData
        RecommendLineup_SetData(tNewData)
        return
    end

    -- 长度还不够
    if nOldLen < nRecommendLineupLen then
        tNewData = tOldData
        table.insert(tNewData, tHeroData)
        RecommendLineup_SetData(tNewData)
        return
    end
    
    -- 只有30个直接遍历
    local nLen = 0
    local tLastData = nil
    for i, value in ipairs(tOldData) do
        if nLen >= nRecommendLineupLen then
            break
        end

        if i == nIndex then
            table.insert(tNewData, tHeroData)
            tLastData = value
        elseif i < nIndex then
            table.insert(tNewData, value)
        elseif i > nIndex then
            table.insert(tNewData, tLastData)
            tLastData = value
        end
        
        nLen = nLen + 1
    end

    RecommendLineup_SetData(tNewData)
end

-- 封装信息
local function RecommendLineup_MakeSimpleData(net, tSaveData)
    local cf = HeroExcel.hero[tSaveData.id]
    if not cf then
        print("[RecommendLineup_MakeSimpleData] 不存在对应英雄配置 nHeroID = "..tSaveData.id)
        return
    end
    net.id = tSaveData.id
	net.index = tSaveData.index or 0
    net.uuid = tSaveData.uuid or ""
	net.gl = tSaveData.gl or 0
	net.camp = tSaveData.camp or 0
	net.job = tSaveData.job or 0
	net.star = tSaveData.star or 0 --cf and cf.star or 0 星级调整 dxzeng  
	net.icon = tSaveData.head or (cf.head or 0)
	net.body = tSaveData.body or 0
	net.up =  tSaveData.up or 0 --是否能够升星

	net.lv = tSaveData.lv or 1
	net.xLv = tSaveData.xLv or 0
	net.zhandouli = tSaveData.zhandouli or 0
	net.quality = tSaveData.quality or 0
	net.isLock = tSaveData.isLock and 1 or 0
	net.hp = tSaveData.hp or 0		
	net.hpMax = tSaveData.hpMax or 0	
	net.cnt = tSaveData.cnt or 1
	net.isget = tSaveData.isget  or 0
	net.weightLv = tSaveData.grade or 0
	net.name = tSaveData.name or ""
	net.grade = tSaveData.grade or 0
	net.jobDesc = tSaveData.desc or ""
    net.label = tSaveData.label or ""
    net.order = tSaveData.order or 0
    net.isGongMing = tSaveData.isGongMing or 0
    
    
    --是否发送图鉴信息
    net.general = tSaveData.general

	--宝石
	net.gemData = tSaveData.gemData
end

---------------------------------------- 客户端请求 ------------------------------------
function GetRecommendLineUp(human)
    local tData = tTuiJian.Sheet1
    -- table.print_lua_table(tData)

    local tMsgData = Msg.gc.GC_DRAWCARD_GET_RECOMMEND_LINEUP
    tMsgData.list[0] = 0 

    local nAllLen, nNowSendLen = #tData, 0
    for _, v in ipairs(tData) do
        tMsgData.list[0] = tMsgData.list[0] + 1
        -- print("[GetRecommendLineUp] i = "..tMsgData.list[0])
        local tHeroData = tMsgData.list[tMsgData.list[0]]
        tHeroData.name = v.name
        tHeroData.data[0] = 0
        nNowSendLen = nNowSendLen + 1

        for i, tHero in ipairs(v.list) do
            tHeroData.data[0] = tHeroData.data[0] + 1
            local tHeroSimple = tHeroData.data[tHeroData.data[0]]
            local nHeroID = tHero[1]
            local nHeroStar = tHero[2]

            HeroGrid.makeHeroSimpleByID(tHeroSimple, nHeroID)
            tHeroSimple.star = nHeroStar
            -- print("[GetRecommendLineUp] 封装 英雄数据结束 nHeroID = "..nHeroID.." nHeroStar = "..nHeroStar.." i = "..i)
        end

        if tMsgData.list[0] >= nSendLen then
            tMsgData.bIsEnd = nNowSendLen == nAllLen and 1 or 0
            Msg.send(tMsgData, human.fd)
            tMsgData.list[0] = 0
        end
        -- table.print_lua_table(tHeroData)
    end
	
    if tMsgData.list[0] > 0 then
        tMsgData.bIsEnd = 1
        Msg.send(tMsgData, human.fd)
    end
	print("[GetRecommendLineUp] 发送成功 ")
end


function RecommendLineup_UpDate(human)
    if human then
        return
    end

    return
    -- if 5 >= human.db.lv then
    --     return
    -- end

    -- local combatHero,helpList = CombatPosLogic.getCombatHeros(human, CombatDefine.COMBAT_TYPE1)
    -- if not combatHero then
    --     return
    -- end

    -- local nNewPower = CombatPosLogic.getCombatHeroZDL(human, CombatDefine.COMBAT_TYPE1, true)

    -- local nIndex = RecommendLineup_GetIndex(nNewPower, human)
    -- if -1 >= nIndex then
    --     return
    -- end

    -- print("[RecommendLineup_UpDate] 获取到的下表 nIndex = "..nIndex)

    -- local tHeroData = {
    --     key = human.db._id,
    --     nPower = nNewPower,
    --     data = {},
    -- }

    -- local nLen = 1
    -- for _, v in pairs(combatHero) do
    --     local heroGrid = HeroLogic.getHeroGridByUuid(human, v)
    --     if heroGrid then
    --           tHeroData.data[nLen] = {
    --             general = {},
    --             gemData = {},
    --             relic = {},
    --         }
    --         HeroGrid.makeHeroSimple(tHeroData.data[nLen], heroGrid, heroGrid.bagIndex, human)
    --         nLen = nLen + 1
    --     end
    -- end
    
    -- for _,v in pairs(helpList) do
    --     local heroGrid = HeroLogic.getHeroGridByUuid(human, v)
    --     if heroGrid then
    --         tHeroData.data[nLen] = {
    --             general = {},
    --             gemData = {},
    --         }
    --         HeroGrid.makeHeroSimple(tHeroData.data[nLen], heroGrid, heroGrid.bagIndex, human)
    --         nLen = nLen + 1
    --     end
    -- end

    -- RecommendLineup_UpDateData(human, nIndex, tHeroData)
end

function RecommendLineup_GMClear()
    local tData = {}
    tRecommendLineupData = tData
    CommonDB.SetRecommendLineUp(tRecommendLineupData)
    print("[RecommendLineup_GMClear] 重置成功")
end