local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local MonthCardExcel = require("excel.present").monthCard
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local BagLogic = require("bag.BagLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local YyHandler = require("yunying.Handler")
local KingWorldLogic = require("present.KingWorldLogic")
local HeroLogic = require("hero.HeroLogic")

local MONTH_CARD_STATE_0 = 0        -- 已过期
local MONTH_CARD_STATE_1 = 1        -- 未过期
local MONTH_CARD_STATE_2 = 2        -- 已领取

---  功能修改 前  已经有不删档 服 开启 所以 DB 以原有的为标准 大体不去修改

local function getState(monthCardDB)
    local now = os.time()
    if monthCardDB.endTime >= 0 and monthCardDB.endTime < now then
        return MONTH_CARD_STATE_0
    end
    
    if monthCardDB.getTime then
        if Util.isSameDay(monthCardDB.getTime) then
            return MONTH_CARD_STATE_2
        else
            return MONTH_CARD_STATE_1
        end 
    else
        local day = Util.diffDay(monthCardDB.startTime) + 1
        if monthCardDB.endTime > 0  and day > monthCardDB.day  then
            return MONTH_CARD_STATE_1
        elseif monthCardDB.endTime < 0 and not monthCardDB.getTime then
            return MONTH_CARD_STATE_1
        end
    end

    return MONTH_CARD_STATE_2
end

local function checkDB(human,id)
    if not MonthCardExcel[id] then
        assert(nil,id)
    end 
    human.db.monthCard = human.db.monthCard or {}
    human.db.monthCard[id] = human.db.monthCard[id] or {day = 0,startTime = 0,endTime = 0,money = 0, get = 0}
    local monthCardDB = human.db.monthCard[id] 
    if monthCardDB.startTime > 0 and getState(monthCardDB) == 0 then
        human.db.monthCard[id] = {day = 0,startTime = 0,endTime = 0,money = 0, get = 0}
    end
    return human.db.monthCard[id]
end

-- 获取特权参数
function getPowerArgs(human, powerType)
    -- 检查 是否开了 永久月卡
    local monthCardDB = checkDB(human, 2)
    if getState(monthCardDB) ~= MONTH_CARD_STATE_0 then
        local config = MonthCardExcel[2]

        return config[powerType] or 0
    end

    local monthCardDB = checkDB(human, 1)
    if getState(monthCardDB) ~= MONTH_CARD_STATE_0 then
        local config = MonthCardExcel[1]

        return config[powerType] or 0
    end

    return 0
end

function query(human)  
    local len = 0
    local msgRet = Msg.gc.GC_MONTH_CARD_QUERY
    msgRet.jiacheng = 0 
    msgRet.drawZhekou = 0
    msgRet.heroBagAdd = 0
    for k, v in ipairs(MonthCardExcel) do
       local id = k
       
       len = len + 1
       local net = msgRet.list[len]

       local monthCardDB = checkDB(human,id)
       net.id = id
       net.name = v.name
       net.startTime = monthCardDB.startTime
       net.endTime = monthCardDB.endTime
       local day = Util.diffDay(monthCardDB.startTime) + 1
       net.day = v.day - day
       net.get = monthCardDB.get or 0
       net.state = getState(monthCardDB)
       Grid.makeItem(net.yuanbao,ItemDefine.ITEM_ZUANSHI_ID,v.yuanbao)
       Grid.makeItem(net.instantly,ItemDefine.ITEM_ZUANSHI_ID,v.instantly)
  --     Grid.makeItem(net.maxYuanbao,ItemDefine.ITEM_ZUANSHI_ID,v.yuanbao * v.day + v.instantly)
       BuyLogic.fontBuyItem(human, net.buyItem, v.buyID)
       msgRet.jiacheng = v[7]
       msgRet.drawZhekou = v[21]
       msgRet.heroBagAdd = v[12]
    end
    msgRet.list[0] = len
    Msg.send(msgRet,human.fd)
end

function get(human,id)   
    local monthCardDB = checkDB(human,id)
    if getState(monthCardDB) ~= MONTH_CARD_STATE_1 then
        return
    end
    local config = MonthCardExcel[id]
    local addYuanbao = config.yuanbao
    if config.day > 0 then
        monthCardDB.day = Util.diffDay(monthCardDB.startTime) + 1
    end
    monthCardDB.get =  monthCardDB.get or 0
    monthCardDB.get =  monthCardDB.get + 1
    monthCardDB.getTime = os.time()
    ObjHuman.addZuanshi(human,addYuanbao,"monthCard")
    BagLogic.sendItemGetList(human, {{ItemDefine.ITEM_ZUANSHI_ID,addYuanbao}}, "monthCard")
    local msgRet = Msg.gc.GC_MONTH_CARD_GET
    Msg.send(msgRet,human.fd)
    query(human)
	for k, v in pairs(funcID) do
	    YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3303)
        break
    end
end

function buyMonthCard(human,id)
    local monthCardDB = checkDB(human,id)
    if getState(monthCardDB) == MONTH_CARD_STATE_1 then
        return
    end
    local config = MonthCardExcel[id]
    local now = os.time()
    monthCardDB.startTime = Util.getDayStartTime(now)
    if config.day > 0 then
        monthCardDB.endTime = monthCardDB.startTime + config.day * 86400
    else
        monthCardDB.endTime = -1
    end

    ObjHuman.addZuanshi(human,config.instantly,"monthCard")
    BagLogic.sendItemGetList(human, {{ItemDefine.ITEM_ZUANSHI_ID,config.instantly}}, "monthCard")
    
	--YunYingLogic.updateIcon(KingWorldLogic.YYInfo, human)
    
    for k, v in pairs(KingWorldLogic.funcID) do
        YunYingLogic.updateIcon(KingWorldLogic.YYInfo[k], human)
        break
    end
    query(human)

    if monthCardDB.endTime == -1 then
        
        HeroLogic.sendHeroBagCap(human)
    end
end

-- 是否购买
function isActive(human, YYInfo, funcConfig)
   local monthCardDB = checkDB(human, 1)
    if getState(monthCardDB) == MONTH_CARD_STATE_0 then
        return
    end

    monthCardDB = checkDB(human, 2)
    if getState(monthCardDB) == MONTH_CARD_STATE_0 then
        return
    end

    return true
end

function isRed(human, YYInfo, funcConfig)
    local monthCardDB = checkDB(human,1)
    if getState(monthCardDB) == MONTH_CARD_STATE_1 then
        return true
    end

    local monthCardDB = checkDB(human,2)
    if getState(monthCardDB) == MONTH_CARD_STATE_1 then
        return true
    end
    return
end