--------------------------------
-- 文件名       ：  UnlimitDrawLogic.lua
-- 文件说明     ：  无限高抽
-- 创建时间     :   2025/09/29
-- 创建人       :   FC
--------------------------------
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local HeroGrid = require("hero.HeroGrid")
local HeroLogic = require("hero.HeroLogic")
local BagLogic = require("bag.BagLogic")
local Log = require("common.Log")
local Broadcast = require("broadcast.Broadcast")
local DrawCardExcel = require("excel.drawCard").drawCard
local RandomLogic = require("common.RandomLogic")
local HeroDefine = require("hero.HeroDefine")
local HeroExcel = require("excel.hero").hero
local HeroBook = require("hero.HeroBook")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")

-- local DrawCardLogic = require("drawCard.DrawCardLogic")

local UNLIMIT_CARD_ID = 12
local UNLIMIT_CARD_LV = 35

local LOGINDAY3 = 3 -- 登录3天
local LOGINDAY7 = 7 -- 登录7天

----------------------------------------- 内部处理开始 -------------------------------------
local function UnlimitDraw_CreateDB(human)
    human.db.UnlimitDraw = {
        nNowNum = 0,
        bLogin = false,
        bLv = false,
        nFirstLoginTime = 0,
        bNextLogin = false,
        tChacheHero = nil,
    }
end

local function UnlimitDraw_CheckDB(human)
    if not human.db.UnlimitDraw then
        UnlimitDraw_CreateDB(human)
    end
end

local function UnlimitDraw_GetNum(human)
    return human.db.UnlimitDraw.nNowNum
end

local function UnlimitDraw_SetNum(human, nValue)
    human.db.UnlimitDraw.nNowNum = human.db.UnlimitDraw.nNowNum + nValue
    if human.db.UnlimitDraw.nNowNum < 0 then
        human.db.UnlimitDraw.nNowNum = 0
    end
end

local function UnlimitDraw_GetLogin(human)
    return human.db.UnlimitDraw.bLogin
end

local function UnlimitDraw_SetLogin(human, bValue)
    human.db.UnlimitDraw.bLogin = bValue
end

local function UnlimitDraw_GetLoginTime(human)
    return human.db.UnlimitDraw.nFirstLoginTime
end

local function UnlimitDraw_SetLoginTime(human, nValue)
    human.db.UnlimitDraw.nFirstLoginTime = nValue
end

local function UnlimitDraw_GetLv(human)
    return human.db.UnlimitDraw.bLv
end

local function UnlimitDraw_SetLv(human, bValue)
    human.db.UnlimitDraw.bLv = bValue
end

local function UnlimitDraw_GetNextLogin(human)
    return human.db.UnlimitDraw.bNextLogin
end

local function UnlimitDraw_SetNextLogin(human, bValue)
    human.db.UnlimitDraw.bNextLogin = bValue
end

local function UnlimitDraw_SetCacheHero(human, tHeroInfo)
    human.db.UnlimitDraw.tChacheHero = tHeroInfo
end

local function UnlimitDraw_GetCacheHero(human)
    return human.db.UnlimitDraw.tChacheHero
end

local function UnlimitDraw_ClearCacheHero(human)
    human.db.UnlimitDraw.tChacheHero = nil
end

local function UnlimitDraw_randHeroID(human, config, randomIndex)
    local heroID = nil
    local rindex = RandomLogic.getRandom1(config.weight, nil, 2)
    local rconf = config.weight[rindex]
    local weightLv = rconf[1]

    heroID = heroID or HeroDefine.getRandHeroByWeightLv(weightLv)
    return heroID
end

local function UnlimitDraw_GetLoginDay(human)
    return human.db.UnlimitDraw.LoginDay or 0
end

local function UnlimitDraw_AddLoginDay(human, bValue)
    human.db.UnlimitDraw.LoginDay = (human.db.UnlimitDraw.LoginDay or 0) + bValue
end


----------------------------------------- 客户端请求 -------------------------------------
function UnlimitDrawLogic_Query(human)
    UnlimitDraw_CheckDB(human)

    local tMsgData = Msg.gc.GC_UNLIMITDRAW_QUERY

    tMsgData.nNowNum = UnlimitDraw_GetNum(human)
    tMsgData.tGetInfo[0] = 5
    tMsgData.tGetInfo[1] = UnlimitDraw_GetLogin(human) and 1 or 0
    tMsgData.tGetInfo[2] = UnlimitDraw_GetLv(human) and 1 or 0
    tMsgData.tGetInfo[3] = UnlimitDraw_GetNextLogin(human) and 1 or 0

    local nowLoginDay = UnlimitDraw_GetLoginDay(human)
    tMsgData.tGetInfo[4] = nowLoginDay >= LOGINDAY3 and 1 or 0
    tMsgData.tGetInfo[5] = nowLoginDay >= LOGINDAY7 and 1 or 0

    Msg.send(tMsgData, human.fd)
end

