--local SPELLS = {
	-- spellIDs to play sounds for
	-- Format: [spellID] = true, -- Spell Name
--[3044] = true, -- Arcane Shot
--[781] = true, -- Disengage
-- 136 -- Mend Pet
-- 34026 -- kill command
-- 19574 -- bestial wrath
-- 109248 -- binding shot
--}

local CALL_PETS = {
[883] = true, -- call pet 1 through 5
[83242] = true,
[83243] = true,
[83244] = true,
[83245] = true,
[982] = true, -- revive pet
}

local SOUNDS_HOP = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0142.mp3", -- hop
	"Interface\\AddOns\\KindredVO\\Sounds\\File0143.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0144.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0145.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0153.mp3", -- ha
	"Interface\\AddOns\\KindredVO\\Sounds\\File0154.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0155.mp3",
}

local SOUNDS_BINDINGSHOT = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0018.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0026.mp3",
}

local SOUNDS_PHOENIX = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0173.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0174.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0175.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0176.mp3",
}

local SOUNDS_KILLCOMMAND = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0011.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0014.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0015.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0016.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0017.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0146.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0147.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0148.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0149.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0150.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0151.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0152.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0032.mp3",
}

local SOUNDS_BESTIALWRATH = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0022.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0024.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0082.mp3",
}

local SOUNDS_LEVELUP = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0108.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0109.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0110.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0111.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0112.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0116.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0117.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0119.mp3",
}

local SOUNDS_PETSPRINTCHARGE = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0009.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0012.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0013.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0084.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0090.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0096.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0097.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0120.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0121.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0122.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0123.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0124.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0125.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0126.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0127.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0128.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0157.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0010.mp3",
}

local SOUNDS_AFK = {
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.idle01.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.idle02.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.idle03.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.idle04.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.idle05.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.idle06.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0105.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0106.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0107.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0129.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0131.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0067.mp3",
}

local SOUNDS_CALLPET = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0045.mp3", -- never one without the other
	"Interface\\AddOns\\KindredVO\\Sounds\\Kindred.start01.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0046.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0137.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0047.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0140.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0041.mp3",
}

local SOUNDS_CAMO = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0094.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0130.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0135.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0136.mp3",
}

local SOUNDS_STARTCOMBAT = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0025.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0085.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0086.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0087.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0089.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0093.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0098.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0099.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0100.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0102.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0134.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0138.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0159.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0160.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0164.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0158.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0065.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0101.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0133.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0118.mp3",
}

local SOUNDS_KILLSHOT = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0001.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0002.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0003.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0004.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0005.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0006.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0007.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0095.mp3",
}

local SOUNDS_RES = {
	"Interface\\AddOns\\KindredVO\\Sounds\\File0008.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0066.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0068.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0069.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0070.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0071.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0113.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0114.mp3",
	"Interface\\AddOns\\KindredVO\\Sounds\\File0115.mp3",
}


-- The sound channel to play sounds on.
-- This can be "Master", "SFX", "Music" or "Ambience"
local SOUND_CHANNEL = "Master"

-------------------
-- END OF CONFIG --
-------------------

-- Store the length of the SOUNDS table (i.e. how many sounds we have).
-- If this length wasn't constant, we'd probably just calculate in the UNIT_SPELLCAST_SUCCEEDED method instead of storing it here.
--local NUM_SOUNDS_ARCANESHOT = #SOUNDS_ARCANESHOT
--local NUM_SOUNDS_DISENGAGE = #SOUNDS_DISENGAGE

local f = CreateFrame("Frame")
f:SetScript("OnEvent", function(self, event, ...) -- Called when one of the events we're watching fires
	self[event](self, ...) -- Calls the event name as a method of f, i.e. `f:EVENT_NAME(arg1, arg2, arg3)`
end)

local time1 = time()

-- THIS IS VALUE OF MINIMUM DELAY IN SECONDS BETWEEN MOST COMMON VOICE EVENTS --
local dely = 30;

f:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player")
function f:UNIT_SPELLCAST_SUCCEEDED(unit, spellName, spellRank, lineID, spellID)
	if spellID == 781 then	-- Disengage
		local soundPath = SOUNDS_HOP[random(#SOUNDS_HOP)]
		time2 = time()
		if difftime(time2, time1) > 5
			then PlaySoundFile(soundPath, SOUND_CHANNEL); 
		end
	end
	if spellID == 34026 then	-- Kill Command
		local soundPath = SOUNDS_KILLCOMMAND[random(#SOUNDS_KILLCOMMAND)]
		time2 = time()
		if difftime(time2, time1) > dely
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
	if spellID == 19574 then	-- Bestial Wrath
		local soundPath = SOUNDS_BESTIALWRATH[random(#SOUNDS_BESTIALWRATH)]
		time2 = time()
		if difftime(time2, time1) > 5
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
	if spellID == 109248 then	-- Binding Shot
		local soundPath = SOUNDS_BINDINGSHOT[random(#SOUNDS_BINDINGSHOT)]
		time2 = time()
		if ((difftime(time2, time1) > 5) and (UnitExists("pet")==true))
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
	if ((CALL_PETS[spellID]) and (UnitAffectingCombat("player") == false)) then -- calling or reviving pet
		local soundPath = SOUNDS_CALLPET[random(#SOUNDS_CALLPET)]
		time2 = time()
		if difftime(time2, time1) > 5
		then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() end
	end
	if spellID == 51753 then	-- camo
		local soundPath = SOUNDS_CAMO[random(#SOUNDS_CAMO)]
		time2 = time()
		if difftime(time2, time1) > dely
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
	if spellID == 53351 then	-- kill shot
		local soundPath = SOUNDS_KILLSHOT[random(#SOUNDS_KILLSHOT)]
		time2 = time()
		if difftime(time2, time1) > dely
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
	if (((spellID == 75) or (spellID == 3044)) and (UnitExists("pet")==true)) then	-- auto shot and arcane shot -- when hunter starts combat, basically
		local soundPath = SOUNDS_STARTCOMBAT[random(#SOUNDS_STARTCOMBAT)]
		time2 = time()
		if difftime(time2, time1) > dely
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
end


-- greet on login
--local f2 = CreateFrame("FRAME", "FooAddonFrame");
--f2:RegisterEvent("PLAYER_ENTERING_WORLD");
--local function eventHandler(self, event, ...)
--if UnitName("pet")== "Волк" then 
--	C_Timer.After(5, function() PlaySoundFile("Interface\\AddOns\\KindredVO\\Sounds\\File0045.mp3", SOUND_CHANNEL) end) 
--end
--end
--f2:SetScript("OnEvent", eventHandler);

-- hum and talk when AFK
local f3 = CreateFrame("FRAME");
f3:RegisterEvent("PLAYER_FLAGS_CHANGED");
local function eventHandler2(self, event, ...)
local soundPath = SOUNDS_AFK[random(#SOUNDS_AFK)]
if UnitIsAFK("player") then PlaySoundFile(soundPath, SOUND_CHANNEL) end
end
f3:SetScript("OnEvent", eventHandler2);

-- CASTS MADE BY PET -- 
local f4 = CreateFrame("Frame")
f4:SetScript("OnEvent", function(self, event, ...)
	self[event](self, ...)
end)

f4:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "pet")
function f4:UNIT_SPELLCAST_SUCCEEDED(unit, spellName, spellRank, lineID, spellID)
	if spellID == 55709 then	-- Heart of Phoenix
		local soundPath = SOUNDS_PHOENIX[random(#SOUNDS_PHOENIX)]
		time2 = time()
		if difftime(time2, time1) > 5
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time()
		end
	end
	if ((spellID == 61684) or (spellID == 61685)) then	-- pet sprint and charge -- pet starts combat
		local soundPath = SOUNDS_PETSPRINTCHARGE[random(#SOUNDS_PETSPRINTCHARGE)]
		time2 = time()
		if difftime(time2, time1) > dely
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time()
		end
	end
	if spellID == 34026 then	-- Kill Command
		local soundPath = SOUNDS_KILLCOMMAND[random(#SOUNDS_KILLCOMMAND)]
		time2 = time()
		if difftime(time2, time1) > dely
			then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
		end
	end
end


local f5 = CreateFrame("FRAME");
f5:RegisterEvent("PLAYER_UNGHOST"); -- accepting a res
local function eventHandler5(self, event, ...)
	local soundPath = SOUNDS_RES[random(#SOUNDS_RES)]
	time2 = time()
	if difftime(time2, time1) > dely
		then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
	end
end
f5:SetScript("OnEvent", eventHandler5);

local f6 = CreateFrame("FRAME");
f6:RegisterEvent("PLAYER_ALIVE"); -- accepting a res
local function eventHandler6(self, event, ...)
	local soundPath = SOUNDS_RES[random(#SOUNDS_RES)]
	time2 = time()
	if difftime(time2, time1) > dely
		then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
	end
end
f6:SetScript("OnEvent", eventHandler6);

local f7 = CreateFrame("FRAME");
f7:RegisterEvent("PLAYER_LEVEL_UP"); -- ding
local function eventHandler7(self, event, ...)
	local soundPath = SOUNDS_LEVELUP[random(#SOUNDS_LEVELUP)]
	time2 = time()
	if difftime(time2, time1) > 5
		then PlaySoundFile(soundPath, SOUND_CHANNEL); time1 = time() 
	end
end
f7:SetScript("OnEvent", eventHandler7);