local mod	= DBM:NewMod("RomuloAndJulianne", "DBM-Karazhan")
local L		= mod:GetLocalizedStrings()

mod:SetRevision(("$Revision: 175 $"):sub(12, -3))
mod:SetCreatureID(17534, 17533)

mod:RegisterCombat("yell", L.RJ_Pull)
mod:RegisterKill("yell", L.Bogus)--there isn't actually a yell, but we use this to prevent mod from ending combat early using UNIT_DIED after they both die once.
mod:SetWipeTime(25)--guesswork

mod:RegisterEvents(
	"SPELL_CAST_START",
	"SPELL_AURA_APPLIED",
	"SPELL_AURA_APPLIED_DOSE",
	"SPELL_AURA_REMOVED",
	"CHAT_MSG_MONSTER_YELL",
	"UNIT_DIED",
	"CHAT_MSG_RAID_WARNING"
)

local warnPhase2		= mod:NewPhaseAnnounce(2)
local warnPhase3		= mod:NewPhaseAnnounce(3)
local warningHeal		= mod:NewCastAnnounce(30878, 4)
local warningDaring		= mod:NewTargetAnnounce(30841, 3)
local warningDevotion	= mod:NewTargetAnnounce(30887, 3)
local warningPosion		= mod:NewAnnounce("warningPosion", 2, 30830, mod:IsHealer() or mod:IsTank())

-- Heroic
local WarnHeartbroken		= mod:NewAnnounce(L.WarnHeartbroken, 2, 85237) 
local WarnLove				= mod:NewAnnounce(L.WarnLove, 2, 85236) 

local timerHeal				= mod:NewCastTimer(5, 30878)
local timerDaring			= mod:NewTargetTimer(8, 30841)
local timerDevotion			= mod:NewTargetTimer(10, 30887)
local timerCombatStart		= mod:NewTimer(55, "TimerCombatStart", 2457)
local timerNextSpotlight	= mod:NewTimer(30, L.OperaSpotlight, 85112)

mod:AddBoolOption("HealthFrame", true)

local phases = {}
local JulianneDied = 0
local RomuloDied = 0
local LoversSpam = 0
local heartbrokenStacks = 0

local function updateHealthFrame(phase)--WIP
	if phases[phase] then
		return
	end
	phases[phase] = true
	if phase == 1 then
		DBM.BossHealth:Clear()
		DBM.BossHealth:AddBoss(17534, L.Julianne)
	elseif phase == 2 then--UNIT_DIED event triggers not tested yet
		DBM.BossHealth:AddBoss(17533, L.Romulo)
		warnPhase2:Show()
	elseif phase == 3 then
		DBM.BossHealth:AddBoss(17534, L.Julianne)
		DBM.BossHealth:AddBoss(17533, L.Romulo)
	end
end

function mod:OnCombatStart(delay)
	updateHealthFrame(1)
	JulianneDied = 0
	RomuloDied = 0
	timerNextSpotlight:Start(20-delay)
	self.vb.phase = 1
	heartbrokenStacks = 0
end

function mod:SPELL_CAST_START(args)
	if args:IsSpellID(30878) then
		warningHeal:Show()
		if mod:IsDifficulty("normal25", "heroic10", "heroic25") then
			timerHeal:Start()
		else
			timerHeal:Start(2)
		end
	end
end

function mod:SPELL_AURA_APPLIED(args)
	if args:IsSpellID(30822, 30830) then
		warningPosion:Show(args.spellName, args.destName, args.amount or 1)
	elseif args:IsSpellID(30841) then
		warningDaring:Show(args.destName)
		timerDaring:Start(args.destName)
	elseif args:IsSpellID(30887) then
		warningDevotion:Show(args.destName)
		timerDevotion:Start(args.destName)
	elseif args:IsSpellID(85237) then  -- Heartbroken
		heartbrokenStacks = args.amount
		if args.amount and (GetTime() - LoversSpam) > 5 and args.amount >= 10 and args.amount % 5 == 0 then
			LoversSpam = GetTime()
			WarnHeartbroken:Show(args.amount, args.spellName)
		end
	elseif args:IsSpellID(85236) then
		if heartbrokenStacks > 0 then
			heartbrokenStacks = heartbrokenStacks - 0.5
		end

		if args.amount and (GetTime() - LoversSpam) > 5 and heartbrokenStacks <= 5 and args.amount % 5 == 0 and args.amount >= 15 then -- The Power of Love
			LoversSpam = GetTime()
			WarnLove:Show(args.amount, args.spellName)
		end 
	end
end

mod.SPELL_AURA_APPLIED_DOSE = mod.SPELL_AURA_APPLIED

function mod:SPELL_AURA_REMOVED(args)
	if args:IsSpellID(30841) then
		timerDaring:Cancel(args.destName)
	elseif args:IsSpellID(30887) then
		timerDevotion:Cancel(args.destName)
	end
end

function mod:CHAT_MSG_MONSTER_YELL(msg)
	if msg == L.DBM_RJ_PHASE2_YELL or msg:find(L.DBM_RJ_PHASE2_YELL) then
		warnPhase3:Show()
		updateHealthFrame(3)
		self.vb.phase = 3
	elseif msg == L.Event or msg:find(L.Event) then
		timerCombatStart:Start()
	end
end


function mod:UNIT_DIED(args)
	local cid = self:GetCIDFromGUID(args.destGUID)
	if cid == 17534 then
		if phase == 3 then--Only want to remove from boss health frame first time they die, and kill only in phase 3.
			JulianneDied = GetTime()
			if (GetTime() - RomuloDied) < 10 then
				mod:EndCombat()
			end
		else
			DBM.BossHealth:RemoveBoss(cid)
			updateHealthFrame(2)
			self.vb.phase = 2
		end
	elseif cid == 17533 then
		if phase == 3 then--Only want to remove from boss health frame first time they die, and kill only in phase 3.
			RomuloDied = GetTime()
			if (GetTime() - JulianneDied) < 10 then
				mod:EndCombat()
			end
		else
			DBM.BossHealth:RemoveBoss(cid)
		end
	end
end