--------------------------------
-- 文件名       ：  OpenServerActPowerUp.lua
-- 文件说明     ：  开服7天活动 - 战力冲刺
-- 创建时间     :   2024/11/14
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local OpenAct = require("present.OpenAct")
local PresentExcel = require("excel.present")
local OpenActExcel = require("excel.openAct")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local MailManager = require("mail.MailManager")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local KingWorldLogic = require("present.KingWorldLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local SevenDayGiftLogic = require("present.SevenDayGiftLogic")
local CommonDB = require("common.CommonDB")
local BuyLogic = require("topup.BuyLogic")
local GuideLogic = require("guide.GuideLogic")
local Log = require("common.Log")

local tPrizeByID = nil

POWERGIFT_STATE_NONE    =   0       -- 不可领取
POWERGIFT_STATE_CANGET  =   1       -- 可领取
POWERGIFT_STATE_FINISH  =   2       -- 已领取

----------------------------------------- DB数据操作 -------------------------------------

-- 初始化DB数据
local function ActPowerUp_InitDB(human)
    if not human then
        return false
    end

    human.db.PowerGift = {}
    return true
end

-- 获取DB数据
local function ActPowerUp_GetDB(human)
    if not human or not human.db.PowerGift then
        return nil
    end
    
    return human.db.PowerGift
end

-- 删除DB数据 
local function ActPowerUp_DelDB(human)
    if not human or not human.db.PowerGift then
        return
    end
    
    human.db.PowerGift = nil
end

-- 创建DB数据
local function ActPowerUp_CreateDB(human)
    if not human then
        return false
    end

    if not human.db.PowerGift then
        local bRet = ActPowerUp_InitDB(human)
        if false == bRet then
            print("[ActPowerUp_CreateDB] 初始化BD数据失败")
            return false
        end

        if tPrizeByID then
            for nID, value in pairs(tPrizeByID) do
                human.db.PowerGift[nID] = POWERGIFT_STATE_NONE
            end
        else
            print("[ActPowerUp_CreateDB] 不存在对应的ID对应配置")
            return false
        end
    end

    return true
end

-- 获取某个数据状态
local function ActPowerUp_GetDBIDState(human, nID)
    if not human or 0 >= nID then
        return POWERGIFT_STATE_NONE
    end

    local tDBPrize = ActPowerUp_GetDB(human)
    if nil == tDBPrize then
        return POWERGIFT_STATE_NONE
    end

    if not tDBPrize[nID] then
        tDBPrize[nID] = POWERGIFT_STATE_NONE
    end

    return tDBPrize[nID]
end

-- 设置某个数据状态
local function ActPowerUp_SetDBIDState(human, nID, nState)
    if not human or 0 >= nID then
        return false
    end

    local tDBPrize = ActPowerUp_GetDB(human)
    if nil == tDBPrize then
        return false
    end

    tDBPrize[nID] = nState
    return true
end
----------------------------------------- 内部函数判断 -------------------------------------
-- 获取配置
local function ActPowerUp_GetConfig()
    return OpenActExcel.PowerUp
end

-- 日志写入
local function ActPowerUp_WriteLog(szText)
    Log.write(Log.LOGID_OSS_OPENSERVER_ACT, szText)
end

-- 报错打印
local function ActPowerUp_PrintInfo(szFuncName,szText)
    print(szFuncName.." "..szText)
end

-- 客户端回包
local function ActPowerUp_SendClient(fd, data)
    Msg.send(data, fd)
    ActPowerUp_PrintInfo("[ActPowerUp_SendClient]", "发送给客户端结束")
end

-- 活动是否开启
local function ActPowerUp_IsOpen(human)
    local szFuncName = "ActPowerUp_IsOpen"
    local szText = ""
    if not human then
        szText = "参数不正确 "
        ActPowerUp_PrintInfo(szFuncName, szText)
        return false
    end

    local nOpenLv = YunYingLogic.getOpenLvByPanelID(PanelDefine.PANEL_ID_3609)
    if human.db.lv < nOpenLv then
        return false
    end

    local flag = OpenAct.getOpenActTime(OpenAct.OPEN_ACT_SERVER_GIFT)
    if not flag then
		return false
	end

    return true
end

-- 活动剩余时间
local function ActPowerUp_GetLeftTime()
    local openDay = CommonDB.getServerOpenDay()
    if openDay == nil then
        return 0
    end

    local OpenActConfig = OpenActExcel.openAct[1]
    if not OpenActConfig then return 0 end

    local sDay = OpenActConfig.sDay
    local eDay = OpenActConfig.eDay

    if openDay >= sDay and openDay <= eDay then
        local time = os.time()
        local openTime = CommonDB.getServerOpenTime()
        if openTime == 0 then
            return 0
        end

        local endTime = Util.getDayStartTime(openTime) + eDay * 86400
        return endTime - time
    end

    return 0
end

-- 红点
local function ActPowerUp_CheckRed(human)
    local szFuncName = "ActPowerUp_CheckRed"
    local szText = ""
    if not human then
        szText = "参数不正确"
        ActPowerUp_PrintInfo(szFuncName, szText)
        return false
    end

    ActPowerUp_CreateDB(human)

    local tDBData = ActPowerUp_GetDB(human)
    if not tDBData then
        szText = "获取玩家DB奖励数据状态表失败"
        ActPowerUp_PrintInfo(szFuncName, szText)
        return false
    end

    for k, v in pairs(tDBData) do
        if POWERGIFT_STATE_CANGET == v then
            return true
        end
    end

    return false
end

----------------------------------------- 活动模板调用 -------------------------------------
-- 是否开启
function isOpen(human, YYInfo, funcConfig)
    print("[ActPowerUp_isOpen] 进入了判断")
    return ActPowerUp_IsOpen(human)
end

-- 活动剩余时间
function getLeftTime()
    print("[ActPowerUp_getLeftTime] 获取剩余时间")

    return ActPowerUp_GetLeftTime()
end

-- 是否存在红点
function isRed(human)
    print("[ActPowerUp_isRed] 获取红点")
    return ActPowerUp_CheckRed(human)
end

----------------------------------------- 客户端协议请求 -------------------------------------
-- 请求活动协议
function ActPowerUp_Query(human)
    if not human then
        return
    end

    local nCreatRet = ActPowerUp_CreateDB(human)
    if false == nCreatRet then
        ActPowerUp_PrintInfo("[ActPowerUp_Query]", "初始化DB数据失败")
        return
    end

    local tPrize = ActPowerUp_GetConfig()
    if not tPrize then
        return
    end

    local tMsgData = Msg.gc.GC_PRESENT_OPEN_POWERUP_QUERY
    tMsgData.nNowPower = human.db.zhandouli
    tMsgData.list[0] = 0

    for nID, v in pairs(tPrize) do
        tMsgData.list[0] = tMsgData.list[0] + 1
 
        local tPrizeData = tMsgData.list[tMsgData.list[0]]
        tPrizeData.nID = nID
        tPrizeData.nNeedPower = v.nPower
        tPrizeData.nState = ActPowerUp_GetDBIDState(human, nID)
        
        local nPrizeLne = #v.tPrize
        tPrizeData.item[0] = nPrizeLne
        
        -- ActPowerUp_PrintInfo("[ActPowerUp_Query]", "遍历的奖励信息 nID = "
        --         .. nID .." nPower = "..v.nPower .. " nState = ".. tPrizeData.nState.." nPrizeLne = "..nPrizeLne)

        for j = 1, nPrizeLne do
            local nGoodsID = v.tPrize[j][1]
            local nGoodsNum = v.tPrize[j][2]
            ActPowerUp_PrintInfo("[ActPowerUp_Query]", "物品信息 nGoodsID = "..nGoodsID .. " nGoodsNum = "..nGoodsNum)
            
            Grid.makeItem(tPrizeData.item[j], nGoodsID, nGoodsNum)
            if v.tPrize[j][3] then
                tPrizeData.item[j].rare = v.tPrize[j][3]
            end
        end
    end

    -- local tPrintTable = Util.printTable(tMsgData)

    Msg.send(tMsgData, human.fd)
    ActPowerUp_PrintInfo("[ActPowerUp_Query]", "获取到的奖励长度为 nIndex = "..tMsgData.list[0])
end


-- 请求领取奖励
function ActPowerUp_GetPrize(human, nID)
    if not human or 0 >= nID then
        return
    end

    if not tPrizeByID or not tPrizeByID[nID] then
        return
    end

    local nNowPower = human.db.zhandouli
    local tMsgData = Msg.gc.GC_PRESENT_OPEN_POWERUP_GETPRIZE
    tMsgData.nID = nID
    local nNowState = ActPowerUp_GetDBIDState(human, nID)
    if POWERGIFT_STATE_CANGET ~= nNowState then
        ActPowerUp_PrintInfo("ActPowerUp_GetPrize", "请求领取的奖励状态不正确 nID = "..nID)
        tMsgData.nState = 0

        ActPowerUp_SendClient(human.fd, tMsgData)
        return
    end

    local tGoodsInfo = tPrizeByID[nID].tPrize
    -- if nNowPower < tGoodsInfo.nPower then
    --     ActPowerUp_PrintInfo("ActPowerUp_GetPrize", "战斗力不足不应该领取 nID = "..nID)
    --     return
    -- end

    tMsgData.nState = 1

    -- 设置状态
    if false == ActPowerUp_SetDBIDState(human, nID, POWERGIFT_STATE_FINISH) then
        ActPowerUp_PrintInfo("ActPowerUp_GetPrize", "设置数据状态失败 nID = "..nID)
        tMsgData.nState = 0

        ActPowerUp_SendClient(human.fd, tMsgData)
        return
    end

    -- 发奖励
    local tItems = {}
    for _, v in pairs(tGoodsInfo) do
        table.insert(tItems, v)
    end
    -- for i = 1, #tGoodsInfo do
    --     local nGoodsID = tGoodsInfo[i][1]
    --     local nGoodsNum = tGoodsInfo[i][2]

    --     BagLogic.addItem(human, nGoodsID, nGoodsNum,"open_server_PowerUpPrize")
    -- end

    BagLogic.addItemList(human, tItems, "open_server_PowerUpPrize")
	BagLogic.sendItemGetList(human, tItems, "open_server_PowerUpPrize")

	for k, v in pairs(funcID) do
	    YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3609)
        break
    end
    
    ActPowerUp_Query(human)
