-- 月光宝盒
-- 创角时根据服务器开服天数获得一定道具，当玩家从其他特定玩法获得这些道具时，会额外获得一份道具，
-- 额外道具数量 = math.min(其他玩法获得道具数量, 宝盒中道具剩余数量), 并从宝盒中扣除额外道具数量

-- db
--[=[
    human.db.storageBoxData = {
        [itemID] = itemCnt,
    }
]=]--

local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local Log = require("common.Log")
local CommonDB = require("common.CommonDB")
local LogDefine = require("common.LogDefine")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local Util = require("common.Util")


local ITEM_LOG_TYPE = "storageBox" --本模块的道具日志标识

local GET_EXTEA_REWARD_RATE = 50  -- 额外获得一份奖励的几率

local STORAGEBOXID = 1038         -- 月光宝盒Id

local LOGOUT_DAY_COND = 2       -- 至少离线两天, 才可以获得离线奖励


-- 月光宝盒道具列表
-- key 为道具ID， value为基础数量
local ITEM_LIST = {
    [ItemDefine.ITEM_JINBI_ID] = 1000000,
    [ItemDefine.ITEM_GREEN_EXP_ID] = 500000,
    [ItemDefine.ITEM_DRAWCARD_GAOCHOU_ID] = 20,
    [ItemDefine.ITEM_HERO_UPGRADE_BOOK] = 2,
    [ItemDefine.ITEM_BASE_QIYUANZHU_ID] = 2,
    [ItemDefine.ITEM_XIANZHI_BAOZHU_ID] = 1,
    [ItemDefine.ITEM_FUWEN_JINGHUA] = 500,
    [ItemDefine.ITEM_LONGHUNSHI_ID] = 50,
    [ItemDefine.ITEM_ZUANSHI_ID] = 500,
    [ItemDefine.ITEM_HERO_UPGRADE_ID] = 1000,
}

-- 可获得额外道具的来源列表
local SOURECE_LIST = {
    -- 主线闯关相关
    [LogDefine.DEFINE.battle] = '1',
    [LogDefine.DEFINE.battle_win] = '1',
    [LogDefine.DEFINE.tongguan] = '1',
    [LogDefine.DEFINE.chapterReward] = '1',

    -- 恶魔之塔
    [LogDefine.DEFINE.tower_win_reward] = '1',
    [LogDefine.DEFINE.tower_king_unlock] = '1',
    [LogDefine.DEFINE.tower_reward] = '1',
    [LogDefine.DEFINE.tower_king_reward] = '1',

    -- 公会首领
    [LogDefine.DEFINE.unionBoss] = '1',

    -- 绝望深渊
    [LogDefine.DEFINE.lianyu_firstReward] = '1',
    [LogDefine.DEFINE.lianyu_win_reward] = '1',

    -- 女巫森林
    [LogDefine.DEFINE.copy_win] = '1',

    -- 勇者试炼
    [LogDefine.DEFINE.drill] = '1',

    -- 种族试炼
    [LogDefine.DEFINE.racialTrial] = '1',

    -- 冰龙巢穴
    [LogDefine.DEFINE.dragonCopy] = '1',

    -- 遗迹探险
    [LogDefine.DEFINE.yj_treasure] = '1',
    [LogDefine.DEFINE.yj_treasure_kaowen] = '1',


    -- 失落神庙
    [LogDefine.DEFINE.lostTemple] = '1',

    -- 次元魔珠/魔王梼杌
    [LogDefine.DEFINE.ciYuanMozhu] = '1',

    -- 冠军联赛
    [LogDefine.DEFINE.jjc_season_box] = '1',
    [LogDefine.DEFINE.jjc_worship] = '1',
    [LogDefine.DEFINE.jjc_fight] = '1',

    -- 幽暗禁地
    -- [LogDefine.DEFINE.darkforarea] = '1',
}


-- 不能获得额外奖励的来源列表
local EXLUDE_SOURECE_LIST = {
    -- 英雄重生
    [LogDefine.DEFINE.hero_chongsheng] = '1',

    -- 英雄回退
    [LogDefine.DEFINE.hero_huitui] = '1',
}



