local Msg = require("core.Msg")
local Util = require("common.Util")
local PaomaExcel = require("excel.paoma")
local HeroExcel = require("excel.hero").hero
local DrillLogic = require("drill.DrillLogic")
local ItemExcel = require("excel.item").item
local BattleExcel = require("excel.battle")
local EquipExcel = require("excel.equip").equip

PAOMA_TYPE_TIPS  = 1     -- 贴士
PAOMA_TYPE_BROAD = 2     -- 见闻
PAOMA_TYPE_TRIGGER = 3   -- 收获
PAOMA_TYPE_ACTIVITY = 4  -- 通告
PAOMA_TYPE_WARN    = 5   -- 警告

PAOMA_TYPE_BROAD_TYPE1 = 1	--高级抽卡抽到指定权重的英雄
PAOMA_TYPE_BROAD_TYPE2 = 2	--友情抽卡抽到指定权重的英雄
PAOMA_TYPE_BROAD_TYPE3 = 3	--基础抽卡抽到指定权重的英雄
PAOMA_TYPE_BROAD_TYPE4 = 4	--积分召唤获得英雄
PAOMA_TYPE_BROAD_TYPE5 = 5	--通关指定关卡（实际上是领取指定关卡奖励）
PAOMA_TYPE_BROAD_TYPE6 = 6	--通关通天塔（实际上是领取指定关卡奖励）
PAOMA_TYPE_BROAD_TYPE7 = 7	--起源圣树获得指定权重的英雄
PAOMA_TYPE_BROAD_TYPE8 = 8	--竞技场连胜次数
PAOMA_TYPE_BROAD_TYPE9 = 9	--试炼通关x关
PAOMA_TYPE_BROAD_TYPE10 =10	--许愿池抽奖抽到指定奖励档次的奖励
PAOMA_TYPE_BROAD_TYPE11 =11	--高级许愿池抽奖抽到指定奖励档次的奖励
PAOMA_TYPE_BROAD_TYPE12 =12	--进化学院合成5星6星英雄
PAOMA_TYPE_BROAD_TYPE13 =13	--英雄升星至x星
PAOMA_TYPE_BROAD_TYPE14 =14	--地牢通关x关
PAOMA_TYPE_BROAD_TYPE15 =15	--激活首充
PAOMA_TYPE_BROAD_TYPE16 =16	--激活特权卡
PAOMA_TYPE_BROAD_TYPE17 =17	--激活高级特权卡
PAOMA_TYPE_BROAD_TYPE18 =18	--达到VIP等级
PAOMA_TYPE_BROAD_TYPE19 =19	--完成君临天下全部任务
PAOMA_TYPE_BROAD_TYPE20 =20 --星空下的王者
PAOMA_TYPE_BROAD_TYPE21 =21 --精英抽卡抽到指定权重英雄
PAOMA_TYPE_BROAD_TYPE22 =22 --元宵活动放灯获得物品
PAOMA_TYPE_BROAD_TYPE23 =23 --植树节
PAOMA_TYPE_BROAD_TYPE24 =24 --公会红包
PAOMA_TYPE_BROAD_TYPE25 =25 --战神殿

PAOMA_TYPE_BROAD_CONDITION1 = 1 --高抽判定条件
PAOMA_TYPE_BROAD_CONDITION2 = 2 --副本闯关条件
PAOMA_TYPE_BROAD_CONDITION3 = 3 --通天塔闯关条件
PAOMA_TYPE_BROAD_CONDITION4 = 4 --竞技场连胜
PAOMA_TYPE_BROAD_CONDITION5 = 5 --竞技场连胜
PAOMA_TYPE_BROAD_CONDITION6 = 6 --竞技场连胜
PAOMA_TYPE_BROAD_CONDITION7 = 7 --竞技场连胜
PAOMA_TYPE_BROAD_CONDITION8 = 8 --战神殿胜利
PAOMA_TYPE_BROAD_CONDITION9 = 9 --发红包

PAOMA_TYPE_TIPS1 = 1 --- 欢迎进入
PAOMA_TYPE_TIPS2 = 2 --- 地牢开放

ACTIVITY_TYPE1 = 1 --- 圣树试炼
ACTIVITY_TYPE2 = 2 --- 冠军的试炼
ACTIVITY_TYPE3 = 3 --- 组队竞技场
ACTIVITY_TYPE4 = 4 --- 公会战
ACTIVITY_TYPE5 = 5 --- 地牢挑战

local ACTIVITY_TIPS = {}

local HERO_HUONV = 305006

