local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local OpenActExcel = require("excel.openAct")
local BuyLogic = require("topup.BuyLogic")
local Grid = require("bag.Grid")
local OpenAct = require("present.OpenAct")
local Broadcast = require("broadcast.Broadcast")

local function OpenServerGiftPackage_CreatDB(human)
    human.db.OpenServerGiftPackage = {}
    for nID, v in ipairs(OpenActExcel.OpenGiftPackage) do
        human.db.OpenServerGiftPackage[nID] = v.amount
    end
end

local function OpenServerGiftPackage_CheckDB(human)
    if not human.db.OpenServerGiftPackage then
        OpenServerGiftPackage_CreatDB(human)
    end
end

local function OpenServerGiftPackage_GetNum(human, nID)
    if not human.db.OpenServerGiftPackage[nID] then
        if OpenActExcel.OpenGiftPackage[nID] then
            human.db.OpenServerGiftPackage[nID] = OpenActExcel.OpenGiftPackage[nID].amount
        end
    end

    return human.db.OpenServerGiftPackage[nID]
end

local function OpenServerGiftPackage_SetNum(human, nID, nNum)
    human.db.OpenServerGiftPackage[nID] = human.db.OpenServerGiftPackage[nID] + nNum
end


function OpenServerGiftPackage_Query(human)
    OpenServerGiftPackage_CheckDB(human)

    local tMsgData = Msg.gc.GC_OPENSERVER_GIFTPACKAGE_QUERY
    tMsgData.tList[0] = #OpenActExcel.OpenGiftPackage

    for nID, v in ipairs(OpenActExcel.OpenGiftPackage) do
        local tData = tMsgData.tList[nID]
        tData.nID = nID
        tData.nMaxCnt = v.amount
        tData.nNowCnt = OpenServerGiftPackage_GetNum(human, nID)
        
        BuyLogic.fontBuyItem(human, tData.tBuyInfo, v.buyID)

        tData.tItemList[0] = #v.rewards
        for i, tItem in ipairs(v.rewards) do
            Grid.makeItem(tData.tItemList[i], tItem[1], tItem[2])
        end
    end

    Msg.send(tMsgData, human.fd)
    -- print("[OpenServerGiftPackage_Query] 发送数据完成")
end

function onCharge(human, nBuyID, nBuNum)
    -- print("[OpenServerGiftPackage_onCharge] 进入购买函数")
    local tItem, nChoseID = nil, nil
    for nID, v in ipairs(OpenActExcel.OpenGiftPackage) do
        if v.buyID == nBuyID then
            nChoseID = nID
            tItem = v.rewards
        end
    end

    if nChoseID and tItem then
        -- print("[OpenServerGiftPackage_onCharge] 开始发送奖励")
        local nNowCnt = OpenServerGiftPackage_GetNum(human, nChoseID)
        if nNowCnt > 0 then
            -- OpenServerGiftPackage_SetNum(human, nChoseID, -1)
            -- BagLogic.addItemList(human, tItem, "OpenServerGiftPackage")
            OpenServerGiftPackage_SetNum(human, nChoseID, -nBuNum)
            local tItemList = {}
            for _, v in ipairs(tItem) do
                table.insert(tItemList, {v[1], v[2]*nBuNum})
            end
            BagLogic.addItemList(human, tItemList, "OpenServerGiftPackage")

            OpenServerGiftPackage_Query(human)
        end
    end
end

function isRed(human)
	return false
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isBaseOpen(human, YYInfo, funcConfig, noSend)
    if human.db.lv < funcConfig.openLv then
        if not noSend then
            local str = Util.format(Lang.ROLE_LEV_ERROR, funcConfig.openLv)
            return Broadcast.sendErr(human, str)
        end
    end

    local flag = OpenAct.getOpenActTime(funcConfig.param)
    if not flag then return end

    return true
end

function isOpen(human, YYInfo, funcConfig)
	if isBaseOpen(human, YYInfo, funcConfig, true) then return true end
end

function GetRemainNum(human, nBuyID)
    local nChoseID = nil
    for nID, v in ipairs(OpenActExcel.OpenGiftPackage) do
        if v.buyID == nBuyID then
            nChoseID = nID
            break
        end
    end

    if not nChoseID then
        return 0
    end

    local nNowCnt = OpenServerGiftPackage_GetNum(human, nChoseID)
    return nNowCnt
end