'''
Created on 28 aout 2009

@author: Samuel Benveniste
'''

import os
import sys
import subprocess
import re

import pygame
import pygame.midi
import pickle

from gui.constants import *

from gui.PlayingScreen import PlayingScreen
from gui.InstrumentChoice import InstrumentChoice
from instruments.Instrument import Instrument
from cursor.WarpingCursor import *
from controllers.Wiimote import Wiimote
from songs.Song import Song
from gui.StaticFamiliarizer import StaticFamiliarizer
from gui.SongFamiliarizer import SongFamiliarizer
from gui.SongPlayingScreen import SongPlayingScreen
from gui.DummyInstrumentChoice import DummyInstrumentChoice

class FamiliarizerLog():
    '''
    classdocs
    '''

    def __init__(self,eventLog,level,activeWiimotes):
        '''
        Constructor
        '''
        self.eventLog = eventLog
        self.level = level
        self.activeWiimotes = activeWiimotes
        self.scale = scaleDict["majorScale"]   
        
if __name__ == '__main__':
        
        f = file('../../../saves/19-01-2009-testcoll1-v65-1.fmwi', 'r')
        unpickler = pickle.Unpickler(f)
        log = unpickler.load()
        f.close()
        
        pygame.init()
        modeResolution = (1024,768)
        window = pygame.display.set_mode(modeResolution,pygame.FULLSCREEN)
        pygame.midi.init()
        instruments = [Instrument(log.scale, i + 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList[i], ".jpg"]), octaves[i]) for i in range(9)]
        
        joys = [[id,pygame.joystick.Joystick(id).get_name()] for id in range(pygame.joystick.get_count())]
        for joy in joys:
            if joy[1] in joyNames:
                pygame.joystick.Joystick(joy[0]).init() 
        
        ports = [pygame.midi.get_device_info(id)[1] for id in range(pygame.midi.get_count())]
        portOffset = ports.index(portNames[0])
        print(portOffset)
        
        screen = pygame.display.get_surface()
        clock = pygame.time.Clock()        
        cursorImages=[['../cursor/cursorImages/black/10.png'],['../cursor/cursorImages/red/10.png'],['../cursor/cursorImages/blue/10.png'],['../cursor/cursorImages/green/10.png']]
        durations = [75 for i in range(len(cursorImages))]
        
        extsc = True
        casc = False
        easyMode = True
        
        song = Song(scaleDict["majorScale"],[3,9,6,4,1,8,5,7,2,10],True)

        wiimoteCount = 4
        
        cursors = [WarpingCursor(None, cursorImages[i], durations, (300 * i, 300 * i),'../cursor/cursorImages/black/flash.png') for i in range(wiimoteCount)]
        wiimotes = [Wiimote(i, i + portOffset, None, instruments[0], cursors[i]) for i in range(wiimoteCount)]
        dummyInstrumentChoice = DummyInstrumentChoice(wiimotes, window, screen, clock, joys, portOffset, log.activeWiimotes)
        if log.level < 2 :
            familiarize = StaticFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,log.activeWiimotes,level = log.level,eventLog = log.eventLog,replay = True)
        elif log.level == 2 :
            familiarize = SongFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,song,log.activeWiimotes,casc,extsc,easyMode,eventLog = log.eventLog,replay = True)
        else :
            familiarize = SongPlayingScreen(dummyInstrumentChoice,songDict["clairdelalune"],easyMode = True,eventLog = log.eventLog,replay = True)
                        
        while familiarize.nextLevel != None :
            if familiarize.nextLevel < 2 :
                familiarize = StaticFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,log.activeWiimotes,level = familiarize.nextLevel,eventLog = familiarize.eventLog,replay = True)
            elif familiarize.nextLevel == 2 :
                familiarize = SongFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,song,log.activeWiimotes,casc,extsc,easyMode,eventLog = familiarize.eventLog,replay = True)
            else :
                familiarize = SongPlayingScreen(dummyInstrumentChoice,songDict["clairdelalune"],easyMode = True,eventLog = familiarize.eventLog,replay = True)
            
        for wiimote in wiimotes:
            del wiimote.port            
        
        pygame.midi.quit()
        pygame.quit()