# -*- coding: utf-8 -*-
"""
constantes de configuration

$Id$
$URL$
"""

import pygame
import os.path
from ConfigParser import ConfigParser

_here = os.path.abspath(__file__).split(os.path.sep)[:-1]

def _computePath(path) :
    path = path.split('/')
    path = _here + path
    path = os.path.abspath(os.path.sep.join(path))
    return path

def str2IntTuple(s) :
    return tuple(map(int, s.strip('()').split(',')))


pygame.font.init()

conf = ConfigParser()

conf.add_section('playingscreen')
conf.set('playingscreen', 'FRAMERATE', '100')
conf.set('playingscreen', 'FIRST_HUE', '0.6')
conf.set('playingscreen', 'OFF_LUMINANCE', '0.2')
conf.set('playingscreen', 'OFF_SATURATION', '1')
conf.set('playingscreen', 'ON_TOP_LUMINANCE', '0.6')
conf.set('playingscreen', 'ON_BOTTOM_LUMINANCE', '0.9')
conf.set('playingscreen', 'ON_SATURATION', '1')
conf.set('playingscreen', 'ON_COLUMN_OVERSIZING', '2')
conf.set('playingscreen', 'ON_COLUMN_ALPHA', '1')
conf.set('playingscreen', 'FONT_COLOR', '(0,0,0)')
conf.set('playingscreen', 'MIDI_VELOCITY_RANGE', '(64, 127)')
conf.set('playingscreen', 'MIDI_PAN_RANGE', '(32, 96)')
conf.set('playingscreen', 'MIDI_VELOCITY_WRONG_NOTE_ATTN', '0.5')
conf.set('playingscreen', 'IR_POSITION', 'ABOVE')
conf.set('playingscreen', 'SCREEN_RESOLUTION', '(1024, 768)')

conf.add_section('locations')
conf.set('locations', 'notes_font_file', _computePath('fonts/Arial Unicode.ttf'))
conf.set('locations', 'SONG_FILE_PATH', _computePath('../chansons'))
conf.set('locations', 'LOGS_DIR', os.path.join(os.path.expanduser('~'), 'minwii_logs'))
conf.set('locations', 'SOUND_FONT', _computePath('soundfonts/Minwii-light-soundfont.sf2'))

# customisation
conf.read(_computePath('../minwii.ini'))

FRAMERATE = conf.getint('playingscreen', 'FRAMERATE')
FIRST_HUE = conf.getfloat('playingscreen', 'FIRST_HUE')
OFF_LUMINANCE = conf.getfloat('playingscreen', 'OFF_LUMINANCE',)
OFF_SATURATION = conf.getfloat('playingscreen', 'OFF_SATURATION')
ON_TOP_LUMINANCE = conf.getfloat('playingscreen', 'ON_TOP_LUMINANCE')
ON_BOTTOM_LUMINANCE = conf.getfloat('playingscreen', 'ON_BOTTOM_LUMINANCE')
ON_SATURATION = conf.getfloat('playingscreen', 'ON_SATURATION')
ON_COLUMN_OVERSIZING = conf.getfloat('playingscreen', 'ON_COLUMN_OVERSIZING')
ON_COLUMN_ALPHA = conf.getfloat('playingscreen', 'ON_COLUMN_ALPHA')
FONT_COLOR = str2IntTuple(conf.get('playingscreen', 'FONT_COLOR'))
MIDI_VELOCITY_RANGE = str2IntTuple(conf.get('playingscreen', 'MIDI_VELOCITY_RANGE'))
MIDI_PAN_RANGE = str2IntTuple(conf.get('playingscreen', 'MIDI_PAN_RANGE'))
MIDI_VELOCITY_WRONG_NOTE_ATTN = conf.getfloat('playingscreen', 'MIDI_VELOCITY_WRONG_NOTE_ATTN')
IR_POSITION = conf.get('playingscreen', 'IR_POSITION')
SCREEN_RESOLUTION = str2IntTuple(conf.get('playingscreen', 'SCREEN_RESOLUTION'))

SONG_FILE_PATH = conf.get('locations', 'SONG_FILE_PATH')
LOGS_DIR = conf.get('locations', 'LOGS_DIR')
SOUND_FONT = conf.get('locations', 'SOUND_FONT')

NOTES_FONT = pygame.font.Font(conf.get('locations', 'notes_font_file'), 50)
LYRICS_FONT = pygame.font.Font(None, 80)

instruments = ConfigParser()
instruments.add_section('accordeon')
instruments.set('accordeon', 'bank', '0')
instruments.set('accordeon', 'preset', '23')
instruments.set('accordeon', 'pos', '0')

instruments.add_section('carillon')
instruments.set('carillon', 'bank', '0')
instruments.set('carillon', 'preset', '8')
instruments.set('carillon', 'octave', '1')
instruments.set('carillon', 'pos', '1')

instruments.add_section('flute')
instruments.set('flute', 'bank', '0')
instruments.set('flute', 'preset', '73')
instruments.set('flute', 'pos', '2')

instruments.add_section('guitare')
instruments.set('guitare', 'bank', '0')
instruments.set('guitare', 'preset', '24')
instruments.set('guitare', 'octave', '-1')
instruments.set('guitare', 'pos', '3')

instruments.add_section('orgue')
instruments.set('orgue', 'bank', '0')
instruments.set('orgue', 'preset', '19')
instruments.set('orgue', 'pos', '4')

instruments.add_section('piano')
instruments.set('piano', 'bank', '0')
instruments.set('piano', 'preset', '0')
instruments.set('piano', 'pos', '5')

instruments.add_section('tuba')
instruments.set('tuba', 'bank', '0')
instruments.set('tuba', 'preset', '58')
instruments.set('tuba', 'octave', '-2')
instruments.set('tuba', 'pos', '6')


instruments.add_section('violon')
instruments.set('violon', 'bank', '0')
instruments.set('violon', 'preset', '40')
instruments.set('violon', 'pos', '7')

instruments.add_section('violoncelle')
instruments.set('violoncelle', 'bank', '0')
instruments.set('violoncelle', 'preset', '42')
instruments.set('violoncelle', 'octave', '-2')
instruments.set('violoncelle', 'pos', '8')

# customisation
instruments.read(_computePath('../instruments.ini'))


INSTRUMENTS = []
for section in instruments.sections() :
    items = instruments.items(section)
    instru = dict([(k, int(v)) for k, v in items])
    instru['octave'] = instru.get('octave', 0)
    instru['name'] = section
    INSTRUMENTS.append(instru)

INSTRUMENTS.sort(lambda a, b : cmp(a['pos'], b['pos']))
map(lambda a : a.pop('pos'), INSTRUMENTS)

#INSTRUMENTS = (
#    {'name' : 'violoncelle',
#     'bank'   : 0,
#     'preset' : 42,
#     'octave' : -2
#    },
#)
#
#for i in INSTRUMENTS :
#    i['octave'] = i.get('octave', 0)


if __name__ == '__main__' :
    for f in (conf, instruments) :
        for section in f.sections() :
            print '=== ', section, ' ==='
            for k, v in f.items(section) :
                print k, v
            print '*'*80
    
    from pprint import pprint
    pprint(INSTRUMENTS)