--------------------------------
-- 成神之路-君临天下

-- db.kingworld[id] = state
--------------------------------

local Lang = require("common.Lang")
local Util = require("common.Util")
local OpenActLogic = require("present.OpenAct")
local OpenServerExcel = require("excel.openAct")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local MailManager = require("mail.MailManager")
local Grid = require("bag.Grid")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local BattleLogic = require("battle.BattleLogic")
local SceneHandler = require("scene.Handler")
local HeroGrid = require("hero.HeroGrid")
local MonthCardLogic = require("present.MonthCard")
local BagLogic = require("bag.BagLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local OpenServerGiftLogic = require("present.OpenServerGiftLogic")
local ChengjiuExcel = require("excel.chengjiu").chengjiu

KING_TYPE_1 = 1   -- 关卡必须75
KING_TYPE_2 = 2   -- 次日登录
KING_TYPE_3 = 3   -- 激活至尊月卡
KING_TYPE_4 = 4   -- 总览

STATE_NONE = 0
STATE_FINISH = 1 -- 可领取
STATE_GET = 2   -- 已领取


--    第二版需求出了 之后   有的服已经上线了   所以 维持原有的 DB 格式去修改
--
local function initDB(human)
    if not human.db.kingworld then
        human.db.kingworld = { }
        for i = 1, KING_TYPE_4 do
            human.db.kingworld[i] = human.db.kingworld[i] or { }
            human.db.kingworld[i].status = human.db.kingworld[i].status or STATE_NONE
        end
    end
end

-- 是否开放
function isOpen(human)
    if not SceneHandler.canCharge(human) then
        return
    end
    local isOpen, leftTime = OpenActLogic.getOpenActTime(OpenActLogic.OPEN_ACT_KING_WORLD)
    return isOpen, leftTime
end

-- 返回时间
function getLeftTime(human)
   local isOpen, leftTime = OpenActLogic.getOpenActTime(OpenActLogic.OPEN_ACT_KING_WORLD)
   return leftTime
end


-- 发送奖励
local function mailReward(human, id, mailId)
    local config = OpenServerExcel.kingWorld[id]
    if not config then return end
    local items = { }
    for z, v in ipairs(config.reward) do
        items[#items + 1] = { v[1], v[2] }
    end
    local title = MailExcel.mail[mailId].title
    local content = MailExcel.mail[mailId].content
    local senderName = MailExcel.mail[mailId].senderName
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, items, senderName)
end

function fontChengJiuNet(human, net, id)
    local config = ChengjiuExcel[id]
    net.maxCnt = config.param
    net.nowCnt = 0
    net.state = 0
    if human.db.chengjiu.type and human.db.chengjiu.type[config.type] then
        net.nowCnt = human.db.chengjiu.type[config.type]
    end

    if human.db.chengjiu.task and human.db.chengjiu.task[id] == STATE_GET then
        net.state = human.db.chengjiu.task[id]
    end

end

function query(human)
    local open, leftTime = isOpen(human)
    if not open then return end

    local msgRet = Msg.gc.GC_PRESENT_KING_WORLD_QUERY
    msgRet.leftTime = leftTime
    msgRet.list[0] = KING_TYPE_3
    for i = KING_TYPE_1, KING_TYPE_3 do
        local net = msgRet.list[i]
        local cf = OpenServerExcel.kingWorld[i]
        net.id = i
        net.sort = cf.sort
        net.desc = cf.desc
        net.panelID = cf.panelID
        net.tubiao = cf.tubiaoID
        net.item[0] = #cf.reward
        for j = 1, net.item[0] do
            local itemID = cf.reward[j][1]
            local itemCnt = cf.reward[j][2]
            Grid.makeItem(net.item[j], itemID, itemCnt)
        end
        fontChengJiuNet(human, net, cf.chengJiuID)
    end

    local fconfig = OpenServerExcel.kingWorld[KING_TYPE_4]
    msgRet.finalId = KING_TYPE_4
    msgRet.finalReward[0] = #fconfig.reward
    for j = 1, msgRet.finalReward[0] do
        local itemID = fconfig.reward[j][1]
        local itemCnt = fconfig.reward[j][2]
        Grid.makeItem(msgRet.finalReward[j], itemID, itemCnt)
    end

    HeroGrid.makeHeroSimpleByID(msgRet.hero, fconfig.reward[1][1])
    msgRet.finalState = getState(human, KING_TYPE_4)
    Msg.send(msgRet, human.fd)
end

-- 领奖
function getReward(human, id, mail)
    if not isOpen(human) then return end
    local cf = OpenServerExcel.kingWorld[id]
    if not cf then return end

    -- 不在这个界面领取
    if id ~= KING_TYPE_4 then
       return
    end

    local status = getState(human, id)
    if status ~= STATE_FINISH then return end
    human.db.kingworld = human.db.kingworld or {}
    -- 标识状态
    human.db.kingworld[id] = human.db.kingworld[id] or {}
    human.db.kingworld[id].status = STATE_GET

    if not mail then
        BagLogic.addItemList(human, cf.reward, "kingworld")
    else
        mailReward(human, id, 1012)
    end
    
	for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3603)
        break
    end
    query(human)
end


-- 获取当前进度
function canFinish(human, id)
    if id ~= KING_TYPE_4 then
        local cf = OpenServerExcel.kingWorld[id]
        local config = ChengjiuExcel[cf.chengJiuID]
      
    
        if human.db.chengjiu.type and human.db.chengjiu.type[config.type] then
            if human.db.chengjiu.task and human.db.chengjiu.task[cf.chengJiuID] and human.db.chengjiu.task[cf.chengJiuID] == STATE_GET then
               return true     
            end
        end
    
        return  false  
    else
        return isFinishAll(human)
    end
    
end

-- 是否领取
function getState(human, id)

    local cf = OpenServerExcel.kingWorld[id]
    if not cf then return end

    if id == KING_TYPE_4 then

        local state = isFinishAll(human)
        if not state then
            return STATE_NONE
        end

        
        if not human.db.kingworld then return STATE_FINISH end  
        if human.db.kingworld[id] and human.db.kingworld[id].status == STATE_GET then
           return STATE_GET 
        else
            return STATE_FINISH
        end

    end
    --[[
 if human.db.kingworld[id].status == STATE_NONE then
        if canFinish(human, id) then
            return STATE_FINISH
        end
        return STATE_NONE
    end

    return human.db.kingworld[id].status

    ]]
   
end

function setState(human,id)
    if not isOpen(human) then return end
    initDB(human)
    human.db.kingworld[id].status = STATE_GET
    
	for k, v in pairs(funcID) do
	    YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3603)
        break
    end
end

-- 1-3都完成
function isFinishAll(human)
    for i = 1, KING_TYPE_3 do
        if not canFinish(human, i) then
            return
        end
    end
    return true
end


function updateDaily(human)
    if not isOpen(human) then
        -- 活动结束推送没有领取的奖励
        if human.db.kingworld then
            for i = KING_TYPE_1, KING_TYPE_4 do
                getReward(human, i, true)
            end
            human.db.kingworld = nil
        end
        return
    end

    initDB(human)
end



function isRed(human)
    if not isOpen(human) then return end

    for i = KING_TYPE_1, KING_TYPE_4 do
       if getState(human, i) == STATE_FINISH then
        return true 
       end
    end
end