local _, MerfinPlus = ...
local LSM = LibStub("LibSharedMedia-3.0") 

local stformat = string.format
local SpeakText = C_VoiceChat.SpeakText

local localization = MerfinPlus.localization

---------------------
-- Registering Sounds
---------------------

local soundsFileNames = {}

soundsFileNames.noloc = {
    'Alert Bell',
    'Important Mechanic',
    'Soft Alert',
    'Error',
    'Info Beep',
    'Level Up',
    'Spell On You',
    'Alarm',
    'Alert',
    'Pull Timer Start',
    'Boing',
    'Bloop',
    'Frog',
    'Chomp',
    'Bonk',
    'Info',
    'Long',
    'Spell Under You',
} 

soundsFileNames.General = {
    '1',
    '2',
    '3',
    '4',
    '5',
    'Fondago - 1',
    'Fondago - 2',
    'Fondago - 3',
    'Fondago - 4',
    'Fondago - 5',
    'Sally - 1',
    'Sally - 2',
    'Sally - 3',
    'Sally - 4',
    'Sally - 5',
    'Alex - 1',
    'Alex - 2',
    'Alex - 3',
    'Alex - 4',
    'Alex - 5',
    'Add Spawned',
    'Adds Spawned',
    'Air Phase',
    'Avoid',
    'Bait',
    'Burst the boss',
    'Defensive',
    'Face away',
    'Frontal',
    'Interrupt',
    'Knockback',
    'Move the boss',
    'Phase 1',
    'Phase 2',
    'Phase 3',
    'Phase 4',
    'Prepare for Frontal',
    'Run Away',
    'Run in',
    'Run out',
    'Spread out',
    'Stack with raid',
    'Spread',
    'Stack',
    'Stop Casting',
    'Switch Targets',
    'Taunt',
    'Watch your feet',
    'Dance',
    'Plus Damage',
    'Close',
    'Middle',
    'Far',
    'Explosion in',
}

soundsFileNames.CataRaid_Firelands = {
    'Spinners',
    'Sally - Devastation in',
    'Shannox Spawned',
    'Meteor',
    'Concussive Stomp',
    'Drinking Magma',
    'Seeds',
    'Orbs',
    'Left Blow',
    'Middle Blow',
    'Right Blow',
    'Dreadflames',
    'Shards',
    'Firestorm',
}

local soundPaths = {} -- Here we store our sound paths

local mainPath = [[Interface\Addons\MerfinPlus\Media\sound]]
local GetSoundPath = function(folderPath, soundFileName)
    return stformat([[%s\%s\%s.mp3]], mainPath, folderPath, soundFileName)
end

local RegisterSounds = function(folderName, folderPath, RegisterLSM)
    for _, soundName in ipairs(soundsFileNames[folderName]) do
        local soundPath = GetSoundPath(folderPath, soundName)
        soundPaths[soundName] = soundPath
        if RegisterLSM then LSM:Register("sound", stformat('M: %s', soundName), soundPath) end
    end
end

----------------
-- WeakAuras API
----------------

Merfin.PlaySound = function(replica, speed, volume, voiceId)
    -- Since russian TTS is so trash we force Prerecorded Sounds to play
    if Merfin.L == 'ruRU' then
        local soundPath = soundPaths[replica]
        if soundPath then PlaySoundFile(soundPath, 'Master') end
    else 
        local soundPath = soundPaths[replica]
        if soundPath and PlaySoundFile(soundPath, 'Master') then 
            -- Success
        elseif Merfin.L == 'enUS' then 
            SpeakText(voiceId or 1, replica, Enum.VoiceTtsDestination.LocalPlayback, speed or 1, volume or 100)
        end
    end
end

Merfin.RegisterSoundPaths = function()
    RegisterSounds('noloc', 'noloc', true)
    RegisterSounds('General', stformat([[%s\%s]], Merfin.GetLocale(), 'General'), true)
    RegisterSounds('CataRaid_Firelands', stformat([[%s\%s]], Merfin.GetLocale(), 'CataRaid_Firelands'))
end

Merfin.RegisterSoundPaths()