local function initData(human)
    human.db.storageBoxData = {}
end

local function getData(human)
    return human.db.storageBoxData
end

local function updateData(human, itemId, itemCnt, isAdd)
    local storageBoxData = getData(human)

    if isAdd then
        storageBoxData[itemId] = (storageBoxData[itemId] or 0) + itemCnt
    else
        local num = storageBoxData[itemId] - itemCnt
        storageBoxData[itemId] = math.max(num, 0)
    end
end


local function writeLog(human, sourceType, itemId, itemSubCnt, randRate)
    local logStr = string.format("角色: [%s] 通过: %s 扣除月光宝盒中道具, 道具ID: %d, 道具数量: %d, 随机值: %d",
    human.db._id, sourceType, itemId, itemSubCnt, randRate)

    Log.write(Log.LOGID_OSS_STORAGEBOx, logStr)
end


local function addStorageBoxItem(human)
    if human.db.lastLogoutTime and human.db.lastLogoutTime > 0 then
        local logoutDay = Util.diffDay(human.db.lastLogoutTime)
        if logoutDay >= LOGOUT_DAY_COND then
            if BagLogic.getItemCnt(human, STORAGEBOXID) <= 0 then
                BagLogic.addItem(human, STORAGEBOXID, 1, ITEM_LOG_TYPE)
            end

            local storageBoxData = getData(human)
            if not storageBoxData then
                initData(human)
            end

            for itemId, itemBaseCnt in pairs(ITEM_LIST) do
                updateData(human, itemId, itemBaseCnt * logoutDay , true)
            end
        end
    end
end



-- 创角时给角色的月光宝盒中添加一定数量道具
function FillStorageBox(human)
    local openSvrDay = CommonDB.getServerOpenDay()
    if not openSvrDay or openSvrDay <= 1 then
        return
    end

    openSvrDay = openSvrDay - 1

    local storageBoxData = getData(human)
    if storageBoxData then
        return
    end

    -- 新修改： 需要获得一个道具
    BagLogic.addItem(human, STORAGEBOXID, 1, ITEM_LOG_TYPE)

    initData(human)
    -- storageBoxData = getData(human)

    for itemId, itemBaseCnt in pairs(ITEM_LIST) do
        updateData(human, itemId, itemBaseCnt * openSvrDay , true)
    end
end



function onLogin(human)
    addStorageBoxItem(human)
end


-- 从月光宝盒中额外获取一份道具
function GetExtraItem(human, itemId, itemCnt, sourceType)
    local sourceId = LogDefine.DEFINE[sourceType]

    -- 新修改, 不需要特定来源
    -- if not SOURECE_LIST[sourceId] then
    --     return
    -- end

    if EXLUDE_SOURECE_LIST[sourceId] then
        return
    end


    local randRate = math.random(1, 100)
    if randRate > GET_EXTEA_REWARD_RATE then
        return
    end

    if not ITEM_LIST[itemId] then
        return
    end

    local storageBoxData = getData(human)
    if not storageBoxData then
        return
    end


    local itemSubCnt = storageBoxData[itemId]
    if itemSubCnt <= 0 then
        return
    end

    if itemSubCnt >= itemCnt then
        itemSubCnt = itemCnt
    end

    -- 减少月光宝盒中该道具的数量
    updateData(human, itemId, itemSubCnt)

    writeLog(human, sourceType, itemId, itemSubCnt, randRate)

    -- Query(human)

    local itemTbl = {itemId, itemSubCnt}

    -- 增加道具的来源标识
    itemTbl[4] = 1

    return itemTbl
end


-- 查询
function Query(human)
    local storageBoxData = getData(human)
    if not storageBoxData then
        return
    end

    local msgRet = Msg.gc.GC_ROLESTORAGEBOX_QUERY
    local itemList = msgRet.itemList
    local len = 0

    for itemId, itemCnt in pairs(storageBoxData) do
        len = len + 1
        Grid.makeItem(itemList[len], itemId, itemCnt)
    end
    itemList[0] = len

    Msg.send(msgRet, human.fd)
end