##########################################################################
#	Cloth.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 GameLogic
import Rasterizer

import Settings
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 LibLoadClothes():
	logic.LibLoad(mainDir + "Objects/Actor/Kleidungen.blend", "Mesh", load_actions=True, verbose=Debug, load_scripts=True)
	
def Load(cont):
	obj = cont.owner
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	Choose = obj['Choose']
	
	#--------------------------------------------------------------------
	if Choose:
		PropOBJ = objList["Torso_Choose"]
	else:
		PropOBJ = obj
	#--------------------------------------------------------------------
	
	Settings.ReadLastUserFile(cont)
	UserID = obj['UserID']
	
	#--------------------------------------------------------------------
	#Connect to the Database User/Actor_Design
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "User-ID"
	cur.execute("SELECT * FROM Actor_Design WHERE UserID=:Uid", {"Uid":UserID})
	con.commit()

	data = cur.fetchone()
	
	Hut = data[1]
	Kopf = data[2]
	Torso = data[3] 
	Huefte = data[4] 
	Hand_R = data[5] 
	Hand_L = data[6] 
	Arm_R = data[7] 
	Arm_L = data[8] 
	Bein_R = data[9] 
	Bein_L = data[10] 
	
	#Close the Database-Connection
	if con:
		con.close()
	
	PropOBJ['Hat_ID'] = Hut
	PropOBJ['Head_ID'] = Kopf
	PropOBJ['Torso_ID'] = Torso
	PropOBJ['ArmR_ID'] = Arm_R
	PropOBJ['ArmL_ID'] = Arm_L
	PropOBJ['Hip_ID'] = Huefte
	PropOBJ['LegR_ID'] = Bein_R
	PropOBJ['LegL_ID'] = Bein_L
	PropOBJ['HandR_ID'] = Hand_R
	PropOBJ['HandL_ID'] = Hand_L
	
