##########################################################################
#	Car.py 	Blender 2.66a
#
#	Created by Florian Voelker (iFlowProduction)
#	Games, Downloads and co. at http://iflowproduction.de
#
#	Released under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0).
#	http://creativecommons.org/licenses/by-nc-sa/3.0/
#	
#	Feel free to change it for your own application(s), but leave this 
#	Header and inform others about the license.
#	If you use this code, please include this information header and don't change the license.
#
#	If you have any questions, please contact me.
#
#	Version 1.0
##########################################################################

from bge import logic
from bge import events

import sqlite3 as SQL
import sys
import GameLogic
import Rasterizer
import mathutils

#import Cloth
import System

# get the path to the blend directory
mainDir = GameLogic.expandPath("//")

#Activate/Deactivate the Debug and Error-visibility (1 = True/ 0 = False)
Debug = 0
Errors = 1

def onePositive(cont):
  for Keyboard in cont.sensors:
    if Keyboard.positive:
      return True

def replaceActor(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#Sitting = objList["Player_Sitting"]
	
	obj.replaceMesh("PlayerSitting",True, False)
	
def createID(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	objName = obj.name
	
	system = objList["MainSystem"]
	curID = system["VehicleID"]
	
	newID = curID + 1
	
	obj["VehicleID"] = newID
	system["VehicleID"] = newID
	
	obj.state = 2
	
def addCar(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	if onePositive(cont):
		return
	
	act = cont.actuators["Add"]

	act.instantAddObject()
	last = act.objectLastCreated
	#print(last)

def LoadReadCar(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the properties for Car-Selection
	CarID = obj['CarID']
	
	if onePositive(cont):
		return
	
	#--------------------------------------------------------------------
	#Connect to the Database
	con = SQL.connect(mainDir + "Database\Car.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Actor-ID"
	cur.execute("SELECT * FROM Car WHERE CarID=:Id", {"Id": CarID})
	con.commit()

	data = cur.fetchone()

	#--------------------------------------------------------------------
	#Define and save the loaded Database-row-Data
	CarName = data[1]
	File = data[2]
	
	#Close the Database-Connection
	if con:
		con.close()
	
	if Debug:
		print("")
		print(":---------Car_Start---------:")
		print(File + " Loaded")
		
	#Load the Library
	try:
		logic.LibLoad(mainDir + "Objects/Cars/" + ('/') + (File) + ".blend", "Scene", load_actions=True, verbose=Debug, load_scripts=True)
	except:
		if Errors:
			print("Error: " + File + " failed to Load! Maybe already loaded.")
	
	if Debug:
		print(File)
		print("----------Car_End----------")
		print("")
	
	
	# get actuator "Add" for adding an object
	spawn = cont.actuators["Add_Car"]
		
	spawn.object = CarName
	spawn.instantAddObject()
	
def changeCam(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	PosOBJ = obj.position
	
	#Actor_Name = "Actor_Mesh"
	#VehicleID = obj["VehicleID"]
	Active_Pos = objList["Active_Pos"]
	
	Active_Pos["Pos_Name"] = obj.name
	
	Active_Pos.position = PosOBJ
	
	System.end_Actor()
	
	