import modules.User as User
import modules.Statics as Statics
import Controls

from math import pi, sin, cos
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from panda3d.core import Point3
from panda3d.core import WindowProperties
from panda3d.core import Loader
from direct.showbase.ShowBase import ShowBase #Grundmodul zum Starten eines Fenster mit Grundszene
from direct.gui.OnscreenText import OnscreenText #Ermöglicht die direkte Einblendung von Texten
from direct.gui.DirectGui import * #Ermöglicht das Einblenden von Menüs und deren Manipulation
from panda3d.core import TextNode #Texte
from direct.task import Task
from panda3d.core import Vec4, Vec3
from panda3d.core import loadPrcFile #Ermöglicht das Laden eigener Konfigurationsdatein zur Panda3D API
import random
	
class game(ShowBase):
    __debug_mode = [0,1,2,3,4]
    __debuglevel = 0
    __us = User.User
    __statics = Statics.statics
    #__ctrl = Controls.controls
#    __game_mechanics = Game_Mechanics.Game_Mechanics()
    __onscreentext1 = OnscreenText
    __actor1 = 0
    __p3d_conf = loadPrcFile("conf/conf.prc") #Pfadangabe zum .prc File

    __keymap = {"up":False,
                "down":False,
                "left":False,
                "right":False
                }

            
    def __init__(self) -> None:
        ShowBase.__init__(self)
        self.__us = User.User()
        self.__statics = Statics.statics()
        #self.__ctrl = Controls.controls(self)
        
        #font1 = self.loader.load_font("/usr/share/fonts/TTF/Inconsolata-UltraExpandedExtraBold.ttf")
        font1 = self.loader.load_font("data/fonts/Inconsolata-UltraExpandedExtraBold.ttf")
        self.__onscreentext1 = OnscreenText(self.__statics._menupoints[0], font=font1, pos= (0.1,0))   
        self.__onscreentext1.setText("Ich bewege mich fortlaufen!")  

        #self.actor1 = Actor("data/models/myfile.bam")
        self.__actor1 = self.loader.load_model("data/models/myfile.bam")
        self.__actor1.getPos()
        self.__actor1.reparentTo(self.render)

        self.accept('arrow_up', self.update_keystate, ["up", True])
        self.accept('arrow_up-up', self.update_keystate, ["up", False])        
        self.accept('arrow_down', self.update_keystate, ["down", True])
        self.accept('arrow_down-up', self.update_keystate, ["down", False])        
        self.accept('arrow_left', self.update_keystate, ["left", True])
        self.accept('arrow_left-up', self.update_keystate, ["left", False])        
        self.accept('arrow_right', self.update_keystate, ["right", True])
        self.accept('arrow_right-up', self.update_keystate, ["right", False])

        self.taskMgr.add(self.task1, "task1")
        self.taskMgr.add(self.keyboard_event, "keyboard_event")

    #Zusatzfunktionen innerhalb instanzierter ShowBase-Klasse
    def update_keystate(self, controlName, controlState):
        self.__keymap[controlName] = controlState

    def setDebugMode(self, debugmode:int):
        """DEBUGLEVEL"""
        self.__debuglevel = debugmode

    
    #Taskfunctions and definitions
    def task1(self, task):
        _x = -2.8 + (task.time*0.3)        
        #print("Task läuft seit: " + str(_x))
        #print(str(self.__actor1.getPos()))
        #self.__actor1.setPos(1*_x,1*_x,0)

        if (_x >= 2.8000):
            print("Finished")
            return Task.again
        return Task.cont
    
    def keyboard_event(self, task):

        if(self.__debuglevel == 4): print(self.__keymap)
        
        if (self.__keymap['up']):
            self.__actor1.setPos(self.__actor1.getPos() + Vec3(0,0.2,0))
            if(self.__debuglevel >= 3): print("Nach oben!")
        if (self.__keymap['down']):
            self.__actor1.setPos(self.__actor1.getPos() - Vec3(0,0.2,0))
            if(self.__debuglevel >= 3): print("Nach unten!")
        if (self.__keymap['left']):
            self.__actor1.setPos(self.__actor1.getPos() - Vec3(0.2,0,0))
            if(self.__debuglevel >= 3): print("Nach links!")
        if (self.__keymap['right']):
            self.__actor1.setPos(self.__actor1.getPos() + Vec3(0.2,0,0))
            if(self.__debuglevel >= 3): print("Nach rechts!")       

        return Task.cont