-- 三国密令

--[[
    db.miLing = {
                    time =  1 ,         -- 激活时间
                    exp  =  1,          -- 密令经验
                    task    = {},       -- 密令任务获取
                    rechargeGet = {},    -- 累充奖励领取
                    miLingGet = {},     -- 密令等级奖励领取

    }

]]
local Msg = require("core.Msg")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Util = require("common.Util")
local BuyLogic = require("topup.BuyLogic")
local MiLingExcel = require("excel.miLing")

MILINMG_TASK_TYPE_1      = 1         -- 登陆游戏
MILINMG_TASK_TYPE_2      = 2         -- 扫荡试炼 塔
MILINMG_TASK_TYPE_3      = 3
MILINMG_TASK_TYPE_4      = 4
MILINMG_TASK_TYPE_5      = 5

function inintDB(human)
    if human.db.miLing then return end 
    human.db.miLing = {}
    local miLing = human.db.miLing
    miLing.time = os.time()
    miLing.exp  = 0
    miLing.expDay = 0
    miLing.task = {}
    miLing.rechargeGet = {}
    miLing.miLingGet = {}

end

function getOpenDay(human)
    local day = Util.diffDay(human.db.miLing.time)
    return day or 0
end

function updateDaily(human)
    -- 目前不知道 开启条件 默认 创角 就有
    inintDB(human)
    human.db.miLing.expDay = 0

end

function getTaskNum(human, taskId)
    local num = 0
    if  human.db.miLing and  human.db.miLing.task[taskId] then
        num =  human.db.miLing.task[taskId].num
    end
    return num 
end

function getTaskIdState(human, taskId)
    local state = 0 
    if  human.db.miLing and  human.db.miLing.task[taskId] then
        state =  human.db.miLing.task[taskId].state
    end
    return state 
end

function getMilingLv(human)
    if not human.db.miLing then return 0 end
    local lv = 0
    for k, v in ipairs(MiLingExcel.miLing) do
        if human.db.miLing.exp >= v.needExp then 
            lv = v.needLv
        end
    end
    return lv
end

function fontLeiChongNet(human, msgRet)

end

function query(human)
    local msgRet = Msg.gc.GC_PRESENT_MILING_QUERY
    local day = getOpenDay(human)
    
    local len = 0
    for k, v in ipairs(MiLingExcel.task) do
        if v and v.day == day then
            len = len + 1
            local net = msgRet.list[len]
            net.id = k
            net.name = v.name
            net.need = v.need
            net.have = getTaskNum(human, k)
            net.state = getTaskIdState(human, k)
            Grid.makeItem(net.reward[1], v.reward[1],  v.reward[2])
            net.reward[0] = 1
        end
    end
    
    if len <= 0 then return end
    msgRet.list[0] = len

    msgRet.dayGet = 0
    msgRet.jindu = 0
    msgRet.jinduMax = 0

    Msg.send(msgRet, human.fd)
end

function getTaskId(human, taskType)
    local day = getOpenDay(human)
    local taskId  = 0
    for k, v in ipairs(MiLingExcel.task) do
        if v and v.day == day and v.type == taskType then
            taskId = k
        end
    end
    return taskId
end

function checkIsFinish(human, taskId)
    if human.db.miLing and human.db.miLing.task[taskId] and human.db.miLing.task[taskId].state == 0 then
        local config = MiLingExcel.task[taskId]
        if human.db.miLing.task[taskId].num > config.need then
            human.db.miLing.task[taskId].state = 1
        end        
    end
end

function getTaskAddType(taskType)
    if taskType == MILINMG_TASK_TYPE_1 then
        return false
    else
        return true
    end
end

function touchTaskNum(human, type, add)
    if not human.db.miLing then return end 
    local taskId = getTaskId(human, type)

    local config = MiLingExcel.task[taskId]
    if not config then return end

    local miLing = human.db.miLing

    if not miLing.task[taskId] then
        miLing.task[taskId] = {}
        miLing.task[taskId].state = 0
        miLing.task[taskId].num = 0
    end

    if miLing.task[taskId].state == 1 then
        return
    end

    local addType = getTaskAddType(type)

    if addType then
        miLing.task[taskId].num =  miLing.task[taskId].num + add
    else
        miLing.task[taskId].num = add
    end
    
    checkIsFinish(human, taskId)

    query(human)
end

function fontRewardNet(human, config, msgRet)
    for k, v in ipairs(config) do
        local net = msgRet.list[k]
        local net = msgRet.list[len]
        net.id = k
        net.name = v.name
        net.need = v.need
        net.have = 0
        net.state = getTaskState(human, k)
        Grid.makeItem(net.reward[1], v.reward[1],  v.reward[2])
        net.reward[0] = 1
   
    end
    msgRet.list[0] = #config
end

function queryReward(human, queryType)
    if not  human.db.miLing then return end 
    local config 
    if queryType == 1 then
        config = MiLingExcel.miLing
    elseif queryType == 2 then
        config = MiLingExcel.recharge
    end

    if not config then return end 

    local msgRet = Msg.gc.GC_PRESENT_MILING_REWARD_QUERY
    msgRet.queryType = queryType
    fontRewardNet(human, config, msgRet)    
    Msg.send(msgRet, human.fd)
end

function getRechargetState(human, id)
    local recharge = human.db.topupAcount
    local config =  MiLingExcel.recharge[id]
    if human.db.miLing and human.db.miLing.rechargeGet[id] then
        return 2
    end

    if recharge >= config.need then
        return  1
    end
    return 0
end

function getMilingLvState(human, id)
    local lv = getMilingLv(human)
    if human.db.miLing and human.db.miLing.miLingGet[id] then
        return 2
    end

    if lv >= config.needLv then
        return 1
    end
    return 0
end

function getTaskState(human, queryType, id)
    if queryType == 1 then
        return getMilingLvState(human, id)
    elseif queryType == 2 then
        return getRechargetState(human, id)
    end
end

function updateGetState(human, queryType, id)
    if queryType == 1 then
        human.db.miLing.miLingGet =  human.db.miLing.miLingGet or {}
        human.db.miLing.miLingGet[id] = 1
    elseif queryType == 2 then
        human.db.miLing.rechargeGet = human.db.miLing.rechargeGet or {}
        human.db.miLing.rechargeGet[id] = 1
    end
end

function getReward(human, getType, id)

    if not human.db.miLing then return end
    if getTaskState(human, getType, id) ~= 1 then
        return 
    end
    local config                                                       
    if getType == 1 then
        config = MiLingExcel.miLing

    elseif getType == 2 then
        config = MiLingExcel.recharge
    end
    if not config then return end 

    updateGetState(human, getType, id)
    BagLogic.sendItemGetList(human, config.reward, "miLing")
    queryReward(human, getType)
end



