------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------

local love       = require 'love'
local make_class = require 'src.utils.classes'
local Asset      = require 'src.utils.asset'

------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------

-- SoundEffect is a simple wrapper around Löve's Source class to allow for
-- easy loading/unloading and automatically checking if a sound effect is valid
-- before using it.
local SoundEffect = make_class(Asset)


------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------

function SoundEffect:_init(file_name)
    Asset._init(self, file_name)
end


function SoundEffect:load()
    if not self:is_loaded() then
        self.sfx = love.audio.newSource(self.file_name)
    end
end


function SoundEffect:unload()
    if self:isPlaying() then self:stop() end
    self.sfx = nil
end


function SoundEffect:is_loaded()
    return self.sfx ~= nil
end


function SoundEffect:setVolume(volume)
    if self.sfx ~= nil then self.sfx:setVolume(volume) end
end


function SoundEffect:setPitch(pitch)
    if self.sfx ~= nil then self.sfx:setPitch(pitch) end
end


function SoundEffect:setLooping(enable)
    if self.sfx ~= nil then self.sfx:setLooping(enable) end
end


function SoundEffect:getDuration()
    if self.sfx ~= nil then
        return self.sfx:getDuration()
    else
        return nil
    end
end


function SoundEffect:isPlaying()
    return self.sfx ~= nil and self.sfx:isPlaying()
end


function SoundEffect:isPaused()
    return self.sfx ~= nil and self.sfx:isPaused()
end


function SoundEffect:isStopped()
    return self.sfx ~= nil and self.sfx:isStopped()
end


function SoundEffect:tell()
    if self.sfx ~= nil then
        return self.sfx:tell()
    else
        return nil
    end
end


function SoundEffect:play()
    if self.sfx ~= nil then self.sfx:play() end
end


function SoundEffect:pause()
    if self.sfx ~= nil then self.sfx:pause() end
end


function SoundEffect:stop()
    if self.sfx ~= nil then self.sfx:stop() end
end


------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

return SoundEffect
