"""
level select screen
"""


import sys

from OpenGL.GL import *
from OpenGL.GLU import *
import pygame, pygame.image
from pygame.locals import *

import texMgr
import draw_util
import gui_widgets as gw

import fsm_mgr as fsm
import tubeMgr

import levels

import voiceover
import state_level_intro

def func_play(level_name):
    fsm.pushState(tubeMgr.StateGame(), levels.find_level_by_name(level_name))
    fsm.pushState(state_level_intro.StateIntro(), level_name)
    cn=level_name.split("_")[0]
    if cn=="tutorial":
        cn="tim"
    if cn=="blake":
        cn="tyger"
        
    voiceover.select_character(cn)
    voiceover.select_random_channel()

def func_portrait(name):
    pass

def func_back(name):
    fsm.popState()

bsz=45
bc1=400*2/3.0
bc2=400
bc3=400*4/3.0
bc4=400*5/3.0

pc=400*1/3.0

br1 = 600*5/6.0
br2 = 600*4/6.0
br3 = 600*3/6.0
br4 = 600*2/6.0
br5 = 600*1/6.0

buttons=[gw.Button("b_green",     bc1-bsz, br1+bsz, bc1+bsz, br1-bsz, func_play, "tutorial_1", label="lb_lvl_1"),
         gw.Button("b_green",     bc2-bsz, br1+bsz, bc2+bsz, br1-bsz, func_play, "tutorial_2", label="lb_lvl_2"),
         gw.Button("b_green",     bc3-bsz, br1+bsz, bc3+bsz, br1-bsz, func_play, "tutorial_3", label="lb_lvl_3"),
         gw.Button("b_green",     bc4-bsz, br1+bsz, bc4+bsz, br1-bsz, func_play, "tutorial_4", label="lb_lvl_4"),

         gw.Button("b_blue",      bc1-bsz, br2+bsz, bc1+bsz, br2-bsz, func_play, "blake_1", label="lb_lvl_1"),
         gw.Button("b_blue",      bc2-bsz, br2+bsz, bc2+bsz, br2-bsz, func_play, "blake_2", label="lb_lvl_2"),
         gw.Button("b_blue",      bc3-bsz, br2+bsz, bc3+bsz, br2-bsz, func_play, "blake_3", label="lb_lvl_3"),
         gw.Button("b_blue",      bc4-bsz, br2+bsz, bc4+bsz, br2-bsz, func_play, "blake_4", label="lb_lvl_4"),

         gw.Button("b_yellow",    bc1-bsz, br3+bsz, bc1+bsz, br3-bsz, func_play, "tess_1", label="lb_lvl_1"),
         gw.Button("b_yellow",    bc2-bsz, br3+bsz, bc2+bsz, br3-bsz, func_play, "tess_2", label="lb_lvl_2"),
         gw.Button("b_yellow",    bc3-bsz, br3+bsz, bc3+bsz, br3-bsz, func_play, "tess_3", label="lb_lvl_3"),
         gw.Button("b_yellow",    bc4-bsz, br3+bsz, bc4+bsz, br3-bsz, func_play, "tess_4", label="lb_lvl_4"),

         gw.Button("b_red",       bc1-bsz, br4+bsz, bc1+bsz, br4-bsz, func_play, "woody_1", label="lb_lvl_1"),
         gw.Button("b_red",       bc2-bsz, br4+bsz, bc2+bsz, br4-bsz, func_play, "woody_2", label="lb_lvl_2"),
         gw.Button("b_red",       bc3-bsz, br4+bsz, bc3+bsz, br4-bsz, func_play, "woody_3", label="lb_lvl_3"),
         gw.Button("b_red",       bc4-bsz, br4+bsz, bc4+bsz, br4-bsz, func_play, "woody_4", label="lb_lvl_4"),

         gw.Button("p_tim",       pc-bsz, br1+bsz, pc+bsz, br1-bsz, func_portrait, "woody_1" , label="lab_tim"),
         gw.Button("p_tyger",     pc-bsz, br2+bsz, pc+bsz, br2-bsz, func_portrait, "woody_2" , label="lab_blake"),
         gw.Button("p_tess",      pc-bsz, br3+bsz, pc+bsz, br3-bsz, func_portrait, "woody_3" , label="lab_tess"),
         gw.Button("p_woody",     pc-bsz, br4+bsz, pc+bsz, br4-bsz, func_portrait, "woody_4" , label="lab_woody"),

         gw.Button("b_back",     bc4-bsz, br5+bsz, bc4+bsz, br5-bsz, func_back),

         ]


class StateLevelSelect(fsm.State):
    def __init__(self):
        fsm.State.__init__(self)

    def init(self, unused_args):
        texMgr.loadTexture("data/intro/factory.png", "factory")
        texMgr.loadTexture("data/buttons/square_green.png", "b_green")
        texMgr.loadTexture("data/buttons/square_yellow.png", "b_yellow")
        texMgr.loadTexture("data/buttons/square_blue.png", "b_blue")
        texMgr.loadTexture("data/buttons/square_red.png", "b_red")

        texMgr.loadTexture("data/buttons/label_level_1.png", "lb_lvl_1")
        texMgr.loadTexture("data/buttons/label_level_2.png", "lb_lvl_2")
        texMgr.loadTexture("data/buttons/label_level_3.png", "lb_lvl_3")
        texMgr.loadTexture("data/buttons/label_level_4.png", "lb_lvl_4")

        texMgr.loadTexture("data/buttons/back.png", "b_back")

        texMgr.loadTexture("data/portraits/tutorial_tim.png", "p_tim")
        texMgr.loadTexture("data/portraits/tyger_blake.png",  "p_tyger")
        texMgr.loadTexture("data/portraits/tess_turmoil.png", "p_tess")
        texMgr.loadTexture("data/portraits/woody.png",        "p_woody")

        texMgr.loadTexture("data/portraits/labels/tim.png",    "lab_tim")
        texMgr.loadTexture("data/portraits/labels/blake.png",  "lab_blake")
        texMgr.loadTexture("data/portraits/labels/tess.png",   "lab_tess")
        texMgr.loadTexture("data/portraits/labels/woody.png",  "lab_woody")

    def draw(self):
        glColor3f(0.5, 0.5, 0.5)
        draw_util.draw_quad("factory",       0, 700, 800, -100)
        glColor3f(1.0, 1.0, 1.0)

        for b in buttons:
            b.draw()

    def handleMouseButton(self, bDown, buttonIndex):
        if not bDown:
            return True
        
        for b in buttons:
            if b.handleMouseButton(bDown, buttonIndex):
                return True

        return True
        
    def update(self, dt):
        return True
        
        
