--------------------------------------
-- 数据库设计
--[[
    1 超值基金
    2 奢华基金
    human.db.overflow = {}
    human.db.overflow.state = {}   
    human.db.overflow.state[1] = time
    human.db.overflow.state[2] = time
    human.db.overflow.get = {}   
    human.db.overflow.get[1] = val
    human.db.overflow.get[2] = val
]]

-----------------------------------------
-- 引用文件
local PresentExcel = require("excel.present")
local Msg = require("core.Msg")
local Util = require("common.Util")
local BuyLogic = require("topup.BuyLogic")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local PanelDefine = require("broadcast.PanelDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local BuyExcel = require("excel.buy")
local Log = require("common.Log")

-----------------------------------------
-- 宏定义
OVERFLOW_TYPE_1 = 1         -- 超值基金
OVERFLOW_TYPE_2 = 2         -- 奢华基金
-----------------------------------------
-- 逻辑接口
-- 是否开启
function isOpen(human,YYInfo,funcConfig)
    if not human.db.nFirstBuy or 1 ~= human.db.nFirstBuy then
        return false
    end

    return true
end

-- 获取当前基金状态
--return 状态,下次领取天数  状态 0 未购买 1 领取 2 已领取
local function getState(human,type)
    -- 有数据
    if human.db.overflow and human.db.overflow.state and human.db.overflow.state[type] then
        local day = Util.diffDay(human.db.overflow.state[type]) + 1
        human.db.overflow.get = human.db.overflow.get or {}
        human.db.overflow.get[type] = human.db.overflow.get[type] or 0
        local nextDay = human.db.overflow.get[type]
        if nextDay < day then
            return 1,nextDay + 1
        else
            return 2,nextDay + 1
        end
    end

    return 0,0
end
-- 基金信息查询
function overflowQuery(human,type)
    
    -- 根据类型获得配置
    local config = nil
    if type == OVERFLOW_TYPE_1 then
        config = PresentExcel.overflowFund
    elseif type == OVERFLOW_TYPE_2 then
        config = PresentExcel.luxuryFund
    end

    if not config then
        return
    end

    local msgRet = Msg.gc.GC_OVERFLOW_FUND_QUERY
    msgRet.type = type
    BuyLogic.fontBuyItem(human, msgRet.buyMsg, config[1].buyID)
    msgRet.state,msgRet.nextGet = getState(human,type)
    local len = #config
    local count = 0
    for i = 1,len do
        Grid.makeItem(msgRet.item[i],config[i].item[1][1],config[i].item[1][2])
        if config[i].part == 1 then
            count = count + 1
            msgRet.partItem[count] = i
        end
    end
    msgRet.partItem[0] = count
    msgRet.item[0] = len
    msgRet.worth[1] = PresentExcel.overflowFund[1].worth
    msgRet.worth[2] = PresentExcel.luxuryFund[1].worth
    msgRet.worth[0] = 2
    
    Msg.send(msgRet,human.fd)
end

function onBuy(human,type)
    -- Log.write(Log.LOGID_DEBUG, "[OverflowFundLogic.onBuy] 进入购买 type="..type)

    -- 已购买，不可重复购买
    if human.db.overflow and human.db.overflow.state and human.db.overflow.state[type] then
        Log.write(Log.LOGID_DEBUG, "[OverflowFundLogic.onBuy] 已购买，不可重复购买 type="..type)
        return
    end

    human.db.overflow = human.db.overflow or {}
    human.db.overflow.state = human.db.overflow.state or {}
    human.db.overflow.get = human.db.overflow.get or {}
    human.db.overflow.state[type] = os.time()
    human.db.overflow.get[type] = 0
    -- Log.write(Log.LOGID_DEBUG, "[OverflowFundLogic.onBuy] 设置基金状态 type="..type..", state="..human.db.overflow.state[type])
    overflowQuery(human,type)
	for k, v in pairs(funcID) do
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3310)
        break
    end
    
    -- 如果购买的是奢华基金（OVERFLOW_TYPE_2），需要更新勇者试炼的一键扫荡状态
    if type == OVERFLOW_TYPE_2 then
        -- Log.write(Log.LOGID_DEBUG, "[OverflowFundLogic.onBuy] 购买奢华基金，更新勇者试炼状态")
        local DrillLogic = require("drill.DrillLogic")
        -- 只刷新数据，不打开界面（openPanel = false）
        DrillLogic.queryDrillId(human, nil, false)
    end
end

function getFundReward(human,type)
    -- 根据类型获得配置
    local config = nil
    if type == OVERFLOW_TYPE_1 then
        config = PresentExcel.overflowFund
    elseif type == OVERFLOW_TYPE_2 then
        config = PresentExcel.luxuryFund
    end

    if not config then
        return
    end

    local state = getState(human,type)

    if state == 0 then
        return
    end

    local day = Util.diffDay(human.db.overflow.state[type]) + 1
    human.db.overflow.get = human.db.overflow.get or {}
    human.db.overflow.get[type] = human.db.overflow.get[type] or 0
    local nextDay = human.db.overflow.get[type] + 1

    local item = {}
    local len = #config
    for i = nextDay,day do
        if not config[i] then
            break
        end
        
        human.db.overflow.get[type] = human.db.overflow.get[type] + 1
        item[#item + 1] = {config[i].item[1][1],config[i].item[1][2]}
    end
    BagLogic.addItemList(human, item, "overflow_fund")
    -- 领取完最后一天
    if day >= len then
        human.db.overflow.state[type] = nil
        human.db.overflow.get[type] = nil
    end
    overflowQuery(human,type)
    
	for k, v in pairs(funcID) do
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3310)
        break
    end
end

function isRed(human)
    for i = 1,2 do
        local state = getState(human,i)
        -- 已购买
        if state ~= 0 then
            local day = Util.diffDay(human.db.overflow.state[i]) + 1
            human.db.overflow.get = human.db.overflow.get or {}
            human.db.overflow.get[i] = human.db.overflow.get[i] or 0
            local nextDay = human.db.overflow.get[i] + 1
            if day >= nextDay then
                return true
            end
        end
    end
    return false
end

-- function GetRemainNum(human, nBuyID)
--     local tBuyConf = BuyExcel.buy[nBuyID]
--     if not tBuyConf then
--         return 0
--     end

--     local bType = tBuyConf.args[1]
--     local state = getState(human, bType)

--     return (0 == state) and 1 or 0
-- end