local heroSs ={
	[1]="N",
	[2]="R",
	[3]="SR",
	[4]="SSR",
}



local BROADCAST_LIST = {}
local BROADCAST_INDEX = 0

local function paoma(human, msgType, recvMsg, isJson)
    if msgType == PAOMA_TYPE_BROAD then
		BROADCAST_INDEX = BROADCAST_INDEX + 1
		BROADCAST_LIST[BROADCAST_INDEX] = {}	
		BROADCAST_LIST[BROADCAST_INDEX].msgType = msgType
		BROADCAST_LIST[BROADCAST_INDEX].recvMsg = recvMsg
		BROADCAST_LIST[BROADCAST_INDEX].isJson = isJson
		BROADCAST_LIST[BROADCAST_INDEX].name = ""
		BROADCAST_LIST[BROADCAST_INDEX].index = BROADCAST_INDEX
		return
	end
	
	if human == nil then
		return
	end
	
	local send_msg = Msg.gc.GC_CHAT_PAOMA
	send_msg.item.msgType = msgType
	send_msg.item.msg = recvMsg
	send_msg.item.isJson = isJson
	send_msg.item.name = human.db.name
	Msg.send(send_msg, human.fd)
end

--1	播报
function broadcast(human, broadtype, condition, tb)
	local broadExcel = PaomaExcel.broad[broadtype]
	if broadExcel == nil or condition == nil then
		return
	end

	local len = #broadExcel.param
	local paramIndex = nil
	for i = 1, len do
		local param = broadExcel.param[i]
		if param == nil then
		   return
        end
		if condition == param then
			paramIndex = true
			break
		end
	end
	
    if paramIndex == nil and len ~= 0 then
		return		
	end
	
	local randomlen = #broadExcel.desc2
    if randomlen <= 0 then
		return
    end
	
	local random = math.random(1, randomlen)
	local desc2 = broadExcel.desc2[random]
	local desc = broadExcel.desc1 .. desc2
	
	local msg = nil
	if broadExcel.type == PAOMA_TYPE_BROAD_CONDITION1 then
		local heroID =  broadtype == PAOMA_TYPE_BROAD_TYPE15 and HERO_HUONV or tb	
        if heroID == nil then
	        return		
		end
		
		local star = HeroExcel[heroID].star
		local name = HeroExcel[heroID].name
		local grade = HeroExcel[heroID].grade
		local gradeDesc = heroSs[grade] or ""
		local heroColor = PaomaExcel.heroColor[star]
		if heroColor == nil then
			return
		end
		
		msg = Util.format(desc, human.db.name, heroColor.color, name, gradeDesc)

	elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION2 then
        local config = BattleExcel.node[condition]		
		if config == nil then
			return
		end
		
		local mapId = config.mapID
        local mapConfig =  BattleExcel.map[mapId]
		if mapConfig == nil then
			return
		end
        if config.tongguan[1] == nil then
            return
        end
		
		local itemId = config.tongguan[1][1]
		local itemCnt = config.tongguan[1][2]
		local itemName = ItemExcel[itemId].name
		local itemRare = ItemExcel[itemId].rare
        local itemColor = PaomaExcel.itemColor[itemRare].color
        msg = Util.format(desc, human.db.name, condition, mapConfig.name, itemColor, itemName)
	elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION3 then
		msg = Util.format(desc, human.db.name, condition)
	elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION4 then
		msg = Util.format(desc, human.db.name, condition)
	elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION5 then
		local heroID = tb
		if heroID == nil then
	        return
		end
		local star = HeroExcel[heroID].star
		local name = HeroExcel[heroID].name
		local heroColor = PaomaExcel.heroColor[star]
		if heroColor == nil then
			return
		end
		msg = Util.format(desc, human.db.name, heroColor.color, name, condition)
	elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION6 then
	
    elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION7 then
		msg = Util.format(desc, human.db.name,condition)
	elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION8 then
        msg = Util.format(desc, human.db.name, condition)
    elseif broadExcel.type == PAOMA_TYPE_BROAD_CONDITION9 then
        msg = Util.format(desc, human.db.name, condition)
	end
	
	if msg == nil then
		return
	end
	
	paoma(human, PAOMA_TYPE_BROAD, msg, 0)
end

-- 推送tips
function onLogin(human)
	tips(human)	
end

function loginTips(human)
    do return end
    if human.db.account == human.db.name then return end

	local tipsExcel = PaomaExcel.tips[PAOMA_TYPE_TIPS1]
	if tipsExcel ~= nil then
        local desc = Util.format(tipsExcel.desc, human.db.name, human.gameName)
		paoma(human, PAOMA_TYPE_TIPS, desc, 0)
	end
