--------------------------------
-- 文件名       :   ServerCommerceActShop.lua
-- 文件说明     :   跨服商业-战区钜惠
-- 创建时间     :   2025/03/31
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local CommonDefine = require("common.CommonDefine")
local ServerCommerceConf = require("excel.ServerCommerce")
local BuyConf = require("excel.buy")
local CommonDB = require("common.CommonDB")
local ObjHuman = require("core.ObjHuman")
local ServerCommerceManager = require("serverCommerce.ServerCommerceManager")
local ServerCommerceDefine = require("serverCommerce.ServerCommerceActDefine")

local COMMERCEACTFREEID = 1     -- 免费
local COMMERCEACTPAYID  = 2     -- 付费

----------------------------------------- 内部处理开始 -------------------------------------
-- 获取配置
local function CommerceActShop_GetCof()
    return ServerCommerceConf.CommerceShop
end

function CreatDB(human)
    if not human.db.ServerCommerce or not human.db.ServerCommerce.Shop then
        print("[CommerceActShop_CreatDB] 初始化数据失败 name = "..human.db.name)
        return false
    end

    local tConf = CommerceActShop_GetCof()
    for nID, v in ipairs(tConf) do
        human.db.ServerCommerce.Shop[nID] = 
        {
            [COMMERCEACTFREEID] = CommonDefine.COMMON_PRIZE_STATE_NOGET,
            [COMMERCEACTPAYID] = ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0
        }
    end

    print("[CommerceActShop_CreatDB] 战区钜惠 初始化数据完成 name = "..human.db.name)

    return true
end

-- 获取免费奖励状态
local function CommerceActShop_GetFreeStatus(human, nID)
    return human.db.ServerCommerce.Shop[nID][COMMERCEACTFREEID]
end

-- 设置免费奖励状态
local function CommerceActShop_SetFreeStatus(human, nID, nStatus)
    human.db.ServerCommerce.Shop[nID][COMMERCEACTFREEID] = nStatus
end

-- 获取付费奖励状态
local function CommerceActShop_GetPayStatus(human, nID)
    return human.db.ServerCommerce.Shop[nID][COMMERCEACTPAYID]
end

-- 设置付费奖励状态
local function CommerceActShop_SetPayStatus(human, nID, nStatus)
    human.db.ServerCommerce.Shop[nID][COMMERCEACTPAYID] = nStatus
end


----------------------------------------- 客户端请求处理开始 -------------------------------------
-- 查询数据
function CommerceActShop_Query(human)
    local tConf = CommerceActShop_GetCof()
    local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_SHOPQUERY
    tMsgData.nNowPoint = CommonDB.GetCommerceActInfo_Point()

    tMsgData.list[0] = #tConf
    local nLen = 1
    for nID, v in ipairs(tConf) do
        local tNodeData = tMsgData.list[nLen]
        nLen = nLen + 1
        tNodeData.nID = nID
        tNodeData.nNeedPoint = v.nPoint
        tNodeData.nFreeState = CommerceActShop_GetFreeStatus(human, nID)
        tNodeData.nPayState = CommerceActShop_GetPayStatus(human, nID)

        -- 免费物品
        tNodeData.tFreeItem[0] = #v.FreePrize
        for i, tData in ipairs(v.FreePrize) do
            Grid.makeItem(tNodeData.tFreeItem[i], tData[1], tData[2])
        end
        
        tNodeData.tPayItem[0] = #v.PayPrize
        for i, tData in ipairs(v.PayPrize) do
            Grid.makeItem(tNodeData.tPayItem[i], tData[1], tData[2])
        end

        -- 封装购买项物品
        BuyLogic.fontBuyItem(human, tNodeData.buyItem, v.nBuyID)

        --Grid.makeItem(tNodeData.pointItem, ServerCommerceDefine.COMERCEACT_POINT_GOODSID, v.nPoint)
    end

    Msg.send(tMsgData, human.fd)
end

-- 领取奖励
function CommerceActShop_GetPrize(human, nType)
    local tConf = CommerceActShop_GetCof()

    local tGoods = {}
    for nID, v in ipairs(tConf) do
        local nState = CommerceActShop_GetFreeStatus(human, nID)
        local bCanGet = false
        if COMMERCEACTPAYID == nType then
            nState = CommerceActShop_GetPayStatus(human, nID)
            bCanGet = ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3 == nState
        else
            bCanGet = CommonDefine.COMMON_PRIZE_STATE_CANGET == nState
        end

        if true == bCanGet then
            local tPrize
            if COMMERCEACTPAYID == nType then
                CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE4)
                tPrize = v.PayPrize
            else
                CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
                tPrize = v.FreePrize
            end

            for _, tData in ipairs(tPrize) do
                tGoods[tData[1]] = tGoods[tData[1]] or 0
                tGoods[tData[1]] = tGoods[tData[1]] + tData[2]
            end
        end
    end

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

        BagLogic.addItemList(human, tItem, "servercommerce")
        CommerceActShop_Query(human)
        ServerCommerceManager.CommerceAct_SendActInfo(human)
    end