def Save(cont):
	obj = cont.owner
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	PropOBJ = objList["Torso_Choose"]
	
	Hat_ID = PropOBJ['Hat_ID']
	Head_ID = PropOBJ['Head_ID']
	Torso_ID = PropOBJ['Torso_ID']
	ArmR_ID = PropOBJ['ArmR_ID']
	ArmL_ID = PropOBJ['ArmL_ID']
	Hip_ID = PropOBJ['Hip_ID']
	LegR_ID = PropOBJ['LegR_ID']
	LegL_ID = PropOBJ['LegL_ID']
	HandR_ID = PropOBJ['HandR_ID']
	HandL_ID = PropOBJ['HandL_ID']
	#--------------------------------------------------------------------
	
	Settings.ReadLastUserFile(cont)
	UserID = obj['UserID']
	
	#--------------------------------------------------------------------
	#Connect to the Database User/Actor_Design
	con = SQL.connect(mainDir + "Database/User.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "User-ID"
	#cur.execute("UPDATE Actor_Design SET Hut =?, Kopf =?, Torso =?, Huefte =?, Hand_R =?, Hand_L =?, Arm_R =?, Arm_L =?, Bein_R =?, Bein_L =? WHERE UserID=?", (Hat_ID, Head_ID, Torso_ID, Hip_ID, HandR_ID, HandL_ID, ArmR_ID, ArmL_ID, LegR_ID, LegL_ID, UserID))
	cur.execute("INSERT INTO Actor_Design (UserID, Hut, Kopf, Torso, Huefte, Hand_R, Hand_L, Arm_R, Arm_L, Bein_R, Bein_L) VALUES(?,?,?,?,?,?,?,?,?,?,?)", (UserID, Hat_ID, Head_ID, Torso_ID, Hip_ID, HandR_ID, HandL_ID, ArmR_ID, ArmL_ID, LegR_ID, LegL_ID))
	#cur.execute("INSERT INTO BoughtCars (UserID, AutoID, Bought) VALUES(?, ?, 0)", (UserID, AutoID))
		
	con.commit()

	#Close the Database-Connection
	if con:
		con.close()

def LoadBodyPartNumber(part):
	count = 0
	#--------------------------------------------------------------------
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/Actor.db")
	
	cur = con.cursor()
	
	if part == "Arm_L":
		cur.execute("SELECT * FROM Arm_L")
	if part == "Arm_R":
		cur.execute("SELECT * FROM Arm_R")
	if part == "Bein_L":
		cur.execute("SELECT * FROM Bein_L")	
	if part == "Bein_R":
		cur.execute("SELECT * FROM Bein_R")
	if part == "Hand_L":
		cur.execute("SELECT * FROM Hand_L")
	if part == "Hand_R":
		cur.execute("SELECT * FROM Hand_R")	
	if part == "Huefte":
		cur.execute("SELECT * FROM Huefte")
	if part == "Hut":
		cur.execute("SELECT * FROM Hut")
	if part == "Kopf":
		cur.execute("SELECT * FROM Kopf")
	if part == "Torso":
		cur.execute("SELECT * FROM Torso")	
		
	rows = cur.fetchall()
	
	#--------------------------------------------------------------------
	#Read out each row from Database
	for data in rows:
		#Increase the UserNumber to get the maximum Count at the end
		count = count + 1
	
	#Close the Database-Connection
	if con:
		con.close()

	return count	
	
def correctCounts(cont):
	obj = cont.owner
	
	Hat_ID = obj['Hat_ID']
	Head_ID = obj['Head_ID']
	Torso_ID = obj['Torso_ID']
	ArmR_ID = obj['ArmR_ID']
	ArmL_ID = obj['ArmL_ID']
	Hip_ID = obj['Hip_ID']
	LegR_ID = obj['LegR_ID']
	LegL_ID = obj['LegL_ID']
	HandR_ID = obj['HandR_ID']
	HandL_ID = obj['HandL_ID']
	
	if Hat_ID < 1:
		obj['Hat_ID'] = LoadBodyPartNumber("Hut")
	elif Hat_ID > LoadBodyPartNumber("Hut"):
		obj['Hat_ID'] = 1
	
	if Head_ID < 1:
		obj['Head_ID'] = LoadBodyPartNumber("Kopf")
	elif Head_ID > LoadBodyPartNumber("Kopf"):
		obj['Head_ID'] = 1
	
	if Torso_ID < 1:
		obj['Torso_ID'] = LoadBodyPartNumber("Torso")
	elif Torso_ID > LoadBodyPartNumber("Torso"):
		obj['Torso_ID'] = 1
	
	if ArmR_ID < 1:
		obj['ArmR_ID'] = LoadBodyPartNumber("Arm_R")
	elif ArmR_ID > LoadBodyPartNumber("Arm_R"):
		obj['ArmR_ID'] = 1	
	
	if ArmL_ID < 1:
		obj['ArmL_ID'] = LoadBodyPartNumber("Arm_L")
	elif ArmL_ID > LoadBodyPartNumber("Arm_L"):
		obj['ArmL_ID'] = 1
	
	if Hip_ID < 1:
		obj['Hip_ID'] = LoadBodyPartNumber("Huefte")
	elif Hip_ID > LoadBodyPartNumber("Huefte"):
		obj['Hip_ID'] = 1
		
	if LegR_ID < 1:
		obj['LegR_ID'] = LoadBodyPartNumber("Bein_R")
	elif LegR_ID > LoadBodyPartNumber("Bein_R"):
		obj['LegR_ID'] = 1	
		
	if LegL_ID < 1:
		obj['LegL_ID'] = LoadBodyPartNumber("Bein_L")
	elif LegL_ID > LoadBodyPartNumber("Bein_L"):
		obj['LegL_ID'] = 1
		
	if HandR_ID < 1:
		obj['HandR_ID'] = LoadBodyPartNumber("Hand_R")
	elif HandR_ID > LoadBodyPartNumber("Hand_R"):
		obj['HandR_ID'] = 1
		
	if HandL_ID < 1:
		obj['HandL_ID'] = LoadBodyPartNumber("Hand_L")
	elif HandL_ID > LoadBodyPartNumber("Hand_L"):
		obj['HandL_ID'] = 1
		
def ReplacePart(part):	
	cont = logic.getCurrentController()
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	Choose = obj['Choose']
	
	if Choose:
		#Choose-Body
		HAT = objList["Hut_Choose"]
		HEAD = objList["Kopf_Choose"]
		TORSO = objList["Torso_Choose"]
		ARMR = objList["ArmR_Choose"]
		ARML = objList["ArmL_Choose"]
		HIP = objList["Huefte_Choose"]
		LEGR = objList["Bein_R_Choose"]
		LEGL = objList["Bein_L_Choose"]
		HANDR = objList["HandR_Choose"]
		HANDL = objList["HandL_Choose"]
		if Debug == 1:
			print("Choose-Body-Clothes active")
		
	else:
		#Game-Body
		HAT = objList["Hut"]
		HEAD = objList["Kopf"]
		TORSO = objList["Torso"]
		ARMR = objList["ArmR"]
		ARML = objList["ArmL"]
		HIP = objList["Huefte"]
		LEGR = objList["Bein_R"]
		LEGL = objList["Bein_L"]
		HANDR = objList["HandR"]
		HANDL = objList["HandL"]
		if Debug == 1:	
			print("Game-Body-Clothes active")	
	
	#Koerper-ID's
	HatID = obj['Hat_ID']
	HeadID = obj['Head_ID']
	TorsoID = obj['Torso_ID']
	ArmRID = obj['ArmR_ID']
	ArmLID = obj['ArmL_ID']
	HipID = obj['Hip_ID']
	LegRID = obj['LegR_ID']
	LegLID = obj['LegL_ID']
	HandRID = obj['HandR_ID']
	HandLID = obj['HandL_ID']
	
	#--------------------------------------------------------------------
	#Connect to the Database
	con = SQL.connect(mainDir + "Database/Actor.db")
	
	cur = con.cursor()
	
	if part == "Arm_L":
		cur.execute("SELECT * FROM Arm_L WHERE ID=:Id", {"Id": ArmLID})
	if part == "Arm_R":
		cur.execute("SELECT * FROM Arm_R WHERE ID=:Id", {"Id": ArmRID})
	if part == "Bein_L":
		cur.execute("SELECT * FROM Bein_L WHERE ID=:Id", {"Id": LegLID})	
	if part == "Bein_R":
		cur.execute("SELECT * FROM Bein_R WHERE ID=:Id", {"Id": LegRID})
	if part == "Hand_L":
		cur.execute("SELECT * FROM Hand_L WHERE ID=:Id", {"Id": HandLID})
	if part == "Hand_R":
		cur.execute("SELECT * FROM Hand_R WHERE ID=:Id", {"Id": HandRID})	
	if part == "Huefte":
		cur.execute("SELECT * FROM Huefte WHERE ID=:Id", {"Id": HipID})
	if part == "Hut":
		cur.execute("SELECT * FROM Hut WHERE ID=:Id", {"Id": HatID})
	if part == "Kopf":
		cur.execute("SELECT * FROM Kopf WHERE ID=:Id", {"Id": HeadID})
	if part == "Torso":
		cur.execute("SELECT * FROM Torso WHERE ID=:Id", {"Id": TorsoID})	
	
	data = cur.fetchone()
	
	name = data[1]
	
	#Close the Database-Connection
	if con:
		con.close()
	
	if part == "Arm_L":
		ARML.replaceMesh(name, True, True)
	if part == "Arm_R":
		ARMR.replaceMesh(name, True, True)
	if part == "Bein_L":
		LEGL.replaceMesh(name, True, True)	
	if part == "Bein_R":
		LEGR.replaceMesh(name, True, True)
	if part == "Hand_L":
		HANDL.replaceMesh(name, True, True)
	if part == "Hand_R":
		HANDR.replaceMesh(name, True, True)
	if part == "Huefte":
		HIP.replaceMesh(name, True, True)
	if part == "Hut":
		HAT.replaceMesh(name, True, True)
	if part == "Kopf":
		HEAD.replaceMesh(name, True, True)
	if part == "Torso":
		TORSO.replaceMesh(name, True, True)

def Replace():
	ReplacePart("Arm_L")
	ReplacePart("Arm_R")
	ReplacePart("Bein_L")
	ReplacePart("Bein_R")
	ReplacePart("Hand_L")
	ReplacePart("Hand_R")
	ReplacePart("Huefte")
	ReplacePart("Hut")
	ReplacePart("Kopf")
	ReplacePart("Torso")
	
def test():
	print(logic.getCurrentScene().objects)