#!/usr/bin/env python
# encoding: utf-8
"""
quiz-console.py

Created by Jordan Thevenow-Harrison on 2008-04-05.
"""

DELAY = 2 #in seconds
SOUNDLOCATION = '/Users/jtth/Documents/Projects/Classes/COGS-Q 270/proj 1/experiment program/sounds/*'
OUTPUTLOCATION = '/Users/jtth/Desktop/'

import sys
import os
import glob
import time

def outputCSV(data):
    

def prompt(soundfilepath):
    guess = raw_input()#some fuction to get user input
    return guess
    
def muxmap(ls1, ls2):
    '''makes a dictionary from merging ls1 and ls2, where ls1 are keys and ls2 are values'''
    d={}
    if len(ls1) == len(ls2):
        for i in map(None,ls1,ls2):
            d[i[0]]=i[1]
        return d
    else:
        return "Oh oh oh oh something bad happened oh oh oh oh."
        
################################################################
        
def macprog():
    '''This is a hack to make sound work on both mac and windows. linux can blow itself.''' 
    
    class MacSound:
        '''Dunno who wrote this, but it`s all over the internet.'''
        def __init__(self, filepath):
            self._sound = NSSound.alloc()
            self._sound.initWithContentsOfFile_byReference_(filepath, True)
        def play(self): self._sound.play()
        def stop(self): self._sound.stop()
        def is_playing(self): return self._sound.isPlaying()

    def main():
        itempaths = glob.glob(SOUNDLOCATION)
        soundfilepaths = []
        for itempath in itempaths:
            itemname = os.path.split(itempath)[1]
            soundfilepaths.append(itempath)
        sndresp = []
        for soundfilepath in soundfilepaths:
            macsound = MacSound(soundfilepath)
            macsound.play()
            sndresp.append(prompt(soundfilepath))
            time.sleep(DELAY)
            while True:
                if not macsound.is_playing():
                    break
        data = muxmap(soundfilepaths,sndresp)
        print data
    main() # run everything

###############################################################

def winprog():
    itempaths = glob.glob(SOUNDLOCATION)
    soundfilepaths = []
    for itempath in itempaths:
        itemname = os.path.split(itempath)[1]
        soundfilepaths.append(itempath)
    sndresp = []
    for sound in soundfilepaths:
        winsound.PlaySound(sound) # hopefully this will wait until playback is done to move on
        time.sleep(DELAY)
    data = muxmap(soundfilepaths,sndresp)
    print data

###############################################################

if __name__ == '__main__':
    try:
        from AppKit import NSSound
        macprog()
    except ImportError:
        from winsound import *
        winprog()