end

--2	tips
function tips(human)
    loginTips(human)
	
    local msgRet = Msg.gc.GC_CHAT_TIPS
	local tipsCnt = #PaomaExcel.tips
	local len = 0
	for i = 3, tipsCnt do
		if i > 50 then
			break
		end
		
		len = len + 1
		
		local tip = PaomaExcel.tips[i]
		msgRet.item[len].msgType = PAOMA_TYPE_TIPS1
		msgRet.item[len].msg = tip.desc
		msgRet.item[len].isJson = 0
		msgRet.item[len].name = ""
	end
	msgRet.item[0] = len
	Msg.send(msgRet, human.fd)
end


TRIGGER_TYPE_1 = 1
TRIGGER_TYPE_2 = 2
function trigger(human, actType, itemId, itemCnt, delItemId, delItemCnt)
	local triggerExcel = PaomaExcel.trigger[actType]
	if triggerExcel == nil then
		return
	end

    local item  = ItemExcel[itemId]
    if item == nil then
       item = EquipExcel[itemId]
    end

    if item == nil then
       return
    end
	
	local itemName = item.name
    local itemRare = item.rare
    local itemColor = PaomaExcel.itemColor[itemRare].color
    
    	
	local msg = nil
    if actType == TRIGGER_TYPE_1 then
		local delItemName = ItemExcel[delItemId].name
		local delItemRare = ItemExcel[delItemId].rare
		local delItemColor = PaomaExcel.itemColor[delItemRare].color
	
		msg = Util.format(triggerExcel.desc, delItemColor, delItemName, delItemCnt, itemColor, itemName, itemCnt)
	elseif actType == TRIGGER_TYPE_2 then
		msg = Util.format(triggerExcel.desc, itemColor, itemName, itemCnt)
    end
	if msg == nil then
		return
	end
	paoma(human, PAOMA_TYPE_TRIGGER, msg, 0)
end


local function checkAct(actType)
	local now = os.time()
	local startTime = Util.getDayStartTime()
	
	if ACTIVITY_TIPS[actType] == nil then
		ACTIVITY_TIPS[actType] = {}
	end
	local act = PaomaExcel.activity[actType]
	if act == nil then
		return
	end
	
	local actTips =  ACTIVITY_TIPS[actType]
	local tipTs = act.ts
	for i = 1, #tipTs do
        local tipTime = tipTs[i] * 60  + startTime
        if actTips[i] == nil and actTips[i] ~= tipTime and now > tipTime then
            index = i
			actTips[i] = tipTime
			return act
        end
	end
end

function activity()
	if _G.is_middle == true then return end
	
	--圣树试炼
	local isStart = DrillLogic.getState()
	if isStart == 1 then
		local act = checkAct(ACTIVITY_TYPE1)
        if act ~= nil then
            paoma(nil, PAOMA_TYPE_ACTIVITY, act.desc, 0)
        end
	end
	
	--公会战
--[[	if UnionWarLogic.getState() == UnionWarLogic.STATE_JIFEN then
		local act = checkAct(ACTIVITY_TYPE4)
        if act ~= nil then
            paoma(nil, PAOMA_TYPE_ACTIVITY, act.desc, 0)
        end	
	end	
	]]--
end

function sort(a,b)
    return a.index < b.index
end

function broad()
	if _G.is_middle == true then return end
	local broad = BROADCAST_LIST[1]
	if broad == nil then
		return
	end

	local send_msg = Msg.gc.GC_CHAT_PAOMA
	send_msg.item.msgType = broad.msgType
	send_msg.item.msg = broad.recvMsg
	send_msg.item.isJson = broad.isJson
	send_msg.item.name = broad.name
	Msg.sendWorld(send_msg)

	BROADCAST_LIST[1].index = os.time()

	table.sort(BROADCAST_LIST, sort)

	BROADCAST_LIST[BROADCAST_INDEX] = nil
	BROADCAST_INDEX = BROADCAST_INDEX - 1
end

function onTimer()
    broad()
    activity()
end

function notice(recvMsg)
    local send_msg = Msg.gc.GC_CHAT_PAOMA
    send_msg.item.msgType = PAOMA_TYPE_ACTIVITY
    send_msg.item.msg = recvMsg
    send_msg.item.isJson = 0
    send_msg.item.name = ""
    Msg.sendWorld(send_msg)
end