##########################################################################
#	Language.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

# 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 LoadMaxNumber(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the Lang-Count and set it to zero
	LangNumber = obj['LangNumber'] = 0
	
	#--------------------------------------------------------------------
	#Connect to the Database
	con = SQL.connect(mainDir + "Database\Data.db")
	
	cur = con.cursor()
	cur.execute("SELECT * FROM Language")
	
	rows = cur.fetchall()
	
	#--------------------------------------------------------------------
	#Read out each row from Database
	for data in rows:
		#Increase the LangNumber to get the maximum Lang-Count at the end
		obj['LangNumber'] = obj['LangNumber'] + 1
	
	#Close the Database-Connection
	if con:
		con.close()
		
def IDcheck(cont):
	obj = cont.owner
	
	#Get the properties for Language-Selection
	LangID = obj['LangID']			#The current Language-ID
	LangNumber = obj['LangNumber']	#The current Lang-ID
	
	#--------------------------------------------------------------------
	#Check if the selected LangID is possible
	if LangID > LangNumber:
		obj['LangID'] = 1
	elif LangID < 1:
		obj['LangID'] = LangNumber

#Check the Number of Languages for the settings-menu		
def IDcheckS(cont):
	obj = cont.owner
	
	#Get the properties for Language-Selection
	LangID = obj['OP_Language']			#The current Language-ID
	LangNumber = obj['LangNumber']		#The current Lang-ID
	
	#--------------------------------------------------------------------
	#Check if the selected LangID is possible
	if LangID > LangNumber:
		obj['OP_Language'] = 1
	elif LangID < 1:
		obj['OP_Language'] = LangNumber
		
