
TenOut = LibStub("AceAddon-3.0"):NewAddon("TenOut", "AceEvent-3.0", "AceConsole-3.0", "LibSink-2.0")
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
local SM = LibStub("LibSharedMedia-3.0")
SM:Register('sound', 'HoodWolf', [[Sound\Creature\HoodWolf\HoodWolfTransformPlayer01.wav]])
SM:Register('sound', 'FlameWreath', [[Interface\Addons\TenOut\sound\FlameWreath.wav]])

local trigger = false	

local function Colours(pack, alpha)
    local r, g, b = unpack(pack)
    return r, g, b, alpha
end
TenOut.Colours = Colours

local function GetSMIndex(t, value)
	for k, v in pairs(SM:List(t)) do
	if v == value then
		return k
		end
	end
	return nil
end

local defaults = {
	profile = {
		woptions = {
			col = { 1,.85,.62 }, 
			soundson = true,
			warningsound = "None",
			message = "Your RaidWarning message here!",
			colS = { 1,.43,.04 }, 
			soundsonS = true,
			warningsoundS = "FlameWreath",
			},
		soptions = {
			message = "RUN AWAY!!!",
			},
		sinkoptions = { sink20OutputSink = "UIErrorsFrame" },
	},	
}

	
local options = {
			type = "group",
			args = {
				woptions = {
				type = 'group',
				name = "Warning Options",
				desc = "Warning Options",
				order = 1,
				args = {
					blizwarn = {type = 'header', name = "Blizzard's BossEmote/RaidWarning message", order = 2,},
					TestWarning = {
						type = "execute",
						name = "Test Warnings",
						desc = "Test your current output settings.",
						func = function()
                            TenOut:RunTestW()
						end,
						order = 3,
						},
					barcolour = {
                        type = 'color',
                        name = "Warning Colour",
                        desc = "Change warning colour",
                        order = 4,
                        disabled = function()
                            return not TenOut.db.profile.woptions
						end,
                        get = function(info)
                            return unpack(TenOut.db.profile.woptions.col)
                        end,
                        set = function(info, r, g, b)
                            TenOut.db.profile.woptions.col = {r, g, b}
                        end,
                        hasAlpha = false 
						},
					on = {
                        type = 'toggle',
                        name = "Toggle Sounds",
                        desc = "Enable/Disable sounds",
                        order = 5,
                        disabled = function()
                            return not TenOut.db.profile.woptions
                        end,
                        get = function()
                            return TenOut.db.profile.woptions.soundson
                        end,
                        set = function()
                            TenOut.db.profile.woptions.soundson = not TenOut.db.profile.woptions.soundson
                        end
                        },
                    warningsound = {
                        type = 'select',
                        name = "Warning Sound",
                        desc = "Change the sound played when warning is reached",
                        order = 6,
                        disabled = function()
                            return not TenOut.db.profile.woptions.soundson or not TenOut.db.profile.woptions
                        end,
                        values = SM:List("sound"),
                        get = function() 
                            return GetSMIndex("sound", TenOut.db.profile.woptions.warningsound)
                        end,
                        set = function(info, v)
                            TenOut.db.profile.woptions.warningsound = SM:List("sound")[v]
                        end,
                        },
						blocked = {type = 'header', name = "Special Warnings", order = 7,},
					TestWarningS = {
						type = "execute",
						name = "Test Special Warnings",
						desc = "Test your current output settings.",
						func = function()
                            TenOut:RunTestS()
						end,
						order = 8,
						},
					barcolourS = {
                        type = 'color',
                        name = "Warning Colour",
                        desc = "Change warning colour",
                        order = 9,
                        disabled = function()
                            return not TenOut.db.profile.woptions
						end,
                        get = function(info)
                            return unpack(TenOut.db.profile.woptions.colS)
                        end,
                        set = function(info, r, g, b)
                            TenOut.db.profile.woptions.colS = {r, g, b}
                        end,
                        hasAlpha = false 
						},
					onS = {
                        type = 'toggle',
                        name = "Toggle Sounds",
                        desc = "Enable/Disable sounds",
                        order = 10,
                        disabled = function()
                            return not TenOut.db.profile.woptions
                        end,
                        get = function()
                            return TenOut.db.profile.woptions.soundsonS
                        end,
                        set = function()
                            TenOut.db.profile.woptions.soundson = not TenOut.db.profile.woptions.soundsonS
                        end
                        },
                    warningsoundS = {
                        type = 'select',
                        name = "Warning Sound",
                        desc = "Change the sound played when warning is reached",
                        order = 11,
                        disabled = function()
                            return not TenOut.db.profile.woptions.soundsonS or not TenOut.db.profile.woptions
                        end,
                        values = SM:List("sound"),
                        get = function() 
                            return GetSMIndex("sound", TenOut.db.profile.woptions.warningsoundS)
                        end,
                        set = function(info, v)
                            TenOut.db.profile.woptions.warningsoundS = SM:List("sound")[v]
                        end,
					},
					special = {type = 'header', name = "", order = 12,},
					},
					},
				soptions = {
				type = 'group',
				name = "Special warning Options",
				desc = "Special warning Options",
				order = 13,
				args = {
					special = {type = 'header', name = "Special Warning message", order = 6,},
					msg = {
						type = "input",
						name = "Output message text",
						desc = "Your output warning message here",
						usage = "<Input output message>",
						width = "full",
						order = 7,
						get = function(info) 
							return TenOut.db.profile.soptions.message
						end,
						set = function(info, newValue)
							TenOut.db.profile.soptions.message = newValue
						end,
					},
					msgtrack = {
						type = "input",
						name = "Tracked BossEmote",
						desc = "BossEmote tracked message",
						usage = "<Input tracked message>",
						multiline = true,
						width = "full",
						order = 8,
						get = false,
						set = function(info, value)
						table.insert( TenOut.db.profile.soptions.track, value )
						end,
					},
					Remove = {
						type = "multiselect",
						name = 'Mesage list',
						desc = 'Click to remove mesage',
						order = 9,
						values = function(info) 
							return TenOut.db.profile.soptions.track
						end,
						get = function() return nil end,
						set = function(info, value)
							table.remove( TenOut.db.profile.soptions.track, value )
						end,				
					},
				},	
			},
			output = TenOut:GetSinkAce3OptionsDataTable(),			
		},
}

