---------------------------------------------------------------------------------
--
-- Prat - A framework for World of Warcraft chat mods
--
-- Copyright (C) 2006-2007  Prat Development Team
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to:
--
-- Free Software Foundation, Inc., 
-- 51 Franklin Street, Fifth Floor, 
-- Boston, MA  02110-1301, USA.
--
--
-------------------------------------------------------------------------------

--[[
Name: PratSounds
Revision: $Revision: 46606 $
Author(s): Sylvanaar - Copy/Pasted from ChatSounds hy TotalPackage
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#Filtering
Subversion: http://svn.wowace.com/wowace/trunk/Prat/
Discussions: http://groups.google.com/group/wow-prat
Issues and feature requests: http://code.google.com/p/prat/issues/list
Description: A port of the Chatsounds addon to the Prat framework. (default=off).
Dependencies: Prat
]]

-- Get Utility Libraries
local util, DBG, CLR = GetPratUtils()

-- set prat module name
local PRAT_MODULE = Prat:RequestModuleName("PratSounds")

if PRAT_MODULE == nil then 
    return 
end

-- define localized strings
local L = AceLibrary("AceLocale-2.2"):new(PRAT_MODULE)

L:RegisterTranslations("enUS", function() return {
	["Sounds"] = true,
	["A module to play sounds on certain chat messages."] = true,
} end)

--[[
	Chinese Local : CWDG Translation Team 昏睡墨鱼 (Thomas Mo)
	CWDG site: http://Cwowaddon.com
	$Rev: 46606 $
	$Date: 2007-08-15 06:47:05 +0200 (Wed, 15 Aug 2007) $
]]

L:RegisterTranslations("zhCN", function() return {
	["Sounds"] = "声音",
	["A module to play sounds on certain chat messages."] = "特定聊天消息出现时播放声音提醒.",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Sounds"] = "音效",
	["A module to play sounds on certain chat messages."] = "在特定聊天訊息出現時播放音效",
} end)

L:RegisterTranslations("deDE", function() return {
	["Sounds"] = "Sounds",
	["A module to play sounds on certain chat messages."] = "Ein Modul um Sounds jeh nach Chatnachrichtenart abzuspielen.",
} end)

L:RegisterTranslations("koKR", function() return {
	["Sounds"] = "효과음",
	["A module to play sounds on certain chat messages."] = "특정 대화 메세지에 효과음을 재생하기 위한 모듈입니다.",
} end)

L:RegisterTranslations("esES", function() return {
	["Sounds"] = "Sonidos",
	["A module to play sounds on certain chat messages."] = "Un módulo que permite reproducir sonidos para ciertos mensajes de chat.",
} end)

-- get prat module categories
local cat = Prat.Categories

-- create prat module
Prat_Sounds = Prat:NewModule(PRAT_MODULE)
Prat_Sounds.revision = tonumber(string.sub("$Revision: 46606 $", 12, -3))

-- define key module values
Prat_Sounds.moduleName = L["Sounds"] 	
Prat_Sounds.moduleDesc = L["A module to play sounds on certain chat messages."] 	

Prat_Sounds.consoleName = string.lower(Prat_Sounds.moduleName)
Prat_Sounds.guiName = Prat_Sounds.moduleName

Prat_Sounds.Categories	= { cat.FILTER, cat.SOUNDS }	
				-- list here: http://groups-beta.google.com/group/wow-prat/web/prat-categories

