--------------------------------
-- 文件名       ：  QQBigShot.lua
-- 文件说明     ：  QQ大咖相关
-- 创建时间     :   2025/05/19
-- 创建人       :   FC
--------------------------------

local Msg = require("core.Msg")
local Util = require("common.Util")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local QQBigShouCong = require("excel.qqbigshot")
local CommonDefine = require("common.CommonDefine")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")

local tLvID2ConfID =
{
    [1] = 3,
    [2] = 4,
    [3] = 1,
    [4] = 2,
}

local tNextLv = 
{
    [0] = 3,
    [3] = 4,
    [4] = 1,
    [1] = 2
}

----------------------------------------- 内部处理开始 -------------------------------------
-- 初始化玩家qq大咖数据
local function QQBigShot_CreatDB(human)
    if not human.db.qqBigShot then
        human.db.qqBigShot = {}
    end

    human.db.qqBigShot = {
        nQQLv = 0,
        nResetTime = os.time(),
        tPrizeInfo = {
            --[[
                [nID] = {   -- 可以用位运算，但是没必要
                    [1] = 0/1/2 -- 首次
                    [2] = 0/1/2 -- 免费
                }
            ]]
        }
    }

    local tPrize = human.db.qqBigShot.tPrizeInfo
    for nID, v in ipairs(QQBigShouCong.Sheet1) do
        if not tPrize[nID] then
            tPrize[nID] = {}
        end

        tPrize[nID][1] = CommonDefine.COMMON_PRIZE_STATE_NOGET
        tPrize[nID][2] = CommonDefine.COMMON_PRIZE_STATE_NOGET
    end
end

-- 设置玩家大咖等级
local function QQBigShot_SetLv(human, nLv)
    human.db.qqBigShot.nQQLv = nLv
    if not human.db.yellow then
        human.db.yellow = {}
    end
    human.db.yellow.lv = nLv
end

-- 获取玩家大咖等级
local function QQBigShot_GetLv(human)
    return human.db.qqBigShot.nQQLv
end

-- 获取首次礼包状态
local function QQBigShot_GetPrizeFirst(human, nID)
    return human.db.qqBigShot.tPrizeInfo[nID][1]
end

-- 设置首次礼包状态
local function QQBigShot_SetPrizeFirst(human, nID, nValue)
    human.db.qqBigShot.tPrizeInfo[nID][1] = nValue
end

-- 获取周礼包状态
local function QQBigShot_GetPrizeWeek(human, nID)
    return human.db.qqBigShot.tPrizeInfo[nID][2]
end

-- 设置首次礼包状态
local function QQBigShot_SetPrizeWeek(human, nID, nValue)
    human.db.qqBigShot.tPrizeInfo[nID][2] = nValue
end

local function QQBigShot_CheckCreate(human)
    if human.db.qqBigShot then
       return
    end

    QQBigShot_CreatDB(human)
end

local function QQBigShot_SetResetTime(human)
    human.db.qqBigShot.nResetTime = os.time()
end

-- 重置对应等级的周礼包
local function QQBigShot_ResetWeekPrize(human, nID)
    QQBigShot_SetPrizeWeek(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_109)
end

----------------------------------------- 客户端请求开始 -------------------------------------
-- 请求数据
function QQBigShot_Query(human)
    QQBigShot_CheckCreate(human)

    local tMsgData = Msg.gc.GC_QQ_BIGSHOT_QUERY
    tMsgData.tItemData[0] = 0
    for nID, v in ipairs(QQBigShouCong.Sheet1) do
        tMsgData.tItemData[0] = tMsgData.tItemData[0] + 1
        local tPrizeInfo = tMsgData.tItemData[tMsgData.tItemData[0]]
        tPrizeInfo.nID = v.nValue
        tPrizeInfo.nFirstStatus = QQBigShot_GetPrizeFirst(human, nID)
        tPrizeInfo.nWeekStatus = QQBigShot_GetPrizeWeek(human, nID)
        
        tPrizeInfo.tFirstlist[0] = #v.tFirstPrize
        for nIndex, tGoods in ipairs(v.tFirstPrize) do
            local itemId = tGoods[1]
            local itemCnt = tGoods[2]
            Grid.makeItem(tPrizeInfo.tFirstlist[nIndex], itemId, itemCnt)
        end

        tPrizeInfo.tWeeklist[0] = #v.tWeekPrize
        for nIndex, tGoods in ipairs(v.tWeekPrize) do
            local itemId = tGoods[1]
            local itemCnt = tGoods[2]
            Grid.makeItem(tPrizeInfo.tWeeklist[nIndex], itemId, itemCnt)
        end
    end

    Msg.send(tMsgData, human.fd)
end

