# -*- coding: utf-8 -*-
"""
Écran de jeu MinWii :
bandes arc-en-ciel représentant un clavier.

$Id$
$URL$
"""
import pygame
from cursors import WarpingCursor
from column import Column
import events
from eventutils import event_handler, EventDispatcher, EventHandlerMixin
import types
from musicxml import Tone

from config import FRAMERATE
from config import BORDER
from config import FIRST_HUE
from config import DEFAULT_MIDI_VELOCITY

from globals import BACKGROUND_LAYER
from globals import CURSOR_LAYER
from globals import PLAYING_MODES

class _PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :

    def __init__(self, synth, distinctNotes=[]) :
        """
        distinctNotes : notes disctinctes présentes dans la chanson
        triées du plus grave au plus aigu.
        """
        super(_PlayingScreenBase, self).__init__()
        self.synth = synth
        self.distinctNotes = distinctNotes
        self.keyboardLength = 0
        self.keyboardRects = []
        self.cursor = None
        self._initRects()
        self.columns = {}
        self._initColumns()
        self._running = False
        self.draw(pygame.display.get_surface())
        self._initCursor()    
    
    def _initRects(self) :
        """ création des espaces réservés pour
            afficher les colonnes.
        """
        #ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
        #if ambitus <= 12 :
        #    self.keyboardLength = 8
        #else :
        #    self.keyboardLength = 11
        self.keyboardLength = len(self.distinctNotes)
        
        screen = pygame.display.get_surface()

        # taille de la zone d'affichage utile (bordure autour)
        dispWidth = screen.get_width() - 2 * BORDER
        dispHeight = screen.get_height() - 2 * BORDER
        
        columnWidth = int(round(float(dispWidth) / self.keyboardLength))

        rects = []
        for i in range(self.keyboardLength) :
            upperLeftCorner = (i*columnWidth + BORDER, BORDER)
            rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight))
            rects.append(rect)
        
        self.keyboardRects = rects
    
    def _initColumns(self) :
        
        hueStep = FIRST_HUE / (self.keyboardLength - 1)
        for i, rect in enumerate(self.keyboardRects) :
            hue = FIRST_HUE - hueStep * i
            tone = self.distinctNotes[i]
            c = Column(self, hue, rect, tone)
            self.add(c, layer=BACKGROUND_LAYER)
            self.columns[tone.midi] = c
        
    
    def _initCursor(self) :
        self.cursor = WarpingCursor(blinkMode=True)
        self.add(self.cursor, layer=CURSOR_LAYER)
        
    def run(self):
        self._running = True
        clock = pygame.time.Clock()
        pygame.display.flip()
        pygame.mouse.set_visible(False)
        while self._running :
            EventDispatcher.dispatchEvents()
            dirty = self.draw(pygame.display.get_surface())
            pygame.display.update(dirty)
            clock.tick(FRAMERATE)

        pygame.mouse.set_visible(True)
        self.cursor._stopBlink()

    @event_handler(pygame.KEYDOWN)       
    def handleKeyDown(self, event) :
        if event.key == pygame.K_q:
            self._running = False
    
    @event_handler(pygame.MOUSEBUTTONDOWN)
    def onMouseDown(self, event) :
        # TODO à cleaner : on vire le dernier élément
        # parce qu'il s'agit du curseur
        for col in reversed(self.sprites()[:-1]) :
            if col.rect.collidepoint(*event.pos):
                self.raiseColDown(col)
                break

    @event_handler(pygame.MOUSEBUTTONUP)
    def onMouseUp(self, event) :
        for col in reversed(self.sprites()[:-1]) :
            if col.rect.collidepoint(*event.pos) :
                self.raiseColUp(col)
                break

    @event_handler(pygame.MOUSEMOTION)
    def onMouseMove(self, event) :
        for col in reversed(self.sprites()[:-1]) :
            if col.rect.collidepoint(*event.pos) :
                self.raiseColOver(col)
                break

    def raiseColDown(self, col) :
        evt = pygame.event.Event(events.COLDOWN, column=col)
        pygame.event.post(evt)

    def raiseColUp(self, col) :
        evt = pygame.event.Event(events.COLUP, column=col)
        pygame.event.post(evt)
    
    def raiseColOver(self, col) :
        evt = pygame.event.Event(events.COLOVER, column=col)
        pygame.event.post(evt)

        
class PlayingScreen(_PlayingScreenBase) :
    "fenêtre de jeu pour improvisation"
    
    scale = [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]

    def __init__(self, synth) :
        distinctNotes = []
        for midi in self.scale :
            tone = Tone(midi)
            distinctNotes.append(tone)
        
        super(PlayingScreen, self).__init__(synth, distinctNotes)
            
    @event_handler(events.NOTEON)
    def noteon(self, evt) :
        tone = evt.tone
        self.synth.noteon(0, tone.midi, DEFAULT_MIDI_VELOCITY)

    @event_handler(events.NOTEOFF)
    def noteoff(self, evt) :
        tone = evt.tone
        self.synth.noteoff(0, tone.midi)


class SongPlayingScreen(_PlayingScreenBase) :
    
    def __init__(self, synth, song, mode=PLAYING_MODES['EASY']) :
        super(SongPlayingScreen, self).__init__(synth, song.distinctNotes)
        self.song = song
        self.currentColumn = None
        self.noteIterator = self.song.iterNotes()
        self.displayNext()
        if mode == PLAYING_MODES['NORMAL'] :
            EventDispatcher.addEventListener(events.COLDOWN, self.handleColumnDown)
            EventDispatcher.addEventListener(events.COLUP, self.handleColumnUp)
        elif mode == PLAYING_MODES['EASY'] :
            EventDispatcher.addEventListener(events.COLOVER, self.handleColumnOver)
            
    
    def displayNext(self, event=None) :
        if self.currentColumn:
            self.currentColumn.update(False)
        note, verseIndex = self.noteIterator.next()
        syllabus = note.lyrics[verseIndex].syllabus()
        column = self.columns[note.midi]
        column.update(True, syllabus)
        self.currentColumn = column
        self.currentNote = note
        self.currentNotePlayed = False
    
    def handleColumnDown(self, event) :
        col = event.column
        if col.state:
            self.synth.noteon(0, col.tone.midi, DEFAULT_MIDI_VELOCITY)
            self.currentNotePlayed = True
    
    def handleColumnUp(self, event) :
        if self.currentNotePlayed :
            self.synth.noteoff(0, self.currentColumn.tone.midi)
            self.displayNext()
    
    def handleColumnOver(self, event) :
        col = event.column
        if col.state and not self.currentNotePlayed :
            self.synth.noteon(0, col.tone.midi, DEFAULT_MIDI_VELOCITY)
            SongPlayingScreen.setNoteTimeout(int(self.currentNote.duration * 600))
            self.currentNotePlayed = True
    
    @event_handler(events.NOTEEND)
    def clearTimeOutAndDisplayNext(self, evt) : 
        pygame.time.set_timer(evt.type, 0)
        self.displayNext()
    
    @staticmethod
    def setNoteTimeout(delay) :
        pygame.time.set_timer(events.NOTEEND, delay)