end


----------------------------------------- 其他模块调用 -------------------------------------
-- 用于起服时映射奖励ID对应配置
function Init()
    local tPrize = ActPowerUp_GetConfig()
    if not tPrize then
        ActPowerUp_PrintInfo("Init", "起服获取不到配置")
        return
    end
    
    tPrizeByID = {}
   -- Util.printTable(tPrize)

    for nID, v in pairs(tPrize) do
        if tPrizeByID[nID] then
            ActPowerUp_PrintInfo("Init", "配置了重复的奖励ID")
            return
        end

        tPrizeByID[nID] = v
    end
end

-- 战力改变回调
function ActPowerUp_PowerChange(human, nNewPower)
    -- 活动未开启
    if false == ActPowerUp_IsOpen(human) then
        return
    end

    if not human or 0 >= nNewPower then
        return
    end

    local tPrizeConfig = ActPowerUp_GetConfig()
    if not tPrizeConfig then
        return
    end

    local nSendClient = false
    for nID, v in pairs(tPrizeConfig) do
        local nState = ActPowerUp_GetDBIDState(human, nID)
        if POWERGIFT_STATE_NONE == nState then
            if nNewPower >= v.nPower then
                ActPowerUp_SetDBIDState(human, nID, POWERGIFT_STATE_CANGET)
                nSendClient = true
            end
        end
    end

    if true == nSendClient then
        ActPowerUp_Query(human)
    end

    return
end

function ActPowerUp_GMClear(human)
    ActPowerUp_DelDB(human)
    print("[ActPowerUp_GMClear] 战力冲刺数据清空结束")
end