end

----------------------------------------- 外部调用 -------------------------------------
-- 总积分改变
function onAllPointChange()
    local nNowPoint = CommonDB.GetCommerceActInfo_Point()
    local tConf = CommerceActShop_GetCof()
    
    local tOKID = {}
    for nID, v in ipairs(tConf) do
        if nNowPoint >= v.nPoint then
            table.insert(tOKID, nID)
        end
    end

    for _, human in pairs(ObjHuman.onlineUuid) do
        local bSendClient = false
        for _, nID in ipairs(tOKID) do
            local nStatus = CommerceActShop_GetFreeStatus(human, nID)
            if CommonDefine.COMMON_PRIZE_STATE_NOGET ==  nStatus then
                bSendClient = true
                CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
            end

            nStatus = CommerceActShop_GetPayStatus(human, nID)
            if ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0 == nStatus then
                -- 变成积分足够，未充值状态
                CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE1)
            elseif ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE2 == nStatus then
                -- 已经是购买状态，积分足够，直接变成可领取
                CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3)
            end
        end

        if true == bSendClient then
            CommerceActShop_Query(human)
        end
    end
end

function onCharge(human, price, funcID, buyID)
    local tConf = CommerceActShop_GetCof()
    local nNowPoint = CommonDB.GetCommerceActInfo_Point()
    print("[CommerceActShop_onCharge] nNowPoint = "..nNowPoint)

    local nChoseID = 0
    local tConfData = nil
    for nID, v in ipairs(tConf) do
        if v.nBuyID == buyID then
            local nPayState = CommerceActShop_GetPayStatus(human, nID)
            -- 状态为0 或者状态为积分达成未购买
            if ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0 == nPayState or ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE1 == nPayState then
                nChoseID = nID
                tConfData = v
            else
                print("[CommerceActShop_onCharge] 重复购买了 nBuyID = "..v.nBuyID)
            end

            break
        end
    end

    local tBuyCfg = BuyConf.buy[buyID]
    if not tBuyCfg then
        print("[onCharge] 不存在对应的购买项配置 buyID = "..buyID)
        return
    end

    if nChoseID == 0 or nil == tConfData then
        return
    end

    if nNowPoint >= tConfData.nPoint then
        CommerceActShop_SetPayStatus(human, nChoseID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3)
    else
        CommerceActShop_SetPayStatus(human, nChoseID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE2)
    end

    CommerceActShop_Query(human)

    ServerCommerceManager.CommerceAct_SendActInfo(human)
end

-- 登录检测积分
function onLogin(human)
    local nNowPoint = CommonDB.GetCommerceActInfo_Point()
    local tConf = CommerceActShop_GetCof()

    for nID, v in ipairs(tConf) do
        if nNowPoint >= v.nPoint then
            local nStatus = CommerceActShop_GetFreeStatus(human, nID)
            if CommonDefine.COMMON_PRIZE_STATE_NOGET ==  nStatus then
                CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
            end

            local nPayState = CommerceActShop_GetPayStatus(human, nID)
            -- 已购买积分未达成
            if ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE2 == nPayState then
                CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3)
            end
        end
    end
end

function isRed(human)
    local tConf = CommerceActShop_GetCof()

    for nID, v in ipairs(tConf) do
        local nFreeStatus = CommerceActShop_GetFreeStatus(human, nID)
        local nPayState = CommerceActShop_GetPayStatus(human, nID)
        if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus or
            ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3 == nPayState then
            return true
        end
    end

    return false
end

-- function IsHaveBuyID(nBuyID)
--     local tConf = CommerceActShop_GetCof()
--     for nID, v in ipairs(tConf) do
--         if v.nBuyID == nBuyID then
--             return true
--         end
--     end

--     return false
-- end

-- function GetRemainNum(human, nBuyID)
--     local tConf = CommerceActShop_GetCof()
--     for nID, v in ipairs(tConf) do
--         if v.nBuyID == nBuyID then
--             local nPayState = CommerceActShop_GetPayStatus(human, nID)
--             if ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0 == nPayState or ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE1 == nPayState then
--                 return 1
--             else
--                 return 0
--             end
--         end
--     end

--     return 0
-- end