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 TreasureChestLogic = require("treasurechest.TreasureChestLogic")
local Log = require("common.Log")
local CommonDefine = require("common.CommonDefine")
local ShopLogic = require("shop.ShopLogic")

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

local MONTH_CARD_FOREVER = 2        -- 永久卡ID

local MONTHCHANEID_MUZI = 1         -- 木子渠道

-- 写日志
local function MonthCard_WriteLog(human, szText)
    if human then
        szText = szText.." name = "..human.db.name.." id = "..human.db._id
    end

    Log.write(Log.LOGID_OSS_COMMON, szText)
end

-- 获取配置
local function MonthCard_GetConf(human)
    local nChanelID = human.db.phpChanelID
    if not nChanelID then
        nChanelID = human.phpChanelID
    end

    if not nChanelID then
        print("[MonthCard_GetConf] 不存在对应的渠道")
        return nil
    end

    -- print("[MonthCard_GetConf] 当前的nChanelID =  "..nChanelID)
    
    local tConf = {}
    if CommonDefine.CHANNEL_TAG_MUZI == nChanelID or CommonDefine.CHANNEL_TAG_SANLI_QQ == nChanelID or CommonDefine.CHANNEL_TAG_WX == nChanelID then
        table.insert(tConf, MonthCardExcel[11])
        table.insert(tConf, MonthCardExcel[12])
    else
        table.insert(tConf, MonthCardExcel[1])
        table.insert(tConf, MonthCardExcel[2])
    end

    return tConf
end

---  功能修改 前  已经有不删档 服 开启 所以 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)
    local tMonthCardConf = MonthCard_GetConf(human)

    if not tMonthCardConf or not tMonthCardConf[id] then
    -- 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 MonthCardConf = MonthCard_GetConf(human)
        if not MonthCardConf then
            return 0
        end

        local config = MonthCardConf[2]

        return config[powerType] or 0
    end

    local monthCardDB = checkDB(human, 1)
    if getState(monthCardDB) ~= MONTH_CARD_STATE_0 then
        local MonthCardConf = MonthCard_GetConf(human)
        if not MonthCardConf then
            return 0
        end
        local config = MonthCardConf[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

    local tMonthCardConf = MonthCard_GetConf(human)
    if not tMonthCardConf then
        MonthCard_WriteLog(human, "[MonthCard_Query] 获取不到配置")
        return
    end

    for k, v in ipairs(tMonthCardConf) 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 tMonthCardConf = MonthCard_GetConf(human)
    if not tMonthCardConf then
        MonthCard_WriteLog(human, "[MonthCard_Get] 获取不到配置")
        return
    end

    local config = tMonthCardConf[id]
    -- 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 tMonthCardConf = MonthCard_GetConf(human)
    if not tMonthCardConf then
        MonthCard_WriteLog(human, "[MonthCard_buyMonthCard] 玩家购买月卡获取不到配置 id = "..id)
        return
    end

    local config = tMonthCardConf[id]

    -- 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

    if MONTH_CARD_FOREVER == id then
        TreasureChestLogic.TreasureChestLogic_BuyOpenAuto(human)
        ShopLogic.GetBuyLiftTimeCard(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,2)

    if getState(monthCardDB) ~= MONTH_CARD_STATE_0 then
        print("[MonthCard-isRed] 未过期 name = "..human.db.name)
        TreasureChestLogic.TreasureChestLogic_BuyOpenAuto(human)
    end

    if getState(monthCardDB) == MONTH_CARD_STATE_1 then
        return true
    end

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

-- 是否购买永久月卡
function IsBuyEverlastingCard(human)
    local monthCardDB = checkDB(human, MONTH_CARD_FOREVER)
    return getState(monthCardDB) ~= MONTH_CARD_STATE_0
end

-- function GetRemainNum(human, nBuyID)
--     local tConf = MonthCard_GetConf(human)
--     if not tConf then
--         return 0
--     end

--     local nChoseID = nil
--     for nID, v in ipairs(tConf) do
--         if v.buyID == nBuyID then
--             nChoseID = nID
--             break
--         end
--     end

--     if nil == nChoseID then
--         return 0
--     end

--     local monthCardDB = checkDB(human, nChoseID)
--     return getState(monthCardDB) == MONTH_CARD_STATE_0 and 1 or 0
-- end