local NoticeExcel = require("excel.notice")
local HeroExcel = require("excel.hero")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")


DEFAULT_SYSTEM_SET = 0 -- 默认设置

FAQ_TYPE_1 = 1 -- 1:faq
FAQ_TYPE_2 = 2 -- 2:help

-- 客户端设置
function set(human, flag)
	human.db.systemSet = flag
	
	if human.db.systemSet == DEFAULT_SYSTEM_SET then
		human.db.systemSet = nil
	end
	
	query(human)
end

-- 客户端设置查询
function query(human)
	local msgRet = Msg.gc.GC_SETTING_SYSTEM
	msgRet.flag = human.db.systemSet or DEFAULT_SYSTEM_SET
	Msg.send(msgRet, human.fd)
end

-- 帮助分类查询
function faqQueryBase(human)
	local config = NoticeExcel.faq

	local msgRet = Msg.gc.GC_FAQ_QUERY_BASE
	msgRet.list[0] = #config
	for i = 1, #config do
		msgRet.list[i].id = i
		msgRet.list[i].title = config[i].title
	end
	
	Msg.send(msgRet, human.fd)
end

-- 帮助分类具体内容查询
function faqQueryDetail(human, id)
	local config = NoticeExcel.faq
	
	local msgRet = Msg.gc.GC_FAQ_QUERY_DETAIL
	msgRet.id = id
	msgRet.content = config[id].content
	Msg.send(msgRet, human.fd)
end

-- 通用的问号叹号面板查询
function helpMiscQuery(human, id)
	local config = NoticeExcel.tips[id]
	if config == nil then
		assert(nil)
	end
	local msgRet = Msg.gc.GC_HELP_MISC_QUERY
	msgRet.id = id
	msgRet.title = config.title
    msgRet.type = config.type
    msgRet.content = config.content
	Msg.send(msgRet, human.fd)	
end

----------------------------------------------
-------------- 设置相关具体功能---------------
----------------------------------------------
function isNoShowVip(human)
	local flag = human.db.systemSet or DEFAULT_SYSTEM_SET
	local isNoShow = Util.getBit(flag, 3)
	if isNoShow == 1 then 
		return true
	end
end

function isNoPrivateChat(human)
    local flag = human.db.systemSet or DEFAULT_SYSTEM_SET
	local isNoPrivateChat = Util.getBit(flag, 4)
	if isNoPrivateChat == 1 then 
		return true
	end
end

function explainDetail(human, id)
    local config = NoticeExcel.strategy[id]
	if config == nil then
		assert(nil)
	end

    local msgRet = Msg.gc.GC_EXPLAIN_DETAIL
    msgRet.id = id
	msgRet.title = config.title
	msgRet.content = config.content
	Msg.send(msgRet, human.fd)
end

local function wrapperGetwayInfo(human, net, id, conf)
     if not conf then
		conf = NoticeExcel.resource[id]
	end

	net.id = id
    net.title = tostring(conf.title)
    net.desc = tostring(conf.desc)
    net.icon = tostring(conf.icon)

    net.funlist[0] = 0
    for _,v in ipairs(conf.func) do
        net.funlist[0] = net.funlist[0] + 1
        net.funlist[net.funlist[0]].id = v[1]
        net.funlist[net.funlist[0]].name = tostring(v[2])
        net.funlist[net.funlist[0]].desc = v[3] and tostring(v[3]) or ""
    end
end

function resourceQuery(human)
   local msgRet = Msg.gc.GC_RESOURCE_QUERY
   msgRet.list[0] = 0
   for id, conf in Util.pairsByKeys(NoticeExcel.resource) do
		msgRet.list[0] = msgRet.list[0] + 1
		wrapperGetwayInfo(human, msgRet.list[msgRet.list[0]], id, conf)
   end

    Msg.send(msgRet, human.fd)
end

function strategyQuery(human)
   local config = NoticeExcel.strategy
   local msgRet = Msg.gc.GC_STRATEGY_QUERY
   local len = #config
   msgRet.list[0] = len

	for i = 1, len do
        local index = 0
		msgRet.list[i].id = i
		msgRet.list[i].name = config[i].name
        msgRet.list[i].desc = config[i].desc or ""
        if config[i].team then
            local teamLen = #config[i].team 
            for j = 1,teamLen do
                local heroID = config[i].team[j]
                local heroMsg = HeroExcel.hero[heroID]
                index = index + 1
                msgRet.list[i].team[index].icon = heroMsg.head
                msgRet.list[i].team[index].lv = 100
                msgRet.list[i].team[index].camp = heroMsg.camp
                msgRet.list[i].team[index].star = heroMsg.star
                msgRet.list[i].team[index].id = heroID
                msgRet.list[i].team[index].label = heroMsg.label
            end
        end
        msgRet.list[i].team[0] = index
	end
	
	Msg.send(msgRet, human.fd)
end

-- 声音设置
function settingSound(human, music, acoustic, voice, isSend)
    human.db.systemSound = human.db.systemSound or {}
    human.db.systemSound.music = music
    human.db.systemSound.acoustic = acoustic
    human.db.systemSound.voice = voice
   
    if isSend then
        settingSoundQuery(human)
    end
end

-- 发送声音设置
function settingSoundQuery(human)
    if not human.db.systemSound then return end
    if not human.db.systemSound.music then return end
	
    local msgRet = Msg.gc.GC_SETTING_SOUND
    msgRet.music = human.db.systemSound.music or 0
    msgRet.acoustic = human.db.systemSound.acoustic or 0
    msgRet.voice = human.db.systemSound.voice or 0
    Msg.send(msgRet, human.fd)
end

-- 语言设置
function settingLang(human, lang)
    human.db.systemSound = human.db.systemSound or {}
    human.db.systemSound.lang = lang
    settingLangQuery(human)
end

-- 发送语言设置
function settingLangQuery(human)
    if not human.db.systemSound then return end
    local msgRet = Msg.gc.GC_SETTING_LANGUAGE
    msgRet.lang = human.db.systemSound.lang or 0
    Msg.send(msgRet, human.fd)
end


function onLogin(human)
    settingSoundQuery(human)
	settingLangQuery(human)
end