--------------------------------
-- 文件名       ：  WeekendLoopActHeroStar.lua
-- 文件说明     ：  周末冲刺活动-英雄升星
-- 创建时间     :   2024/11/26
-- 创建人       :   FC
--------------------------------
local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local WeekLoopActDef = require("WeekendLoopActivity.WeekendLoopActDefine")
local WeekLoopActCof = require("excel.WeekLoopAct")
local CommonDB = require("common.CommonDB")
local MailManager = require("mail.MailManager")
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 HeroExcel = require("excel.hero")
local WeekendLoopActManger = require("WeekendLoopActivity.WeekendLoopActManager")

----------------------------------------- 内部处理开始 -------------------------------------
-- 下发数据
local function WeekActHeroStar_SendData(tMsgData, fd)
    Msg.send(tMsgData, fd)
end

-- 获取配置
local function WeekActHeroStar_GetConfig()
    if not WeekLoopActCof then
        return nil
    end

    return WeekLoopActCof.HeroStarUp
end

-- 获取选择的英雄ID
local function WeekActHeroStar_GetHeroID(human)
    if not human then
        return -1
    end

    if not human.db.nWeekHeroID then
        return -1
    end
    
    return human.db.nWeekHeroID
end

-- 重置选择的英雄最大星数
local function WeekActHeroStar_RestHeroStar(human)
    human.db.nWeekHeroStar = 0
end

-- 设置当前英雄最大星数
local function WeekActHeroStar_SetHeroStar(human, nStar)
    human.db.nWeekHeroStar = math.max(human.db.nWeekHeroStar, nStar)
end

local function WeekActHeroStar_GetHeroStar(human)
    return human.db.nWeekHeroStar
end

-- 设置英雄ID
local function WeekActHeroStar_SetHeroID(human, nHeroID)
    if not human or not nHeroID then
        print("[WeekActHeroStar_SetHeroID] 设置英雄ID失败 nHeroID = "..nHeroID)
        return
    end

    human.db.nWeekHeroID = nHeroID
    WeekActHeroStar_RestHeroStar(human)
end

-- 初始化奖励信息
local function WeekActHeroStar_ResetPrize(human)
    if not human then
        return false
    end

    local tConfig = WeekActHeroStar_GetConfig()
    if not tConfig then
        return false
    end

    if not human.db.tWeekHeroPrize then
        human.db.tWeekHeroPrize = {}
    end

    for nID, v in pairs(tConfig) do
        human.db.tWeekHeroPrize[nID] = WeekLoopActDef.WEEKACT_STATE_NONE
    end
    
    return true
end

-- 获取奖励表
local function WeekActHeroStar_GetDBPrize(human)
    if not human then
        return nil
    end

    if not human.db.tWeekHeroPrize then
        return nil
    end
    
    return human.db.tWeekHeroPrize
end

-- 获取奖励ID状态
local function WeekActHeroStar_GetPrizeStatus(human, nID)
    if not human then
        return WeekLoopActDef.WEEKACT_STATE_NONE
    end

    local tPrize = WeekActHeroStar_GetDBPrize(human)
    if not tPrize or not tPrize[nID] then
        return WeekLoopActDef.WEEKACT_STATE_NONE
    end
    
    return human.db.tWeekHeroPrize[nID]
end

-- 设置奖励ID状态
local function WeekActHeroStar_SetPrizeStatus(human, nID, nStatus)
    if not human then
        return false
    end

    local tPrize = WeekActHeroStar_GetDBPrize(human)
    if not tPrize or not tPrize[nID] then
        return false
    end

    tPrize[nID] = nStatus

    return true
end

----------------------------------------- 外部调用 -------------------------------------
-- 重置数据
function WeekActHeroStar_ResetData(human)
    if not human then
        return
    end

    if false == WeekActHeroStar_ResetPrize(human) then
        print("[WeekActHeroStar_ResetData] 重置英雄升星数据失败")
        return
    end

    WeekActHeroStar_SetHeroID(human, 0)

    print("[WeekActHeroStar_ResetData] 英雄升星 数据重置结束 ")
