import direct.directbase.DirectStart
from pandac.PandaModules import AmbientLight,DirectionalLight
from pandac.PandaModules import LightAttrib
from pandac.PandaModules import Vec3,Vec4
from direct.actor.Actor import Actor
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.interval.MetaInterval import Sequence,Parallel
from direct.interval.FunctionInterval import Wait,Func
from direct.gui.DirectButton import DirectButton
import sys,os

class World(DirectObject):
  def __init__(self):
    base.disableMouse()
    base.setBackgroundColor(0,0,0)
    self.accept('escape', sys.exit)
    camera.setPos(-5.4, -16.5, -1.8)
    
    self.card = Actor("models/thankyou_card",
                            {"main": "models/thankyou_anim"})
    self.card.reparentTo(render)
    self.card.pose("main", 0)
    self.card.action = self.card.actorInterval("main")

    self.setupLights()
    self.music = loader.loadMusic('sound/soundtrack.mp3')

    seq = Sequence(Wait(5), Func(self.music.play), self.card.action)
    if sys.platform == "win32":
      #self.card.find("**/pPlane11").setTexture(
      #  loader.loadTexture("models/textures/pandasign_win.jpg"), 1)
      seq = Parallel(seq,
                     Sequence(Wait(40), 
                              Func(self.createButton)))
     
    seq.start()

  def createButton(self):
    self.button = DirectButton(
                   pad = (1.333, 1), frameColor = (.5,.5,.5,0),
                   pos = Vec3(0, 0, 0),
                   rolloverSound = None, clickSound = None,
                   command = self.openTutorialDirWin32)
    #self.button.setTransparency(1)

  def setButtonFrameColor(self, color):
    self.button["frameColor"] = color

  def openTutorialDirWin32(self):
    Sequence(Func(self.setButtonFrameColor, (.5,.5,.5,.5)),
             Wait(.25),
             Func(self.setButtonFrameColor, (1,1,0,0))).start()

    if (len(sys.argv) > 1):
      path = sys.argv[1]
    else:
      path = os.path.join(os.path.abspath(sys.path[0]),"..")
    os.startfile(path)

  def setupLights(self):
    lAttrib = LightAttrib.makeAllOff()
    ambientLight = AmbientLight( "ambientLight" )
    ambientLight.setColor( Vec4(.8, .8, .75, 1) )
    lAttrib = lAttrib.addLight( ambientLight )
    directionalLight = DirectionalLight( "directionalLight" )
    directionalLight.setDirection( Vec3( 0, .8944, -.447 ) )
    directionalLight.setColor( Vec4( 0.9, 0.8, 0.9, 1 ) )
    lAttrib = lAttrib.addLight( directionalLight )
    render.attachNewNode( directionalLight.upcastToPandaNode() )
    render.attachNewNode( ambientLight.upcastToPandaNode() )
    render.node().setAttrib( lAttrib )

w = World()
run()
                            

