
local DB = require("common.DB")
local CommonDB = require("common.CommonDB")
local CdkFixExcel = require("excel.cdkFix")
local LuaMongo = _G.lua_mongo
local Lang = require("common.Lang")

local Code = {  "0","c","e","u","m",
                "k","d","7","x","f",
                "9","j","w","6","8",
                "t","1","h","4","p",
                "y","3","2","b","5","n"}
local batchList = {6,7,8} -- 批次位置 
local idxList = {3,4,5} -- 序号位置

local CodeMap = {
    ["0"] = 1,["c"] = 2,["e"] = 3,["u"] = 4,["m"] = 5,
    ["k"] = 6,["d"] = 7,["7"] = 8,["x"] = 9,["f"] = 10,
    ["9"] = 11,["j"] = 12,["w"] = 13,["6"] = 14,["8"] = 15,
    ["t"] = 16,["1"] = 17,["h"] = 18,["4"] = 19,["p"] = 20,
    ["y"] = 21,["3"] = 22,["2"] = 23,["b"] = 24,["5"] = 25,["n"] = 26
}

local unit = 20
local bit = unit
local bit2 = unit * unit

local function checkCDKParam(finishTime,cdkID)
    local now = os.time()
    if now > finishTime then 
        return
    end
    return true
end

local function genCDKBatch(cnt,useCnt,cdkID)
    local bat = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
    local db = {
        batch = bat + 1,
        cnt = cnt,
        useCnt = useCnt,
        info = cdkID,
        useCDKList = {},
    }
    LuaMongo.insert(DB.db_cdk, db)
    CommonDB.setCDKBatch(bat + 1) -- 将最新的cdkBatch保存
    return bat + 1
end

local function transNumber(num)
    local first,second = 0,0
    if num >= bit2 then
        first = math.floor(num / bit2)
    end
    if num >= bit then
        local t = num - (first * bit2)
        second = math.floor(t / bit)
    end
    local third = num - (first * bit2) - (second * bit)
    --return Code[first + 1] .. Code[second + 1] .. Code[thrid + 1]
    return first,second,third
end

local function calcNumber(first,second,third)
    if first > unit or second > unit  or third > unit  then 
        return -1
    end
    return first * bit2 + second * bit + third
end

local function transNum2Code(num)
    local f,s,t = transNumber(num)
    return Code[f + 1]..Code[s + 1]..Code[t + 1]
end

local function genCDKCode(batch,index)
    return Code[math.random(2,#Code)] .. Code[math.random(#Code)] ..transNum2Code(index) .. transNum2Code(batch) .. Code[math.random(#Code)] .. Code[math.random(#Code)]
end



local function deCDKCode(cdk)
    local num_f_code,num_s_code,num_t_code = CodeMap[string.sub(cdk,3,3) ],CodeMap[string.sub(cdk,4,4) ],CodeMap[string.sub(cdk,5,5) ]
    if not num_f_code or not num_s_code or not num_t_code then
        return false
    end
    num_f_code = num_f_code - 1
    num_s_code = num_s_code - 1
    num_t_code = num_t_code - 1
    local bat_f_code,bat_s_code,bat_t_code = CodeMap[string.sub(cdk,6,6) ],CodeMap[string.sub(cdk,7,7) ],CodeMap[string.sub(cdk,8,8) ]
    if not bat_f_code or not bat_s_code or not bat_t_code then
        return false
    end
    bat_f_code = bat_f_code - 1
    bat_s_code = bat_s_code - 1
    bat_t_code = bat_t_code - 1
    local num = calcNumber(num_f_code,num_s_code,num_t_code)
    if num < 0 then 
        return false
    end
    local batch = calcNumber(bat_f_code,bat_s_code,bat_t_code)
    if batch < 0 then 
        return false
    end
    --local num =  (CodeMap[string.sub(cdk,3,3) ] - 1) * 100 +  (CodeMap[string.sub(cdk,4,4)] - 1) * 10 + (CodeMap[string.sub(cdk,5,5) ] - 1)
    --local batch = (CodeMap[string.sub(cdk,6,6) ] - 1) * 100 +  (CodeMap[string.sub(cdk,7,7) ] - 1) * 10 + (CodeMap[string.sub(cdk,8,8) ] - 1)
    return true , num,batch
end

-- 生成一个批次的激活码
-- 批次最大999 ，每次最多生成999个激活码
--[[
    param1 = cnt 生成CDK数量
    param2 = useCnt 当前批次能够使用最大数量
    param3 = cdkId 道具信息
]]
function genCDK(cnt,useCnt,cdkId)
    useCnt = useCnt or 1
    cnt = cnt < 1000 and cnt or 999
    local bat = genCDKBatch(cnt,useCnt,cdkId)
    local fileName = "cdk"..bat
    local f,err = io.open(fileName,"w")
    if not f then
        print("open file failed ",err)
        return
    end
    local idx = 0
    local data = "cdk : \n"
    while true do 
        if idx > cnt then 
            break
        end
        local cdk = genCDKCode(bat,idx)
        data = data .. cdk .. "\n"
        idx = idx + 1
    end
    f:write(data)
end

-- 使用cdk
function useCDK(cdkBatchData,cdk)
    local ok,_,batch = deCDKCode(cdk)
    QueryCDK = { batch = { ["$eq"] = batch}}
    LuaMongo.update(DB.db_cdk,QueryCDK,{
        ["$set"] = {
            ["useCDKList"] = cdkBatchData.useCDKList
        },
        ["$unset"] = nil
    })
end

-- 检查CDK
function checkCDK(cdk)
    local ok,num,batch = deCDKCode(cdk)
    if not ok then 
        return Lang.CDK_INVALID_ERR
    end
    local curBatch = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
    if curBatch < batch then
        return Lang.CDK_BATCH_ERR
    end
    QueryCDK = { batch = { ["$eq"] = batch} }
    LuaMongo.find(DB.db_cdk,QueryCDK)
    local cdkBatchData = {}
    LuaMongo.next(cdkBatchData)
    if cdkBatchData.cnt < num then 
        return Lang.CDK_INVALID_ERR
    end
    if cdkBatchData.useCDKList[num] then 
        return Lang.CDK_ERR4
    end
    return nil, cdkBatchData
end