--新商业化活动——登录豪礼
--db
--[=[
    human.db.absAct[id] = {
        loginDay = nil, --活动期间登录的天数
        getRewardRecord = nil, {1,3,2,6} --已领取的奖励记录, 记录的是第几天
    }
]=]--

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 AbsActExcel = require("excel.absAct")
local YunYingLogic = require("yunying.YunYingLogic")
local Util = require("common.Util")
local CycleActivityLogic = require("yunying.CycleActivity")


local LOGTAG = "AbsLoginReward" --日志标识


local function check(tbl, day)
    if not tbl or not day then
        return false
    end

    for _, v in ipairs(tbl) do
        if v == day then
            return true
        end
    end

    return false
end

function isOpen(human, YYInfo, funcConfig)
    -- local state, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    local state, endTime, startTime = CycleActivityLogic.isStarted(human, funcConfig.funcID)
    return state, endTime, startTime
end

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

--红点判断
function isRed(human, YYInfo, funcConfig)
    local actData = human.db.absAct[funcConfig.funcID]
    if not actData then
        return false
    end
    
    -- local loginDay = actData.loginDay
    -- if not loginDay then
    --     return false
    -- end
    -- local _, _, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    local _, _, startTime = CycleActivityLogic.isStarted(human, funcConfig.funcID)

    local config = AbsActExcel.AbsLoginReward
    local getRewardRecord = actData.getRewardRecord
    if getRewardRecord and #getRewardRecord >= #config then
        return false
    end

    local loginDay = Util.diffDay(startTime) + 1
    if getRewardRecord and #getRewardRecord >= loginDay then
        return false
    end

    return true
end

function onLogin(human, id)
    -- local state, _, realStartTime = AbsActLogic.isStarted(human, id)
    local state, _, realStartTime = CycleActivityLogic.isStarted(human, id)
    if not state then
        return
    end

    local actData = human.db.absAct[id]
    if not actData then
        return
    end

    --处理活动更到线上前，玩家当天已经登录，没有记录当天登录的情况
    -- if not actData.loginDay then
    --     actData.loginDay = 1
    --     --红点刷新
    --     YunYingLogic.sendBanner(human)
    --     local config = AbsActExcel.absActivity[id]
    --     YunYingLogic.sendGroupUpdate(YYInfo[id], human, config.panelID)
    -- end

    local loginDay = Util.diffDay(realStartTime) + 1
    local getRewardRecord = actData.getRewardRecord
    local config = AbsActExcel.AbsLoginReward

    if not getRewardRecord or (#getRewardRecord <= loginDay and #getRewardRecord <= #config) then
        --红点刷新
        YunYingLogic.sendBanner(human)
        local otherConfig = AbsActExcel.absActivity[id]
        YunYingLogic.sendGroupUpdate(YYInfo[id], human, otherConfig.panelID)
    end
end

--跨天函数
function updateDaily(human, id)
    -- local state = AbsActLogic.isStarted(human, id)
    local state = CycleActivityLogic.isStarted(human, id)

    if not state then
        return
    end

    local actData = human.db.absAct[id]
    if not actData then
        return
    end

    -- actData.loginDay =( actData.loginDay or 0) + 1

    --红点刷新
    YunYingLogic.sendBanner(human)
    local config = AbsActExcel.absActivity[id]
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, config.panelID)
end

--查询
function Query(human, id)
    -- local state, _, realStartTime = AbsActLogic.isStarted(human, id)
    local state, _, realStartTime = CycleActivityLogic.isStarted(human, id)

    if not state then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[id]
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local msgRet = Msg.gc.GC_LOGINREWARD_QUERY
    local dayRewardData = msgRet.dayRewardData

    local config = AbsActExcel.AbsLoginReward
    dayRewardData[0] = #config
    --local loginDay = actData.loginDay

    local loginDay = Util.diffDay(realStartTime) + 1
    
    local getRewardRecord = actData.getRewardRecord

    for day, cfg in ipairs(config) do
        dayRewardData[day].day = day
        dayRewardData[day].state = 0
        local isGet = check(getRewardRecord, day)

        if loginDay and loginDay >= day and isGet then
            dayRewardData[day].state = 2
        elseif loginDay and loginDay >= day and not isGet then
            dayRewardData[day].state = 1
        end

        local rewardInfo = dayRewardData[day].rewardInfo
        rewardInfo[0] = #cfg.item
        for idx, itemInfo in ipairs(cfg.item) do
            Grid.makeItem(rewardInfo[idx], itemInfo[1], itemInfo[2])
        end
    end

    Msg.send(msgRet, human.fd)
end

--领奖
function GetReward(human, id, dayIndex)
    -- local state, _, realStartTime = AbsActLogic.isStarted(human, id)
    local state, _, realStartTime = CycleActivityLogic.isStarted(human, id)
    if not state then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    local actData = human.db.absAct[id]
    if not actData then
        return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
    end

    if dayIndex <= 0 then
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end


    --local loginDay = actData.loginDay
    local loginDay = Util.diffDay(realStartTime) + 1

    if not loginDay or dayIndex > loginDay then
        return Broadcast.sendErr(human, Lang.COMMON_TIME_NOT_ENOUGH)
    end


    local getRewardRecord = actData.getRewardRecord
    if getRewardRecord then
        if check(getRewardRecord, dayIndex) then
            return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
        end
    end

    local config = AbsActExcel.AbsLoginReward
    if dayIndex > #config then
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end

    --更新db
    getRewardRecord = getRewardRecord or {}
    table.insert(getRewardRecord, dayIndex)
    actData.getRewardRecord = getRewardRecord

    --发放奖励
    local targetDayReward = config[dayIndex].item
    BagLogic.addItemList(human, targetDayReward, LOGTAG)

    --发下客户端更新数据
    Query(human, id) --暂定使用查询协议更新数据


    --更新红点
    if #getRewardRecord >= loginDay or #getRewardRecord >= #config then
        YunYingLogic.sendBanner(human)
        local config = AbsActExcel.absActivity[id]
        YunYingLogic.sendGroupUpdate(YYInfo[id], human, config.panelID)
    end
end