# -*- coding: utf-8 -*-

#System
import sys

#Panda3D
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
from direct.gui.OnscreenText import OnscreenText
from direct.gui.OnscreenImage import OnscreenImage
from pandac.PandaModules import VBase4
from pandac.PandaModules import * 
from pandac.PandaModules import ActorNode, CollisionHandlerEvent, CollisionHandlerGravity, CollisionHandlerPusher, CollisionNode, CollisionSphere, CollisionTraverser, BitMask32, CollisionRay, NodePath

#Ludum
from player import Player
import controls
from level import *
from translation import *




loadPrcFile("config.prc")

class LudumGame(ShowBase):

    def __init__(self,god=False):
        setLanguage = 'en'
        ShowBase.__init__(self)
        base.disableMouse()
        self.lens2D = OrthographicLens()
        self.lens2D.setFilmSize(1024/80.0, 768/80.0)  # Or whatever is appropriate for your scene

        self.lens3D = base.cam.node().getLens()
        self.lens3D.setFilmSize(1024/80.0, 768/80.0)
        
        self.win.setClearColor(VBase4(0.0,0.0,0.0,1.0))
        self.god = god
        render.setShaderAuto()

        base.cTrav=CollisionTraverser()
        base.cTrav.setRespectPrevTransform(True)

        self.wall_handler = CollisionHandlerPusher()

        self.floor_handler = CollisionHandlerGravity()
        self.floor_handler.setGravity(9.81+25)
        self.floor_handler.setMaxVelocity(100)

        self.collisionEvent = CollisionHandlerEvent()
        self.collisionEvent.addInPattern('%fn-into')
        self.collisionEvent.addOutPattern('%fn-out')

        #--- Initialisation des controls ----------------

        self.player = None
        self.spawnPlayer()

        self.level = None
        self.curlevel = 1
        self.spawnLevel(self.curlevel)

        taskMgr.add(self.checkForDeath, "checkfordeath")


    def spawnPlayer(self):
        if self.player is not None:
            self.player.node_gui.removeNode()
            self.player.rootnp.removeNode()
            taskMgr.removeTasksMatching("update action")



        self.player = Player(self.floor_handler,god=self.god)
        self.player.rootnp.reparentTo(render)

        self.player.rootnp.setPos(2,0,1)


        controls.init(self.player)

        #chargement du niveau
        #node = base.loader.loadModel('models/test')
        #base.render.reparentTo(node)

        self.wall_handler.addCollider(self.player.wallcollisionnp_top, self.player.rootnp)
        self.wall_handler.addCollider(self.player.wallcollisionnp_btm, self.player.rootnp)

        base.cTrav.addCollider(self.player.ray, self.floor_handler)
        base.cTrav.addCollider(self.player.wallcollisionnp_top, self.wall_handler)
        base.cTrav.addCollider(self.player.wallcollisionnp_btm, self.wall_handler)

        base.cTrav.addCollider(self.player.sensor, self.collisionEvent)

        self.accept('playersensor-into', self.player.sensorIn)


    def spawnLevel(self, number):
        if self.level is not None:
            self.level.rootnp.removeNode()
            taskMgr.removeTasksMatching("check reach")
        if number == 1:
            base.cam.node().setLens(self.lens2D)
            self.level = Level1("level1", self.player)
        elif number == 2:
            base.cam.node().setLens(self.lens2D)
            self.level = Level2("level2", self.player)
        elif number == 3:
            base.cam.node().setLens(self.lens3D)
            self.level = Level3("level3", self.player) 
        elif number == 4:
            base.cam.node().setLens(self.lens3D)
            self.level = Level4("level4/Decor", self.player)            
        elif number == 5:
            base.cam.node().setLens(self.lens2D)
            self.level = Level5("level5", self.player) 

        self.player.node_gui.reparentTo(base.cam)
        self.level.rootnp.reparentTo(render)

    def checkForDeath(self, task):
        if self.player.sprite.currentFrame == 21:
            print "RESPAWN"
            self.spawnPlayer()
            self.spawnLevel(self.curlevel)

        if self.player.rootnp.getX() > self.player.limit + 5:
            self.spawnPlayer()
            self.curlevel+=1
            self.player.curspeed = 0.
            self.spawnLevel(self.curlevel)
            self.player.rootnp.setX(3)

        return task.cont


if __name__ == "__main__":

	# --- For debug only ---
    god = False
    if len(sys.argv) > 1:
        if sys.argv[1] == "--debug" or sys.argv[1] == "-d":
            from panda3d.core import loadPrcFileData
            loadPrcFileData("", "want-directtools #t")
            loadPrcFileData("", "want-tk #t")
        if sys.argv[1] == "--god" or sys.argv[1] == "-g":
            god = True
	# --- end ---
    loadTranslation('translation/traduction.csv')
    checkTranslation()
    app = LudumGame(god)
    app.run()
    


