-- Media

Media = {}

Media.currBackgrounds = {}

local sBackgrounds =
{
    ['LocSecurityStationAmb'] = 'security_ambient'
}

local sBackgroundSets =
{
    ['Main'] = {'main_theme'},
    ['HOGameplay'] = {'loop_1', 'loop_2', 'loop_3', 'loop_4', 'loop_5', 'loop_6', 'loop_7', 'loop_8'},
    ['Collapse'] = {'collapse'},
    ['TextTwist'] = {'text_twist'},
    ['Match2'] = {'match2'},
    ['Cutscene'] = {'cutscenes'}
}

local sSoundEvents =
{
    ['hid.pickup'] = 'hid_pickup',
    ['map.open'] = 'map_open',
    ['bubble.close'] = 'bubble_close',
    ['hint.charged'] = 'hint_charge',
    ['hint.activate'] = 'hint_use',
    ['closeup.open'] = 'closeup_open',
    ['closeup.close'] = 'closeup_close',
    ['penalty'] = 'penalty',
    ['ttwist.enter'] = 'ttwist_letter_make',
    ['ttwist.twist'] = 'ttwist_letter_move',
    ['ttwist.put'] = 'ttwist_letter_put',
    ['collapse.bomb'] = 'collapse_bomb',
    ['collapse.colorBomb'] = 'collapse_colorBomb',
    ['collapse.freeze'] = 'collapse_frost',
    ['collapse.shuffle'] = 'collapse_shuffle',
    ['collapse.tiles'] = 'collapse_tiles', --
    ['match2.first'] = 'match2_match_1',
    ['match2.second'] = 'match2_match_2',
    ['match2.crystall'] = 'match2_crystall',
    ['mix'] = 'items_mix',

    ['int.on.LocSecurityStation.lock'] = 'signalling_turnoff',
    ['int.on.LocKitchen.traces'] = 'clean_traces',
    ['int.on.LocMailRoom.box1'] = 'box_open',
    ['int.on.LocGym.photo'] = 'take_photo',
    ['int.on.LocBeautySPA.socket'] = 'powder',
    ['int.on.LocSupplyCloset.electric'] = 'electricity',
    ['int.on.LocValetParking.window'] = 'parking_rag',
    ['int.on.LocCoffeshop.microwave'] = 'microwave',
    ['int.on.LocLaundry.washingMashine2'] = 'laundry_wash',
    ['int.on.LocPrivateRoom.tv'] = 'pRoom_tv',
    ['int.on.LocObservationDeck.door'] = 'observDeck_lift_open',
    ['int.on.LocNightClub.pipe'] = 'nClub_smoke',
    ['int.on.LocManagersOffice.fax'] = 'managers_fax',
    ['int.on.LocExecutiveWashroom.camera'] = 'eWashroom_camera',
    ['int.on.LocTailor.lamp'] = 'tailor_letter',
    ['int.on.LocConferenceRoom.vase2'] = 'cRoom_glue_vase',
    ['int.on.LocConferenceRoom.vase'] = 'cRoom_break_flowers',
    ['int.on.LocWinterGarden.signalization'] = 'wGarden_aerosol_signalling',
    ['int.on.LocValetParking.firealarm'] = 'vParking2_fire',
    ['int.on.LocTailor.curtain'] = 'tailor2_rope',
    ['int.on.LocSecurityStation.pult'] = 'security2_bottle',
    ['int.on.LocSupplyCloset.timetable'] = 'sCloset_table',
    ['int.on.LocNightClub.lamp'] = 'nClub2_ice',
    ['int.on.LocManagersOffice.monitor'] = 'managers2_note',
    ['int.on.LocMailRoom.safe'] = 'mailRoom2_safe',
    ['int.on.LocLaundry.sack'] = 'laundry2_key',
    ['int.on.LocConferenceRoom.flowers'] = 'cRoom2_Microphone',
    ['int.on.LocBeautySPA.chairs'] = 'bSPA2_uv',
    ['int.on.LocWinterGarden.petals'] = 'wGarden2_petals',

    ['parrot.voice'] = 'wGarden4_parrot'
}



function Media:playBackground(aScene)
    local tBackground = sBackgrounds[aScene]

    for i = 1, #Media.currBackgrounds do
        if tBackground == Media.currBackgrounds[i] then
            return
        end
    end

    if tBackground ~= nil then
        play_sound(tBackground, {loop = true, stream = true, group = group_music})
        table.insert(Media.currBackgrounds, tBackground)
    end
end

function Media:stopBackground(aScene)
    for i = #Media.currBackgrounds, 1, -1 do
        if Media.currBackgrounds[i] == sBackgrounds[aScene] then
            stop_sound(Media.currBackgrounds[i])
            table.remove(Media.currBackgrounds, i)
        end
    end
end

function Media:soundEvent(aEvent)
    local tSound = sSoundEvents[aEvent];

    if tSound ~= nil then
        play_sound(tSound, {loop = false, stream = false, group = group_effect})
    end
end

local sBackgroundSet = ''
local sCurrentSetItem = 0

function Media:switchBackgroundSet(set)
    assert(sBackgroundSets[set] ~= nil)

    if sBackgroundSet == set then
        return
    end

    if sBackgroundSet ~= '' then
        stop_sound(sBackgroundSets[sBackgroundSet][sCurrentSetItem])
    end

    sBackgroundSet = set
    backgroundSetCallback()
end

function backgroundSetCallback(soundName)
	if sBackgroundSets[sBackgroundSet][sCurrentSetItem] then
		stop_sound(sBackgroundSets[sBackgroundSet][sCurrentSetItem])
	end
    sCurrentSetItem = get_random(1, #sBackgroundSets[sBackgroundSet] + 1)
    play_sound(sBackgroundSets[sBackgroundSet][sCurrentSetItem],
        {loop = false, stream = true, group = group_music, callback = 'backgroundSetCallback'})
end
