-------------------------------------------------------
-- 竞技场相关活动管理

-- 列表显示用到
-- getActState 			活动状态
-- getActDesc 			活动描述
-- isActRed 			是否有红点
-- fontActArgs 			额外参数
-------------------------------------------------------

local JjcExcel = require("excel.jjc") 
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local JjcLogic = require("jjc.JjcLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local JjcNewLadderLogic = require("jjcnewladder.jjcNewLadderLogic")

-- 活动状态
STATE_NOOPEN = 0 	-- 未开启
STATE_NORMAL = 1 	-- 常驻活动
STATE_START = 2 	-- 赛季进行中
STATE_READY = 3 	-- 新赛季倒计时

-- id->actInfo
ID_2_ACTINFO = ID_2_ACTINFO or {}
ID_2_ACTINFO_MIDDLE = ID_2_ACTINFO_MIDDLE or {}

local function initActInfo(list, configs)
	for id, cf in pairs(configs) do 
		local moduleFn = nil 
		if cf.moduleFn ~= "" then
			moduleFn = load("return require(\""..cf.moduleFn.."\")")()		
		end

		if moduleFn and moduleFn.Init then
			moduleFn.Init()
		end

		local actInfo = {}
		actInfo.id = id 
		actInfo.config = cf
		actInfo.module = moduleFn
		list[id] = actInfo
	end
end

-- 初始
function init()
	initActInfo(ID_2_ACTINFO, JjcExcel.act)
	initActInfo(ID_2_ACTINFO_MIDDLE, JjcExcel.actMiddle)		
end

-- 获取actInfo
function getActInfo(isMiddle, id)
	local list = isMiddle and ID_2_ACTINFO_MIDDLE or ID_2_ACTINFO
	return list[id]
end

-- 获取活动状态
function getActState(isMiddle, id, human)
	local actInfo = getActInfo(isMiddle, id)
	if not actInfo then
		return STATE_NORMAL, 0
	end
	if not actInfo.module or 
		not actInfo.module.getActState then
		return STATE_NORMAL, 0
	end
	return actInfo.module.getActState(human)
end

-- 额外参数设置
function fontActArgs(isMiddle, id, args, human)
	args[0] = 0
	local actInfo = getActInfo(isMiddle, id)
	if not actInfo then
		return 
	end
	if not actInfo.module or 
		not actInfo.module.fontActArgs then
		return 
	end
	actInfo.module.fontActArgs(args, human)
end

-- 特殊描述
function getActDesc(isMiddle, id, cf)
	local actInfo = getActInfo(isMiddle, id)
	if not actInfo then
		return cf.desc 
	end
	if not actInfo.module or 
		not actInfo.module.getActDesc then
		return cf.desc
	end
	return actInfo.module.getActDesc(cf.desc)
end

-- 是否显示红点
function isActRed(isMiddle, id, human)
	local actInfo = getActInfo(isMiddle, id)
	if not actInfo then
		return
	end
	if not actInfo.module or 
		not actInfo.module.isActRed then
		return 
	end
	return actInfo.module.isActRed(human)
end

-- 封装活动结构体
local function fontJJCActNet(net, isMiddle, id, cf, human)
	net.id = id 
	net.panelID = cf.panelID
	net.bg = cf.bg 
	net.name = cf.name
	local state, leftTime = getActState(isMiddle, id, human)
	if RoleSystemLogic.isOpen(human, cf.systemID) then
		net.desc = getActDesc(isMiddle, id, cf)
		net.state = state
		net.leftTime = leftTime
	else
		net.desc = RoleSystemLogic.getOpenDesc(cf.systemID)
		net.state = STATE_NOOPEN
		net.leftTime = 0
	end	
	net.reward[0] = #cf.reward
	for i = 1, net.reward[0] do
		local itemID = cf.reward[i][1]
		local itemCnt = cf.reward[i][2]
		Grid.makeItem(net.reward[i], itemID, itemCnt)
	end	
	net.isRed = isActRed(isMiddle, id, human) and 1 or 0
	fontActArgs(isMiddle, id, net.args, human)
end

-- 活动列表
function sendActList(human)
	local msgRet = Msg.gc.GC_JJC_ACT_LIST
	msgRet.list[0] = 0
	for id, cf in ipairs(JjcExcel.act) do
		msgRet.list[0] = msgRet.list[0] + 1
		local net = msgRet.list[msgRet.list[0]]
		fontJJCActNet(net, false, id, cf, human)
	end
	msgRet.jjcDouble = JjcLogic.getJJCDoubleCnt(human)
    msgRet.worshipCnt = human.db.jjcBeWorship or 0
	Msg.send(msgRet, human.fd)
end


-- 跨服争霸活动列表
function sendMiddleActList(human)
	local msgRet = Msg.gc.GC_JJC_MIDDLE_ACT_LIST
	msgRet.list[0] = 0
	for id, cf in ipairs(JjcExcel.actMiddle) do
		msgRet.list[0] = msgRet.list[0] + 1
		local net = msgRet.list[msgRet.list[0]]
		fontJJCActNet(net, true, id, cf, human)
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 只有竞技场冠军试炼的
function isDot(human)
    if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1301, true) and
       not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1302, true) then
        return
    end

    if JjcLogic.championChallengeDot(human) == 1 or 
       JjcLogic.championBillboardDot(human) == 1 or 
       JjcLogic.championRecordDot(human) == 1 or
	   JjcNewLadderLogic.MainJieMianDot(human) == 1 then
        return true
    end
end