-- 请求领取数据
function QQBigShot_GetPrize(human, nLv)
    local nID = tLvID2ConfID[nLv]
    if not QQBigShouCong.Sheet1[nID] then
        return
    end

    local nNowLv = QQBigShot_GetLv(human)
    if nLv ~= nNowLv then
        print("[QQBigShot_GetPrize] 当前大咖等级不匹配 nLv = "..nLv.." nNowLv = "..nNowLv)
        return
    end

    local tConfig = QQBigShouCong.Sheet1[nID]

    local nFirstStatus = QQBigShot_GetPrizeFirst(human, nID)
    local nWeekStatus = QQBigShot_GetPrizeWeek(human, nID)
    local tGoods = {}
    if nFirstStatus == CommonDefine.COMMON_PRIZE_STATE_CANGET then
        QQBigShot_SetPrizeFirst(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
        for _, v in ipairs(tConfig.tFirstPrize) do
            if not tGoods[v[1]] then
                tGoods[v[1]] = 0
            end
            tGoods[v[1]] = tGoods[v[1]] + v[2]
        end
    end

    if nWeekStatus == CommonDefine.COMMON_PRIZE_STATE_CANGET then
        QQBigShot_SetPrizeWeek(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
        for _, v in ipairs(tConfig.tWeekPrize) do
            if not tGoods[v[1]] then
                tGoods[v[1]] = 0
            end
            tGoods[v[1]] = tGoods[v[1]] + v[2]
        end
    end

    if nil ~= _G.next(tGoods) then
        local tItem = {}
        for nGoodID, nGoodNum in pairs(tGoods) do
            table.insert(tItem, {nGoodID, nGoodNum})
        end

        BagLogic.addItemList(human, tItem, "qqbigshot")
        QQBigShot_Query(human)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_109)
    end
end

function QQBigShot_UpdateLv(human, nLv)
    QQBigShot_CheckCreate(human)
    local nOldLv = QQBigShot_GetLv(human)
    -- local nNextLv = tNextLv[nOldLv]
    -- if not nNextLv  then
    --     print("[QQBigShot_UpdateLv] 不存在对应的下一等级的大咖等级 nOldLv = "..nOldLv)
    --     return
    -- end

    -- if nLv ~= nNextLv then
    --     print("[QQBigShot_UpdateLv] 要提升的等级和下一等级的配置不同 nOldLv = "..nOldLv.." nLv = "..nLv.." nNextLv = "..nNextLv)
    --     return
    -- end

    print("[QQBigShot_UpdateLv] 收到的nLv = "..nLv.." nOldLv = "..nOldLv)
    if nOldLv ~= nLv then
        local nID = tLvID2ConfID[nLv]
        if not nID then
            print("[QQBigShot_UpdateLv] 不正确的等级 nLv = "..nLv.." nOldLv = "..nOldLv)
            return
        end

        QQBigShot_SetLv(human, nLv)
        local nFirstStatus = QQBigShot_GetPrizeFirst(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFirstStatus  then
            QQBigShot_SetPrizeFirst(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
        end

        QQBigShot_SetPrizeWeek(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)

		RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_109)
        QQBigShot_Query(human)
    end
end

----------------------------------------- 外部调用开始 -------------------------------------
function onLogin(human)
    QQBigShot_CheckCreate(human)

    local nLv = QQBigShot_GetLv(human)
    if 0 == nLv then
        return
    end

    local bRet = Util.isSameWeek(human.db.qqBigShot.nResetTime, os.time())
    if false == bRet then
        local nID = tLvID2ConfID[nLv]
        QQBigShot_ResetWeekPrize(human, nID)
        QQBigShot_SetResetTime(human)
    end
end

function updateDaily(human)
    onLogin(human)
end

function isDot(human)
    QQBigShot_CheckCreate(human)
    
    local bRet = false
    for nID, v in ipairs(QQBigShouCong.Sheet1) do
        local nFirstStatus = QQBigShot_GetPrizeFirst(human, nID)
        local nWeekStatus = QQBigShot_GetPrizeWeek(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFirstStatus or CommonDefine.COMMON_PRIZE_STATE_CANGET == nWeekStatus then
            bRet = true
            break
        end
    end

    return bRet
end

function QQBigShot_HumanLoginHandle(human, nLv)
    print("[QQBigShot_HumanLoginHandle] 收到数据进行处理")
    if not human then
        print("[QQBigShot_HumanLoginHandle] 不存在对应的人物")
        return
    end

    if not human.db then
        print("[QQBigShot_HumanLoginHandle] 不存在对应的人物DB数据")
        return
    end

    if not nLv then
        print("[QQBigShot_HumanLoginHandle] 不存在对应的等级")
        return
    end

    QQBigShot_CheckCreate(human)
    print("[QQBigShot_HumanLoginHandle] 获取到nLv = "..nLv)
    QQBigShot_UpdateLv(human, nLv)
end

function QQBigShot_GMReset(human)
    local nOldLv = QQBigShot_GetLv(human)
    QQBigShot_CreatDB(human)
    QQBigShot_UpdateLv(human, nOldLv)
end