function SoundPlay(sound_name, loop_flag, fade_time)
    loop_flag = loop_flag or false
    fade_time = fade_time or 0.0
    
    return cpp_sound_play(sound_name, loop_flag, fade_time)
end

function StopSound(sound_id, fade_time)
    fade_time = fade_time or 0;
    if(sound_id) then
        cpp_sound_stop(sound_id, fade_time)
    end
end


function MusicPlay(misic_table, level_id)
    if(type(misic_table) ~= "table") then return end
    if(level_id == last_level_id) then return end
    last_level_id = level_id;
    
    --if(last_music) then       
        StopMusic()
    --end
    
    current_music_num = 1;
    play_music_loop(misic_table, level_id)
end   

function MusicPause(pause)
    PauseMusic(pause);
end

function play_music_loop(misic_table, level_id)
    if(level_id ~= last_level_id) then return false end
    if(misic_table[current_music_num] == nil) then current_music_num = 1; end
    if(misic_table[current_music_num] == nil) then return end
    
    StopRequests(220)
    
    if(type(misic_table[current_music_num]) == "string") then
        StopMusic();
        local _id = PlayMusic(misic_table[current_music_num], false)
        last_music = {name = misic_table[current_music_num], id = _id};
        current_music_num = current_music_num + 1
        
        DelayedExec(GetMusicLenght(_id), function()
            play_music_loop(misic_table, level_id)
        end, 220)
    else
        last_music = nil;
        DelayedExec(misic_table[current_music_num], function()
            play_music_loop(misic_table, level_id)
        end, 220)
        current_music_num = current_music_num + 1
    end
end

function __stop_all_sfx()
    if(not sfx_names) then return end
    
    for key, value in pairs(sfx_names) do
        StopSound(value[2])
    end
    
    sfx_names = {}
end

function __sfx_push(_sound_name, _id)
    sfx_names = sfx_names or {}
    table.insert(sfx_names, {_sound_name, _id})
end 

function __sfx_pop(_sound_name)
    if(not sfx_names) then return end
    
    for key, value in pairs(sfx_names) do
        if(value[1] == _sound_name) then
            table.remove(sfx_names, key)
            break;
        end
    end
end

function __is_can_play_sfx(_sound_name)
    if(not sfx_names) then return true end
    
    for key, value in pairs(sfx_names) do
        if(value[1] == _sound_name) then
            return false;
        end
    end
    
    return true;
end

function PlaySfx(sound_name)
    if(not __is_can_play_sfx(sound_name)) then return end
    
    local id = SoundPlay(sound_name);
    
    __sfx_push(sound_name, id);
    DelayedExec(cpp_get_sound_lenght(id), function()
        __sfx_pop(sound_name)
    end)
end

function PlayTableSfx(_table, sound_name)
    for key, value in pairs(_table) do
        if(not __is_can_play_sfx(value)) then 
            return false
        end
    end
    
    local id = SoundPlay(sound_name);
    
    __sfx_push(sound_name);
    DelayedExec(cpp_get_sound_lenght(id), function()
        __sfx_pop(sound_name)
    end)
    
    return true
end