def Menu_Drive(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	#Define the Text-Objects
	startOBJ = objList["Start_Drive_Text"]
	menuOBJ = objList["Menu_Drive_Text"]
	
	#--------------------------------------------------------------------
	#Open the GlobalDict
	g_Lang = logic.globalDict["Language"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	startOBJ['Text'] = g_Lang["T_Drive"]
	menuOBJ['Text'] = g_Lang["T_Menu"]

def Garage(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	#Define the Text-Objects
	menuOBJ = objList["Menu_Drive_Text_4.2"]
	buyOBJ = objList["Buy_Text"]
	sellOBJ = objList["Sell_Text"]
	carOBJ = objList["Car_Text"]
	
	#--------------------------------------------------------------------
	#Open the GlobalDict
	g_Lang = logic.globalDict["Language"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	menuOBJ['Text'] = g_Lang["T_Menu"]
	buyOBJ['Text'] = g_Lang["T_Buy"]
	sellOBJ['Text'] = g_Lang["T_Sell"]
	carOBJ['Text'] = g_Lang["T_Car"]

def Menu(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	#Define the Text-Objects
	settingsOBJ = objList["Text_Settings"]
	exitOBJ = objList["Text_Exit"]
	userOBJ = objList["Text_User"]
	startOBJ = objList["Text_StartGame"]
	
	#--------------------------------------------------------------------
	#Open the GlobalDict
	g_Lang = logic.globalDict["Language"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	settingsOBJ['Text'] = g_Lang["T_Settings"]
	exitOBJ['Text'] = g_Lang["T_Exit"]
	userOBJ['Text'] = g_Lang["T_User"]
	startOBJ['Text'] = g_Lang["T_Start"]
	
def Loadscreen(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	#Define the Text-Objects
	loadingOBJ = objList["Loading_Text"]
	createOBJ = objList["CreateWorld_Text"]
	
	#--------------------------------------------------------------------
	#Open the GlobalDict
	g_Lang = logic.globalDict["Language"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	loadingOBJ['Text'] = g_Lang["T_Loading"] + "..."
	createOBJ['Text'] = g_Lang["T_Creating"] + " " + g_Lang["T_the"] + " " + g_Lang["T_World"] + "..."
	
def HUD2(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	#Define the Text-Objects
	positionOBJ = objList["Position_Text"]
	lapsOBJ = objList["Laps_Text"]
	
	#--------------------------------------------------------------------
	#Open the GlobalDict
	g_Lang = logic.globalDict["Language"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	positionOBJ['Text'] = g_Lang["T_Position"]
	lapsOBJ['Text'] = g_Lang["T_Laps"]
  
def Settings(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	
	#--------------------------------------------------------------------
	#Define the Text-Objects
	menuOBJ = objList["Menu_Settings_Text"]
	smokeOBJ = objList["Smoke_Settings_Text"]
	musicOBJ = objList["Music_Settings_Text"]
	notificationsOBJ = objList["Notification_Settings_Text"]
	languageOBJ = objList["Language_Settings_Text"]
	lightOBJ = objList["Lights_Settings_Text"]
	
	#--------------------------------------------------------------------
	#Open the GlobalDict
	g_Lang = logic.globalDict["Language"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	menuOBJ['Text'] = g_Lang["T_Menu"] + " " + g_Lang["T_Save"]
	smokeOBJ['Text'] = g_Lang["T_Smoke"]
	musicOBJ['Text'] = g_Lang["T_Music"]
	notificationsOBJ['Text'] = g_Lang["T_Notifications"]
	languageOBJ['Text'] = g_Lang["T_Language"] + ":"
	lightOBJ['Text'] = "Adv. " + g_Lang["T_Light"]
   
def Load(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the properties for Language-Selection
	LangID = obj['LangID']			#The current Language-ID
	Module = obj['Module']

	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database\Data.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Language-ID"
	cur.execute("SELECT * FROM Language WHERE LangID=:Id", {"Id": LangID})
	con.commit()

	data = cur.fetchone()

	#--------------------------------------------------------------------
	#Define and save the loaded Database-row-Data
	global_Language = {"T_LangName" : data[1],
						"T_Exit" : data[2],
						"T_Save" : data[3],
						"T_Drive" : data[4],
						"T_Garage" : data[5],
						"T_Back" : data[6],
						"T_Settings" : data[7],
						"T_Stats" : data[8],
						"T_Menu" : data[9],
						"T_Start" : data[10],
						"T_Tuning" : data[11],
						"T_Colors" : data[12],
						"T_Tires" : data[13],
						"T_Body" : data[14],
						"T_Performance" : data[15],
						"T_PS" : data[16],
						"T_KmH" : data[17],
						"T_KG" : data[18],
						"T_Buy" : data[19],
						"T_Sell" : data[20],
						"T_Car" : data[21],
						"T_Position" : data[22],
						"T_Laps" : data[23],
						"T_Price" : data[24],
						"T_Loading" : data[25],
						"T_Creating" : data[26],
						"T_the" : data[27],
						"T_World" : data[28],
						"T_Smoke" : data[29],
						"T_Music" : data[30],
						"T_Notifications" : data[31],
						"T_Language" : data[32],
						"T_Light" : data[33],
						"T_User" : data[34]
						
						}
	
	logic.globalDict["Language"] = global_Language
	
	logic.sendMessage("Init.Text")

	#Close the Database-Connection
	if con:
		con.close()
	
	if Debug:
		print(data[1] + " Loaded")
	
	if Module == "HUD2":
		HUD2(cont)
	if Module == "Loadscreen":
		Loadscreen(cont)
	if Module == "Menu":
		Menu(cont)
	if Module == "Settings":
		Settings(cont)
	if Module == "Settings_Lang":
		Settings_Lang(cont)
	if Module == "Garage":
		Garage(cont)
	#--------------------------------------------------------------------
def Settings_Lang(cont):
	scene = logic.getCurrentScene()
	objList = scene.objects
	obj = cont.owner
	
	#--------------------------------------------------------------------
	#Get the properties for Language-Selection
	LangID = obj['OP_Language']			#The current Language-ID

	#--------------------------------------------------------------------	
	#Connect to the Database
	con = SQL.connect(mainDir + "Database\Data.db")
	
	cur = con.cursor()
	#Load the Database-row with the choosen "Language-ID"
	cur.execute("SELECT * FROM Language WHERE LangID=:Id", {"Id": LangID})
	con.commit()

	data = cur.fetchone()

	#--------------------------------------------------------------------
	#Define the Text-Objects
	langOBJ = objList["Lang_Settings_Text"]
	
	#--------------------------------------------------------------------
	#Change the Text-Property of each object
	langOBJ['Text'] = data[1]

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

	
	