AceConfig:RegisterOptionsTable("TenOut", options)

function TenOut:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("TenOut2DB", defaults, "Default")
	self:RegisterChatCommand("tenout", function() LibStub("AceConfigDialog-3.0"):Open("TenOut") end )
    self:RegisterChatCommand("TenOut", function() LibStub("AceConfigDialog-3.0"):Open("TenOut") end )
	local optFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("TenOut", "TenOut")
	options.args.profile = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
	self:SetSinkStorage(self.db.profile.sinkoptions)
	
	--default tracker's locale
	if not self.db.profile.soptions.track then 
		if(GetLocale() == "ruRU") then
			self.db.profile.soptions.track = {
			"%s смотрит на вас!",
			"Лава вокруг |3-1(%s) начинает бурлить!",
			}
		elseif(GetLocale() == "enUS") then
			self.db.profile.soptions.track = {
			"%s fixes his eyes on you!", -- Malygos(3 phase Surge)
			"The lava surrounding %s churns!",  --Sartharion(Lava wave)
			}
		elseif(GetLocale() == "frFR") then
			self.db.profile.soptions.track = {
			"%s fixe le regard sur vous !", 
			"La lave qui entoure %s bouillonne !",
			}
		elseif(GetLocale() == "deDE") then
			self.db.profile.soptions.track = {
			"Die Augen von %s sind auf Euch fixiert!", 
			"Die Lava um %s brodelt!",
			}			
		else
			self.db.profile.soptions.track = {}
		end	
	end	
	
end

function TenOut:OnEnable()
	self.db.profile.enabled = true
	self:RegisterEvent("CHAT_MSG_RAID_BOSS_EMOTE")
	self:RegisterEvent("CHAT_MSG_RAID_BOSS_WHISPER")	
end

function TenOut:OnDisable()
	self.db.profile.enabled = false
end

function TenOut:RunTestW()
	self:Pour(TenOut.db.profile.woptions.message,Colours(TenOut.db.profile.woptions.col))
	if TenOut.db.profile.woptions.soundson then
		PlaySoundFile(SM:Fetch("sound", TenOut.db.profile.woptions.warningsound))
	end	
end

function TenOut:RunTestS()
	self:Pour(TenOut.db.profile.soptions.message,Colours(TenOut.db.profile.woptions.colS))
	if TenOut.db.profile.woptions.soundsonS then
		PlaySoundFile(SM:Fetch("sound", TenOut.db.profile.woptions.warningsoundS))
	end	
end

function TenOut:CHAT_MSG_RAID_BOSS_EMOTE(event, msg, mob)
	TenOut:Check(event, msg, mob)
end

function TenOut:CHAT_MSG_RAID_BOSS_WHISPER(event, msg, mob)
	TenOut:Check(event, msg, mob)
end

function TenOut:Check(event, msg, mob)
	trigger=false
	if msg then
		for k, v in ipairs(TenOut.db.profile.soptions.track) do
			v = string.gsub(v, "||", "|")
			print(v)
			if msg == v then	
				trigger = true
			end
		end	
		TenOut:SpecOut(msg, mob)
	end
end

function TenOut:SpecOut(msg, mob)
	if trigger then
		TenOut:Pour(string.format(msg, mob).." \n"..TenOut.db.profile.soptions.message,Colours(TenOut.db.profile.woptions.colS))
		if TenOut.db.profile.woptions.soundsonS then
			PlaySoundFile(SM:Fetch("sound", TenOut.db.profile.woptions.warningsoundS))
		end
	else
		TenOut:Pour(string.format(msg, mob),Colours(TenOut.db.profile.woptions.col))
		if TenOut.db.profile.woptions.soundson then
			PlaySoundFile(SM:Fetch("sound", TenOut.db.profile.woptions.warningsound))
		end
	end
end

local hooks = {} 
hooks["RaidNotice_AddMessage"] = RaidNotice_AddMessage();
	
RaidNotice_AddMessage = function(noticeFrame, textString, colorInfo)
	if noticeFrame == RaidWarningFrame then
		TenOut:Pour(textString, Colours(TenOut.db.profile.woptions.col))
		return noticeFrame:Hide()
	end
end	