function UnlimitDrawLogic_Do(human)
    local nNowNum = UnlimitDraw_GetNum(human)
    if 0 >= nNowNum then
        return
    end

    local nDoNum = 10
    local config = DrawCardExcel[UNLIMIT_CARD_ID]

    if HeroLogic.getEmptyCnt(human) < nDoNum then
        return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
    end

    UnlimitDraw_ClearCacheHero(human)

    local heroLen = 0
    local heroList = {}
    local heroNewList = {}
    local heroIndexList = {}
    local fenjielist = {}
    local heroIDs = nil

    local randomMin = math.random(1, nDoNum)
    for i = 1, nDoNum, 1 do
        local nHeroID = UnlimitDraw_randHeroID(human, config, randomMin)
        if nHeroID then
            local heroConfig = HeroExcel[nHeroID]
            local star = heroConfig.star
            local name = heroConfig.name
            local grade = heroConfig.grade
            local isNew = not HeroBook.isGet(human, heroConfig.id, heroConfig.star)
            -- local heroIndex, fjlist = HeroLogic.addHero(human, nHeroID, nil, 1, "draw_card")

            heroLen = heroLen + 1
            heroList[heroLen] = nHeroID
            heroNewList[heroLen] = isNew

            if not heroIDs then
                heroIDs = nHeroID
            else
                heroIDs = heroIDs .. "|" .. nHeroID
            end
        else
            print("[UnlimitDrawLogic_Do] 生成英雄失败直接返回")
            return
        end
    end

    local tMsgData = Msg.gc.GC_UNLIMITDRAW_DO
    tMsgData.tHeros[0] = nDoNum
    for i = 1, heroLen do
        local heroID = heroList[i]
        local isNew = heroNewList and heroNewList[i]
        -- local index = heroIndexList and heroIndexList[i]
        HeroGrid.makeHeroNice(tMsgData.tHeros[i], heroID, 1, isNew)
    end

    UnlimitDraw_SetCacheHero(human, heroList)
    Msg.send(tMsgData, human.fd)
end

function UnlimitDrawLogic_Abandon(human)
    UnlimitDraw_ClearCacheHero(human)
end

function UnlimitDrawLogic_Get(human)
    local tHeroInfo = UnlimitDraw_GetCacheHero(human)
    if not tHeroInfo then
        return
    end

    local nNowNum = UnlimitDraw_GetNum(human)
    if 0 >= nNowNum then
        return
    end

    UnlimitDraw_SetNum(human, -1)

    local heroIndexList = {}
    local fenjielist = {}
    for _, nID in ipairs(tHeroInfo) do
        local heroIndex, fjlist = HeroLogic.addHero(human, nID,nil, 1, "UnlimitDrawCard")
        if fjlist and type(fjlist) == "table" then
            for fjItemID, fjItemCnt in pairs(fjlist) do
                fenjielist[fjItemID] = (fenjielist[fjItemID] or 0) + fjItemCnt
            end
        end

        table.insert(heroIndexList, heroIndex)
    end

    if nil ~= _G.next(fenjielist) then
        local tItem = {}
        for key, value in pairs(fenjielist) do
            table.insert(tItem, {key, value})
        end
        BagLogic.sendItemGetList(human, tItem, "UnlimitDrawCard")
    end
    
    UnlimitDraw_ClearCacheHero(human)
    UnlimitDrawLogic_Query(human)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_111, nil, true)
end

function onLogin(human)
    UnlimitDraw_CheckDB(human)

    local bSendDot = false
    local bLogin = UnlimitDraw_GetLogin(human)
    if false == bLogin then
        UnlimitDraw_SetLogin(human, true)
        UnlimitDraw_SetLoginTime(human, os.time())
        UnlimitDraw_SetNum(human, 1)
        UnlimitDraw_AddLoginDay(human, 1)
        bSendDot = true
    else
        local nLastTime = UnlimitDraw_GetLoginTime(human)
        local bNextLogin = UnlimitDraw_GetNextLogin(human)
        if not Util.isSameDay(nLastTime) and false == bNextLogin then
            UnlimitDraw_SetNextLogin(human, true)
            UnlimitDraw_SetNum(human, 1)
            bSendDot = true
        end

        if not Util.isSameDay(nLastTime) then
            local beforeLoginDay = UnlimitDraw_GetLoginDay(human)
            if beforeLoginDay <= LOGINDAY7 then
                UnlimitDraw_AddLoginDay(human, 1)
                UnlimitDraw_SetLoginTime(human, os.time())

                local nowLoginDay = UnlimitDraw_GetLoginDay(human)
                if (beforeLoginDay < LOGINDAY3 and nowLoginDay >= LOGINDAY3) or (beforeLoginDay < LOGINDAY7 and nowLoginDay >= LOGINDAY7) then
                    UnlimitDraw_SetNum(human, 1)
                    bSendDot = true
                end
            end
        end
    end

    local bLv = UnlimitDraw_GetLv(human)
    if false == bLv and human.db.lv >= UNLIMIT_CARD_LV then
        UnlimitDraw_SetLv(human, true)
        UnlimitDraw_SetNum(human, 1)
        bSendDot = true
    end

    if true == bSendDot then
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_111)
    end
end

function onLvUp(human, nNewLv)
    local bLv = UnlimitDraw_GetLv(human)
    if false == bLv and nNewLv >= UNLIMIT_CARD_LV then
        UnlimitDraw_SetLv(human, true)
        UnlimitDraw_SetNum(human, 1)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_111, true)
    end
end

function ModuleisOpen(human)
    UnlimitDraw_CheckDB(human)

    if true == UnlimitDraw_GetLogin(human) and UnlimitDraw_GetLv(human) == true 
        and true == UnlimitDraw_GetNextLogin(human) then
        local beforeLoginDay = UnlimitDraw_GetLoginDay(human)
        if UnlimitDraw_GetNum(human) > 0 or beforeLoginDay <= LOGINDAY7 then
            return true
        else
            print("[ModuleisOpen] 当前系统未开放")
            return false
        end
    end

    return true
end

function isDot(human)
    UnlimitDraw_CheckDB(human)

    if true == ModuleisOpen(human) then
        if UnlimitDraw_GetNum(human) > 0 then
            local nNowNum = UnlimitDraw_GetNum(human)
            print("[UnlimitDraw_isDot] 当前系统开放可抽取数量大于0 nNowNum = "..nNowNum)
            return true
        end
    end

    return false
end