---------------------------------------------------------------------------------
--
-- 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: 30740 $
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
]]

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

-- 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)

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("zhTW", function() return {
	["Sounds"] = "聲音",
	["A module to play sounds on certain chat messages."] = "收到訊息時會發出提示聲音的模組",
} 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: 30740 $", 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("CHAT_MSG_WHISPER_INFORM", "ChatEvent")	
end

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


-- Todo, grab the sounds from Prat.Sounds
local soundslist = {}

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
    
    table.insert(soundslist, "none")
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_Sounds: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_Sounds: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_Sounds: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_Sounds: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_Sounds: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_Sounds: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_Sounds: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_Sounds: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_Sounds: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_Sounds:PlaySound(v) db.outgoing["WHISPER"] = v end,
						order = 5,
						validate = soundslist,
					},
				},
			},
		},
	}
	
  	return self.moduleOptions	
end


--[[------------------------------------------------
	Core Functions
------------------------------------------------]]--

function Prat_Sounds:ChatEvent(a1, a2, a3, a4, a5, a6, a7, a8, a9, ...)
--    self:Debug("ChatEvent", event, a1, a2, a3, a4, a5, a6, a7, a8, a9, ...)

	local msgtype = string.sub(event, 10)
	local plr, svr = strsplit("-", (a2 or ""))
	local outgoing = (plr == UnitName("player")) and true or false
	local sndprof = outgoing and self.db.profile.outgoing or self.db.profile.incoming

    if msgtype == "CHANNEL" then 
	    local chan = string.lower(a9)
        self:Debug("ChatEvent", event, chan)
		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"])
			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:Debug("ChatEvent", event, msgtype)
		self:PlaySound(sndprof[msgtype])
    end
end

function Prat_Sounds:PlaySound(sound)
	if not sound or not Prat.sounds[sound] then return end
	
	if sound ~= "TellMessage" then
		PlaySoundFile(Prat.sounds[sound])
	else
		PlaySound(Prat.sounds[sound])
	end
end