end

-- 活动结束
function WeekActHeroStar_End(human)
    if not human then
        return
    end

    print("[WeekActHeroStar_End] 英雄升星 活动结束处理开始")

    local tPrize = WeekActHeroStar_GetDBPrize(human)
    local tPrizeConfig = WeekActHeroStar_GetConfig()
    if not tPrize or not tPrizeConfig then
        return
    end

    local tMailConfig = MailExcel.mail[WeekLoopActDef.WEEKACT_HEROSTAR_MAILID]
    if not tMailConfig then
        return
    end

    local items = { }
    for nID, v in pairs(tPrizeConfig) do
        if WeekLoopActDef.WEEKACT_STATE_CANGET == tPrize[nID] then
            for _, data in pairs(v.prize) do
                items[#items + 1] = { data[1], data[2] }
            end
            WeekActHeroStar_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH)
        end
    end

    local title = tMailConfig.title
    local content = tMailConfig.content
    local senderName = tMailConfig.senderName
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)

    WeekActHeroStar_RestHeroStar(human)

    print("[WeekActHeroStar_End] 英雄升星 活动结束处理完成")
end

-- 是否有红点
function isRed(human)
    local tPrize = WeekActHeroStar_GetConfig()
    if not tPrize then
        return false
    end

    for nID, v in pairs(tPrize) do
        if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActHeroStar_GetPrizeStatus(human, nID) then
            return true
        end
    end

    return false
end

----------------------------------------- 客户端请求 -------------------------------------
-- 请求英雄升星活动信息
function WeekActHeroStar_Query(human)
    if not human then
        return
    end

    local tPrizeConfig = WeekActHeroStar_GetConfig()
    local tPrize = WeekActHeroStar_GetDBPrize(human)

    if not tPrizeConfig or not tPrize then
        return
    end
    
    local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_HEROQUERY

    -- 奖励信息
    tMsgData.list[0] = 0
    for nID, v in pairs(tPrizeConfig) do
        tMsgData.list[0] = tMsgData.list[0] + 1

        local tPrizeData = tMsgData.list[tMsgData.list[0]]
        tPrizeData.nID = nID
        tPrizeData.nState = WeekActHeroStar_GetPrizeStatus(human, nID)

        local nPrizeLne = #v.prize
        tPrizeData.item[0] = nPrizeLne
        --print("[WeekActHeroStar_Query] nItemLen = "..tPrizeData.item[0].." nID = "..nID)

        for j = 1, nPrizeLne do
            local nGoodsID = v.prize[j][1]
            local nGoodsNum = v.prize[j][2]

            --print("[WeekActHeroStar_Query] nGoodsID = "..nGoodsID.. " nGoodsNum = "..nGoodsNum)
            Grid.makeItem(tPrizeData.item[j], nGoodsID, nGoodsNum)
        end
    end
    --print("[WeekActHeroStar_Query] ListLen = ".. tMsgData.list[0])

    local nChoseHeroID = WeekActHeroStar_GetHeroID(human)
    if 0 < nChoseHeroID then
        tMsgData.HeroData[0] = 1
        HeroGrid.makeHeroSimpleByID(tMsgData.HeroData[1], nChoseHeroID)
    else
        tMsgData.HeroData[0] = 0
    end

    WeekActHeroStar_SendData(tMsgData, human.fd)
    print("[WeekActHeroStar_Query] 消息发送完成 ")
end

-- 英雄升星- 请求所有英雄简略信息
function WeekActHeroStar_GetAllHeroInfo(human)
    if not human then
        return
    end

    local tMsgData = Msg.gc.GC_WEEKLOOP_ACT_HEROGETSIMPLIFYINFO

	local tHeroData = HeroExcel.hero
	tMsgData.tHeroData[0] = 0

	print("[getACTHeroInfo] 获取数据开始 ")

    for nID, v in pairs(tHeroData) do
        if not WeekLoopActDef.TWEEKACT_HEROSTAR_IGNORE[nID] then
            -- ssr 和 ur
            if WeekLoopActDef.TWEEKACT_HEROSTAR_RARITY[v.grade] then
                tMsgData.tHeroData[0] = tMsgData.tHeroData[0] + 1

                HeroGrid.makeHeroSimpleByID(tMsgData.tHeroData[tMsgData.tHeroData[0]], nID)
            end
        end
    end
   
    WeekActHeroStar_SendData(tMsgData, human.fd)
