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 = {
    'Air Horn',
    'AlertBell',
    'Important Mechanic',
    'Soft Alert',
    'Sonar',
    'Error',
    'Info Beep',
    'Level Up',
    'Bike Horn',
    'Spell On You',
    'Alarm',
    'Alert',
    'Bite',
} 

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 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',
}

soundsFileNames.CataRaid_BWD = {
    'Keep staying inside',
    'Burst the boss',
    'Health Drop Cast Soon',
    'Pool is exploding',
    --'Prepare to run down',
    'Reduce sound',
    'Kite the beam',
    'Magmatron',
    'Electron',
    'Toxitron',
    'Arcanatron',
    'Shield used',
    'Dark Phase',
    'Green Phase',
    'Blue Phase',
    'Red Phase',
    'Spark'
}

soundsFileNames.CataRaid_T4W = {
    'Short',
    'Long',
}

soundsFileNames.CataRaid_Bastion = {
    'Stay among ranges',
    'Stay among melees',
    'Spread out or stop casting',
    'Orbs Soon',
    'Whelps',
    'Get Winds',
    'Get Grounded',
    'Watch Range from Blue Boss',
    'Prepare for Deep Breath'
}

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_BWD', stformat([[%s\%s]], Merfin.GetLocale(), 'CataRaid_BWD'))
    RegisterSounds('CataRaid_T4W', stformat([[%s\%s]], Merfin.GetLocale(), 'CataRaid_T4W'))
    RegisterSounds('CataRaid_Bastion', stformat([[%s\%s]], Merfin.GetLocale(), 'CataRaid_Bastion'))
    RegisterSounds('CataRaid_Firelands', stformat([[%s\%s]], Merfin.GetLocale(), 'CataRaid_Firelands'))
end

Merfin.RegisterSoundPaths()