-- 通用节日活动 - 登录豪礼
-- db
--[=[
    human.db.absAct[actId] = {
        getRecordData = {
            [gitftType] = {
                [day1] = true,
                [day3] = true,
            },
        },

        buyGiftType = nil, -- 已经购买的礼包类型
    }   
]=]--

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local AbsActLogic = require("absAct.AbsActLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local LoginGiftConfig = require("excel.commonact").loginreward
local CommonDefine = require("common.CommonDefine")
local Util = require("common.Util")
local YunYingLogic = require("yunying.YunYingLogic")
local AbsActExcel = require("excel.absAct")
local BuyLogic = require("topup.BuyLogic")

local LOGTYPE = "CommonActLoginGift"
local COMMONACT_LOGINGIFT_ID = 7505


local function getActData(human)
    return human.db.absAct[COMMONACT_LOGINGIFT_ID]
end

local function updateGetRecordData(human, giftType, dayIdx)
    human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData = human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData or {}
    human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData[giftType] = human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData[giftType] or {}
    human.db.absAct[COMMONACT_LOGINGIFT_ID].getRecordData[giftType][dayIdx] = true
end

local function updateBuyGiftType(human, giftType)
    human.db.absAct[COMMONACT_LOGINGIFT_ID].buyGiftType = giftType
end



local function isOpenAct(human, funcID)
    return AbsActLogic.isStarted(human, funcID)
end

local function transformCfg()
    local tb = {}
    for _, giftCfg in ipairs(LoginGiftConfig) do
        if not tb[giftCfg.giftType] then
            tb[giftCfg.giftType] = {}
        end
        local t = tb[giftCfg.giftType]
        t[#t+1] = giftCfg
    end

    return tb
end

local function getAwardState(human, giftType, dayIdx)
    local actData = getActData(human)
    local diffDays = Util.diffDay(actData.realStartTime) + 1
    local getRecordData = actData and actData.getRecordData or {}
    local nowGiftTypeGetRecord = getRecordData[giftType] or {}
    local buyGiftType = actData and actData.buyGiftType

    local isBuy = true
    local state = CommonDefine.COMMON_PRIZE_STATE_CANGET

    if giftType ~= 1 and (not buyGiftType or buyGiftType ~= giftType) then -- 需要充值的礼包类型
        state = CommonDefine.COMMON_PRIZE_STATE_NOGET
        isBuy = false
    end

    if isBuy then
        if diffDays < dayIdx then
            state = CommonDefine.COMMON_PRIZE_STATE_NOGET
        else
            if nowGiftTypeGetRecord[dayIdx] then
                state = CommonDefine.COMMON_PRIZE_STATE_GET
            end
        end
    end

    return state
end

-- 检查是否有可领取的奖励
local function isCanGet(human)
    local actData = getActData(human)
    if not actData then
        return false
    end

    local type_2_cfg = transformCfg()

    for giftType, giftCfgArr in pairs(type_2_cfg) do
        for _, giftCfg in ipairs(giftCfgArr) do
            local state = getAwardState(human, giftType, giftCfg.dayIdx)
            if state == CommonDefine.COMMON_PRIZE_STATE_CANGET then
                return true
            end
        end
    end

    return false
end
-- 填充协议结构
local function populateGiftMsg(human, net, giftCfg, nowGiftType)
    net.reward[0] = #giftCfg.reward
    for i, itemInfo in ipairs(giftCfg.reward) do
        Grid.makeItem(net.reward[i], itemInfo[1], itemInfo[2])
    end

    net.reallyDays = giftCfg.dayIdx
    net.rewardState = getAwardState(human, nowGiftType, giftCfg.dayIdx)
    BuyLogic.fontBuyItem(human, net.buyMsg, giftCfg.buyId)
    net.giftType = nowGiftType
end
-- 主动刷新一次红点
local function updateRedDot(human)
    YunYingLogic.sendBanner(human)
    local config = AbsActExcel.absActivity[COMMONACT_LOGINGIFT_ID]
    YunYingLogic.sendGroupUpdate(YYInfo[COMMONACT_LOGINGIFT_ID], human, config.panelID)
end




function isRed(human, YYInfo, funcConfig)
    local state = isOpenAct(human, funcConfig and funcConfig.funcID)
    if not state then
        return false
    end

    return isCanGet(human)
end

function isOpen(human, YYInfo, funcConfig)
    return isOpenAct(human, funcConfig and funcConfig.funcID)
end

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

function onCharge(human, nBuyID, buyNum)
    if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
        return
    end

    local actData = getActData(human)
    local buyGiftType = actData and actData.buyGiftType
    if buyGiftType then
        return
    end

    local type_2_cfg = transformCfg()

    for giftType, giftCfgArr in pairs(type_2_cfg) do
        local singleCfg = giftCfgArr[1]
        if singleCfg.buyId == nBuyID then
            updateBuyGiftType(human, giftType)
            updateRedDot(human)
            CommonActLoginGift_Query(human)
            break
        end
    end
end

function updateDaily(human, funcID)
    if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
        return
    end

    updateRedDot(human)
end




-- 查询
function CommonActLoginGift_Query(human)
    if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end
    local msgRet = Msg.gc.GC_ABS_COMONACT_LOGINGIFT_QUERY
    msgRet.giftDatafree[0] = 0
    msgRet.giftData68[0] = 0
    msgRet.giftData128[0] = 0
    msgRet.giftData328[0] = 0
    msgRet.giftData648[0] = 0
    msgRet.isEnd = 0
    msgRet.isStart = 1
    msgRet.buyGiftType = 0

    local actData = getActData(human)
    msgRet.buyGiftType = actData and actData.buyGiftType or 0

    local len, msgOnceLen = 0, 5
    local type_2_cfg = transformCfg()
    local configNum = type_2_cfg[1] and #type_2_cfg[1] or 0
    local num = configNum

    for i=1, configNum do
        len = len + 1

        if type_2_cfg[1] then
            msgRet.giftDatafree[0] = len
            populateGiftMsg(human, msgRet.giftDatafree[len], type_2_cfg[1][i], 1)
        end

        if type_2_cfg[2] and type_2_cfg[2][i] then
            msgRet.giftData68[0] = len
            populateGiftMsg(human, msgRet.giftData68[len], type_2_cfg[2][i], 2)
        end

        if type_2_cfg[3] and type_2_cfg[3][i] then
            msgRet.giftData128[0] = len
            populateGiftMsg(human, msgRet.giftData128[len], type_2_cfg[3][i], 3)
        end

        if type_2_cfg[4] and type_2_cfg[4][i] then
            msgRet.giftData328[0] = len
            populateGiftMsg(human, msgRet.giftData328[len], type_2_cfg[4][i], 4)
        end

        if type_2_cfg[5] and type_2_cfg[5][i] then
            msgRet.giftData648[0] = len
            populateGiftMsg(human, msgRet.giftData648[len], type_2_cfg[5][i], 5)
        end

        if len >= msgOnceLen then
            num = num - len
            if num <= 0 then
                msgRet.isEnd = 1
                return Msg.send(msgRet, human.fd)
            end

            Msg.send(msgRet, human.fd)
            len = 0
            msgRet.isStart = 0
        end
    end

    if len > 0 then
        msgRet.isEnd = 1
        Msg.send(msgRet, human.fd)
    end
end

-- 领取奖励
function CommonActLoginGift_GetReward(human, targetGiftType)
    if not isOpenAct(human, COMMONACT_LOGINGIFT_ID) then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end
    local type_2_cfg = transformCfg()
    local giftArr = type_2_cfg[targetGiftType]
    if not giftArr then
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end

    local actData = getActData(human)
    local buyGiftType = actData and actData.buyGiftType
    if targetGiftType ~= 1 and (not buyGiftType or buyGiftType ~= targetGiftType) then
        return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
    end

    local itemArr = {}
    for _, giftCfg in ipairs(giftArr) do
        local state = getAwardState(human, targetGiftType, giftCfg.dayIdx)
        if state == CommonDefine.COMMON_PRIZE_STATE_CANGET then
            for _, itemInfo in ipairs(giftCfg.reward) do
                itemArr[#itemArr+1] = { itemInfo[1], itemInfo[2] }
            end

            -- 记录领取
            updateGetRecordData(human, targetGiftType, giftCfg.dayIdx)
        end
    end

    if #itemArr == 0 then
        return Broadcast.sendErr(human, Lang.HO_NO_CAN_GET)
    end

    -- 发放道具
    BagLogic.addItemList(human, itemArr, LOGTYPE)

    -- 更红点
    updateRedDot(human)

    -- 推送最新数给客户端
    CommonActLoginGift_Query(human)
end