"""
main menu 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


cues={0: "woody",
      3005: "package",
      7832: "factory",
      14327: "marble",
      21441: "woody_logo"}




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

        texMgr.loadTexture("data/intro/woody.png", "woody")
        texMgr.loadTexture("data/intro/package.png", "package")
        texMgr.loadTexture("data/intro/factory.png", "factory")
        texMgr.loadTexture("data/intro/marbles.png", "marbles")
        texMgr.loadTexture("data/intro/logo.png", "woody_logo")
        
        self.timeNow=0
        self.hasStartedMusic=False

    def init(self, unused_args):
        if pygame.mixer:
            pygame.mixer.music.load("data/intro/introSound_1.ogg")

    def draw(self):
        glClearColor(1.0, 1.0, 1.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        if self.timeNow>21441:
            draw_util.draw_quad("woody_logo",   100, 600, 700, 0)
        elif self.timeNow>14327:
            d=self.timeNow-14327
            totalDur=21441-14327
            frac=d/float(totalDur)

            init_off=400-128
            final_off=400-900

            left=init_off+(final_off-init_off)*frac
            
            draw_util.draw_quad("marbles",       left, 428, left+1024, 172)
            
        elif self.timeNow>7832:
            draw_util.draw_quad("factory",       0, 700, 800, -100)            
        elif self.timeNow> 3005:
            draw_util.draw_quad("package",     100, 600, 700, 0)
        else:
            draw_util.draw_quad("woody",       100, 600, 700, 0)            



    def endState(self):
        if pygame.mixer:
            pygame.mixer.music.fadeout(200)
        fsm.popState()
        


    def handleKey(self, key, unicode):
        self.endState()
        return True

    def handleMouseButton(self, bDown, buttonIndex):
        if bDown:
            self.endState()
        return True
        
    def update(self, dt):
        if not pygame.mixer:
            self.timeNow+=dt*1000
            if self.timeNow>30000:
                self.endState()
        else:
            if not self.hasStartedMusic:
                pygame.mixer.music.play()
                self.hasStartedMusic=True
                self.timeNow=0.0
            elif pygame.mixer.music.get_busy():
                self.timeNow=pygame.mixer.music.get_pos()
            else:
                self.endState()
            
        return True
        
        