end

-- 英雄升星 - 选择英雄
function WeekActHeroStar_ChoseHero(human, nID)
    if not human or 0 >= nID then
        return
    end

    Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, "[WeekActHeroStar_ChoseHero] 玩家选择了英雄 nHeroID = "..nID.." _id = "..human.db._id.." "..human.db.name)

    WeekActHeroStar_SetHeroID(human, nID)

    WeekActHeroStar_Query(human)
end


-- 请求领取奖励
function WeekActHeroStar_GetPrize(human)
    if not human then
        return
    end

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

    local tItemList = {}
    for nID, v in pairs(tPrize) do
        if WeekLoopActDef.WEEKACT_STATE_CANGET == WeekActHeroStar_GetPrizeStatus(human, nID) then
            if false == WeekActHeroStar_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_FINISH) then
                print("[WeekActHeroStar_GetPrize] 奖励领取失败 nID = "..nID)
                Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, "[WeekActHeroStar_GetPrize] 设置奖励状态失败 nID = "..nID.." _id = "..human.db._id.." "..human.db.name)
                break
            end

            for _, data in ipairs(v.prize) do
                local nItemID = data[1]
                local nItemNum = data[2]

                tItemList[nItemID] = tItemList[nItemID] or 0
                tItemList[nItemID] = tItemList[nItemID] + nItemNum
            end
        end
    end

    if nil ~= _G.next(tItemList) then
        local tGoodsInfo = {}
        for k, v in pairs(tItemList) do
            table.insert(tGoodsInfo, {k,v})

            -- 获取奖励写日志
            Log.write(Log.LOGID_OSS_WEEKLOOP_ACT, "[WeekActHeroStar_GetPrize] 玩家获取到奖励 name = "
                    ..human.db.name.." _id = "..human.db._id.." nGoodsID = "..k.." nGoodsNum = "..v)
        end

        BagLogic.addItemList(human, tGoodsInfo, "week_loop_act")

        BagLogic.sendItemGetList(human, tItemList, "week_loop_act")
    end

    WeekActHeroStar_Query(human)
    WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
end

-- 英雄升星
function WeekActHeroStar_HeroStarUp(human, nHeroID, nStar)
    if not human or 0 >= nHeroID or 0 >= nStar then
        return
    end

    local nChoseHeroID = WeekActHeroStar_GetHeroID(human)
    local tConfig = WeekActHeroStar_GetConfig()
    if -1 >= nChoseHeroID or not tConfig then
        return
    end

    if nHeroID ~= nChoseHeroID then
        return
    end

    for nID, v in pairs(tConfig) do
        if nID == nStar then
            local nStatus = WeekActHeroStar_GetPrizeStatus(human, nID)
            if WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
                WeekActHeroStar_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
            end
        end
    end

    WeekActHeroStar_SetHeroStar(human, nStar)
    WeekActHeroStar_Query(human)
    WeekendLoopActManger.WeekLoopACT_SendActInfo(human)
end

function onLogin(human)
    if not human.db.nWeekHeroID or not human.db.nWeekHeroStar or not human.db.tWeekHeroPrize then
        return
    end

    -- local tConfig = WeekActHeroStar_GetConfig()
    -- local nNowStar = WeekActHeroStar_GetHeroStar(human)
    -- if tConfig then
    --     for nID, v in pairs(tConfig) do
    --         if nNowStar >= nID then
    --             local nStatus = WeekActHeroStar_GetPrizeStatus(human, nID)
    --             if WeekLoopActDef.WEEKACT_STATE_NONE == nStatus then
    --                 WeekActHeroStar_SetPrizeStatus(human, nID, WeekLoopActDef.WEEKACT_STATE_CANGET)
    --             end
    --         end
    --     end
    -- end

end