-- init moduleOoptions (which gets populated by the other
-- various options tables below
Prat_Sounds.moduleOptions = {}

-- default values for any settings that need them
Prat_Sounds.defaultDB = {
	on	= false,
	["incoming"] = {
		["GUILD"] = "Kachink",
		["OFFICER"] = "Link",
		["PARTY"] = "Text1",
		["RAID"] = "Text2",
		["WHISPER"] = "Heart",
	},
	["outgoing"] = {
		["GUILD"] = "none",
		["OFFICER"] = "none",
		["PARTY"] = "none",
		["RAID"] = "none",
		["WHISPER"] = "none",
	},
	["customlist"] = {
		"healer",
		"pall",
		"priest",
		"warrior",
		"mage",
		"hunter",
		"druid",
		"lock",
		"rogue",
	},
}

-- any boolean options
Prat_Sounds.toggleOptions = { sep19_sep=19, sep9_sep=9 }


--[[------------------------------------------------
    Module Event Functions
------------------------------------------------]]--
-- things to do when the module is enabled
function Prat_Sounds:OnModuleEnable()
--[[
	self:RegisterEvent("CHAT_MSG_CHANNEL", "ChatEvent")
	
	self:RegisterEvent("CHAT_MSG_GUILD", "ChatEvent")
	self:RegisterEvent("CHAT_MSG_OFFICER", "ChatEvent")
	self:RegisterEvent("CHAT_MSG_PARTY", "ChatEvent")
	self:RegisterEvent("CHAT_MSG_RAID", "ChatEvent")
	self:RegisterEvent("CHAT_MSG_RAID_LEADER", "ChatEvent")
	self:RegisterEvent("CHAT_MSG_BATTLEGROUND", "ChatEvent")
	self:RegisterEvent("CHAT_MSG_BATTLEGROUND_LEADER", "ChatEvent")
	
	self:RegisterEvent("CHAT_MSG_WHISPER", "ChatEvent")
--]]		
	self:RegisterEvent(Prat.Events.OUTBOUND)	
	self:RegisterEvent(Prat.Events.POST_ADDMESSAGE)	
	
	self:RegisterEvent("SharedMedia_Registered")
end

-- things to do when the module is disabled
function Prat_Sounds:OnModuleDisable()
	-- unregister events
	self:UnregisterAllEvents()
end

function Prat_Sounds:Prat_OutboundChat(m)
    if self:IsDebugging() then
        self:Debug("Prat_OutboundChat", m.CTYPE, Prat:GetEventID())
    end
end

local soundslist = {}

local media = AceLibrary("SharedMedia-1.0")

function Prat_Sounds:BuildSoundList()
    for i,v in ipairs(soundslist) do
        soundslist[i] = nil
    end
    
    for k,v in pairs(Prat.sounds) do
        table.insert(soundslist, k)
    end

    for k,v in pairs(media:List(media.MediaType.SOUND)) do
        if not Prat.sounds[v] then 
            table.insert(soundslist, v)
        end
    end
    
    -- SML has a None sound
    --table.insert(soundslist, "none")
end

function Prat_Sounds:SharedMedia_Registered(mediatype, name)
    if mediatype == media.MediaType.SOUND then
        self:BuildSoundList()
    end
end

function Prat_Sounds:GetModuleOptions()
	local db = self.db.profile
	
	self:BuildSoundList()
	
    self.moduleOptions = {
        name = L["Sounds"],
        desc = L["A module to play sounds on certain chat messages."],
        type = "group",
		args = {
			custom = {
				type = "text",
				name = "Add a custom channel",
				desc = "Play a sound for a certain channel name (can be a substring)",
				set = function(v) tinsert(db.customlist, v) end,
				get = function() return end,
				usage = "",
				order = 3,
			},
			
            removenick = {
                name = "Remove a custom channel",
                desc = "Remove a custom channel",
                type = "text",
                order = 4,
				current = "",
				get = function() return "" end,
				validate = db.customlist,
                disabled = function() return #db.customlist == 0 end,
				set = function(v) for i,v2 in ipairs(db.customlist) do if v == v2 then tremove(db.customlist, i) end end end,
            },			
			reset = {
				type = "execute",
				name = "Reset settings",
				desc = "Restore default settings and resets custom channel list",
				func = function() db.customlist = { } Prat_Sounds:ResetDB() end,
				order = 10,
			},
			inc = {
				type = "group",
				name = "Incoming Sounds",
				desc = "Sound selection for incoming chat messages",
				order = 20,
				hidden = function() Prat_Sounds:BuildSoundList() end,
				args = {
					party = {
						type = "text",
						name = "Party",
						desc = "Sound for incoming party messages",
						get = function() return db.incoming["PARTY"] end,
						set = function(v) Prat:PlaySound(v) db.incoming["PARTY"] = v end,
						order = 1,
						validate = soundslist,
					},
					raid = {
						type = "text",
						name = "Raid",
						desc = "Sound for incoming raid or battleground group/leader messages",
						get = function() return db.incoming["RAID"] end,
						set = function(v) Prat:PlaySound(v) db.incoming["RAID"] = v end,
						order = 2,
						validate = soundslist,
					},
					guild = {
						type = "text",
						name = "Guild",
						desc = "Sound for incoming guild messages",
						get = function() return db.incoming["GUILD"] end,
						set = function(v) Prat:PlaySound(v) db.incoming["GUILD"] = v end,
						order = 3,
						validate = soundslist,
					},
					officer = {
						type = "text",
						name = "Officer/Custom",
						desc = "Sound for incoming officer or custom channel messages",
						get = function() return db.incoming["OFFICER"] end,
						set = function(v) Prat:PlaySound(v) db.incoming["OFFICER"] = v end,
						order = 4,
						validate = soundslist,
					},
					whisper = {
						type = "text",
						name = "Whisper",
						desc = "Sound for incoming whisper messages",
						get = function() return db.incoming["WHISPER"] end,
						set = function(v) Prat:PlaySound(v) db.incoming["WHISPER"] = v end,
						order = 5,
						validate = soundslist,
					},
				},
			},
			out = {
				type = "group",
				name = "Outgoing Sounds",
				desc = "Sound selection for outgoing (from you) chat messages",
				order = 30,
				args = {
					party = {
						type = "text",
						name = "Party",
						desc = "Sound for outgoing party messages",
						get = function() return db.outgoing["PARTY"] end,
						set = function(v) Prat:PlaySound(v) db.outgoing["PARTY"] = v end,
						order = 1,
						validate = soundslist,
					},
					raid = {
						type = "text",
						name = "Raid",
						desc = "Sound for outgoing raid or battleground group/leader messages",
						get = function() return db.outgoing["RAID"] end,
						set = function(v) Prat:PlaySound(v) db.outgoing["RAID"] = v end,
						order = 2,
						validate = soundslist,
					},
					guild = {
						type = "text",
						name = "Guild",
						desc = "Sound for outgoing guild messages",
						get = function() return db.outgoing["GUILD"] end,
						set = function(v) Prat:PlaySound(v) db.outgoing["GUILD"] = v end,
						order = 3,
						validate = soundslist,
					},
					officer = {
						type = "text",
						name = "Officer/Custom",
						desc = "Sound for outgoing officer or custom channel messages",
						get = function() return db.outgoing["OFFICER"] end,
						set = function(v) Prat:PlaySound(v) db.outgoing["OFFICER"] = v end,
						order = 4,
						validate = soundslist,
					},
					whisper = {
						type = "text",
						name = "Whisper",
						desc = "Sound for outgoing whisper messages",
						get = function() return db.outgoing["WHISPER"] end,
						set = function(v) Prat:PlaySound(v) db.outgoing["WHISPER"] = v end,
						order = 5,
						validate = soundslist,
					},
				},
			},
		},
	}
	
  	return self.moduleOptions	
end


--[[------------------------------------------------
	Core Functions
------------------------------------------------]]--
function Prat_Sounds:Prat_PostAddMessage(message, frame, event, text, r, g, b, id)
    if Prat:GetEventID() and Prat:GetEventID() == self.lastevent and self.lasteventtype == event then return end
    
	local msgtype = string.sub(event, 10)
	local plr, svr = strsplit("-", (message.PLAYERLINK or ""))
	local outgoing = (plr == UnitName("player")) and true or false
	local sndprof = outgoing and self.db.profile.outgoing or self.db.profile.incoming

    if self:IsDebugging() then 
        self:Debug("Prat_PostAddMessage", msgtype, frame:GetName(), Prat:GetEventID())
    end  
    
    if msgtype == "CHANNEL" then 
	    local chan = string.lower(message.ORG.CHANNEL)
        if self:IsDebugging() then 
            self:Debug("ChatEvent", event, chan)
        end
		for _,value in pairs(self.db.profile.customlist) do
			if strlen(value) > 0 and string.find(chan, string.lower(value)) then
                self:PlaySound(sndprof["OFFICER"], event)
			end
		end
    else 
		if msgtype == "WHISPER_INFORM" then
		    msgtype = "WHISPER"
		    sndprof = self.db.profile.outgoing
		elseif msgtype == "WHISPER" then
		    sndprof = self.db.profile.incoming
		end
		
		if msgtype == "RAID_LEADER" or msgtype == "BATTLEGROUND" or msgtype == "BATTLEGROUND_LEADER" then
			msgtype = "RAID"
		end
 
		self:PlaySound(sndprof[msgtype], event)
    end
end


function Prat_Sounds:PlaySound(sound, event)
    if self:IsDebugging() then 
        self:Debug("PlaySound", sound, Prat:GetEventID())
    end
    self.lasteventtype = event
    self.lastevent = Prat:GetEventID()
    Prat:PlaySound(sound)
end