/*
	[EX] PostNuke Wasteland Autohaus System
	@extending <pnwl_cars.inc>
	
	Data: 09.11.2011
	Author: NeoPhoenix
*/

#if defined pnwl_EX_autohaus_included
	#endinput
#else
	#define pnwl_EX_autohaus_included
#endif

/*
	native autohaus_EX_Abkling(playerid);
	native autohaus_EX_OnPlayerUpdate(playerid,ud,lr);
*/

//Defines
//@basic information
#define EX_MAX_AUTOHAUS_CARS	(2)
#define EX_MAX_AUTOHOUSES		(2)
#define EX_MAX_WATCH_POS		(4)

enum (+=1)
{
	dialog_EX_car_MAINRE = 30,
	dialog_EX_car_MAIN,
	dialog_EX_car_INFO_BUY,
	dialog_EX_car_BUY,
	dialog_EX_car_LEAVE,
	dialog_EX_car_BUY_SUC,
	dialog_EX_car_BUY_FAIL,
	dialog_EX_car_INFO,
}

//Structures
enum e_a_house
{
	model,
	carname[50],
	preis
}

//Data
new const AutohausInfo[EX_MAX_AUTOHAUS_CARS][e_a_house] = {
{604,"Glendale",25000},
{605,"Sadler",	25000}
};
new EX_car[MAX_PLAYERS] = {-1,...},
	bool:EX_inuse[EX_MAX_AUTOHOUSES];

new const Float:AutohausBotPos[EX_MAX_AUTOHOUSES][4] = {
{59.5830,1214.7313,18.8433,261.5},
{59.5830,1214.7313,18.8433,261.5}
};	
	
new const Float:AutohausShowVehPos[EX_MAX_AUTOHOUSES][4] = {
{67.26536560,1231.10913086,-16.12109756,90.0},
{67.26536560,1231.10913086,-16.12109756,90.0}
};
//Functions
autohaus_EX_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	#pragma unused inputtext
	switch(dialogid)
	{
		case dialog_NULL: return 1;
		case dialog_EX_car_LEAVE:
		{
			if(!response)
			{//Verlassen
				autohaus_ResetToJason(playerid);
				autohaus_OnLeavePlayerAH(playerid);
				return 1;
			}
			//Weitershoppen
			return 1;
		}
		case dialog_EX_car_INFO:
		{
			autohaus_DisplayMainDialog(playerid);
			return 1;
		}
		case dialog_EX_car_MAIN:
		{//"Hauptmenü" beim Autohändler
			if(!response) listitem = 2;//Gespräch beenden
			switch(listitem)
			{//0 = informationen, 1 = autos ansehen, 2 = gespräch verlassen
				case 0:
				{
					car::ShowPlayerDialog(playerid,dialog_EX_car_INFO,DIALOG_STYLE_MSGBOX,"{C87E35}Gespräch mit Jason","{F1F7DC}Jo, hier kannst du Autos kaufen oder deinen Wagen reparieren bzw\ntunen lassen. Um ne Karre zu kaufen brauchst du ein paar hübsche Items für mich und\nAutoersatzteile damit ich dir einen fahrbaren Untersatz bauen kann...","Zurück","");
					return 1;
				}
				case 1:
				{
					new ah = GetPVarInt(playerid,"EX_autohaus_nr");
					if(EX_inuse[ah] == true)
					{
						autohaus_DisplayMainDialog(playerid);
						return 1;
					}
					TogglePlayerControllable(playerid,			false);
					SetPVarInt(playerid,"EX_autohaus_showing",	1);
					SetPVarInt(playerid,"EX_autohaus_car",		0);
					SetPVarInt(playerid,"EX_autohaus_watchpos",	0);
					SetPVarInt(playerid,"EX_autohaus_in",		1);
					EX_inuse[ah] = true;
					
					EX_car[playerid] = car::CreateVehicle(AutohausInfo[0][model],AutohausShowVehPos[ah][0],AutohausShowVehPos[ah][1],AutohausShowVehPos[ah][2],AutohausShowVehPos[ah][3], -1, -1, -1, false, true, false);
					switch(ah)
					{
						case 0: SetPlayerCameraPos(playerid,62.06022644,1231.08569336,-13.85814476);
						default: SetPlayerCameraPos(playerid,62.06022644,1231.08569336,-13.85814476);
					}
					SetPlayerCameraLookAt(playerid,AutohausShowVehPos[ah][0],AutohausShowVehPos[ah][1],AutohausShowVehPos[ah][2]);
					
					format(string,sizeof(string),"~<~ %s ~>~~n~Hoch/Runter um Ansicht zu aendern~n~LEER = Kaufen, ENTER = Zurueck",AutohausInfo[0][carname]);
					ShowInfoMessage(playerid,string);					
				}
				case 2:
				{
					SetPVarInt(playerid,"npc_gespraech",0);
					SetPVarInt(playerid,"EX_autohaus_in",0);
					SetPVarInt(playerid,"EX_autohaus_nr",0);
					SetPVarInt(playerid,"EX_autohaus_showing",0);
					DeletePVar(playerid,"EX_autohaus_showing");
					DeletePVar(playerid,"EX_autohaus_in");
					DeletePVar(playerid,"EX_autohaus_nr");
					DeletePVar(playerid,"EX_autohaus_car");
					DeletePVar(playerid,"EX_autohaus_watchpos");
					TogglePlayerControllable(playerid,true);
				}
			}
			return 1;
		}
		case dialog_EX_car_INFO_BUY:
		{
			if(response)
			{//Kauf
				new buy_id	 	= GetPVarInt(playerid,"EX_autohaus_car"),
					buy_preis   = AutohausInfo[buy_id][preis],
					npc_cash    = PlayerNPC[playerid][NPC_JASON][cash];
				if(buy_preis > npc_cash)
				{//Hat nicht genug Geld
					format(string,sizeof(string),"{C87E35}Jason (Autohändler):\n\"{F1F7DC}Tut mir leid aber der Wagen kostet %d, du hast mir\nnur %d gegeben. Biete mir mehr.{C87E35}\"",buy_preis,npc_cash);
					car::ShowPlayerDialog(playerid,dialog_EX_car_BUY_FAIL,DIALOG_STYLE_MSGBOX,"{C87E35}Gespräch mit Jason",string,"Zurück","Items geben");
					return 1;
				}
				else
				{//Kauft den Wagen
					format(string,sizeof(string),"{C87E35}Jason (Autohändler):\n\"{F1F7DC}Okay, möchtest du wirklich den %s für %d kaufen?{C87E35}\"",AutohausInfo[buy_id][carname],buy_preis);
					car::ShowPlayerDialog(playerid,dialog_EX_car_BUY,DIALOG_STYLE_MSGBOX,"{C87E35}Gespräch mit Jason",string,"Nee","Kaufen");
					return 1;
				}
			}
			else
			{//Nicht kaufen
				SetPVarInt(playerid,"car_keystate_fix",1);
				return 1;
			}
		}
		case dialog_EX_car_BUY_FAIL:
		{//Kauf gescheitert
			if(!response)
			{//Items geben
				item_SHOW_ITEM_LIST(playerid);
			}
			else
			{//Shop weiter angucken
				SetPVarInt(playerid,"car_keystate_fix",1);
			}
			return 1;
		}
		case dialog_EX_car_BUY:
		{//Kaufoption
			if(!response) 
			{
				SetPVarInt(playerid,"car_keystate_fix",1);
				car::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{C87E35}Gespräch mit Jason","{C87E35}Jason (Autohändler):\n\"{F1F7DC}Okay, vielleicht später...{C87E35}\"","Zurück","");
				return 1;
			}
			new ahaus_id	= GetPVarInt(playerid,"EX_autohaus_nr"),
				buy_id	 	= GetPVarInt(playerid,"EX_autohaus_car"),
				buy_mod		= AutohausInfo[buy_id][model],
				buy_preis   = AutohausInfo[buy_id][preis],
				npc_cash    = PlayerNPC[playerid][NPC_JASON][cash];
			if(buy_preis > npc_cash) 
			{
				SetPVarInt(playerid,"car_keystate_fix",1);
				return 1;//User hat zu wenig Geld, Kauf failed
			}
			SetPVarInt(playerid,"npc_gespraech",0);
			SetPVarInt(playerid,"EX_autohaus_in",0);
			SetPVarInt(playerid,"EX_autohaus_nr",0);
			SetPVarInt(playerid,"EX_autohaus_showing",0);
			DeletePVar(playerid,"EX_autohaus_showing");
			DeletePVar(playerid,"EX_autohaus_in");
			DeletePVar(playerid,"EX_autohaus_nr");
			DeletePVar(playerid,"EX_autohaus_car");
			DeletePVar(playerid,"EX_autohaus_watchpos");
			TogglePlayerControllable(playerid,true);
			HidePlayerDialog(playerid);
			if(EX_car[playerid] != -1)
			{//Auto zerstören
				car::DestroyVehicle(EX_car[playerid]);
				EX_car[playerid] = -1;
			}
			
			new buy_veh = -1;
			switch(ahaus_id)
			{
				case 0: 	buy_veh = car::CreateVehicle(buy_mod,70.5610,1219.0754,18.6675,165.3189,random(132),random(132),-1,true,false,true);
				default: 	buy_veh = car::CreateVehicle(buy_mod,70.5610,1219.0754,18.6675,165.3189,random(132),random(132),-1,true,false,true);
			}

			autohaus_ResetToJason(playerid);
			autohaus_OnLeavePlayerAH(playerid);
			if(buy_veh == -1)
			{//Fehler
			}
			else
			{
				PlayerNPC[playerid][NPC_JASON][cash] -= buy_preis;
				VehicleInfo[buy_veh][cOwner] 	= PlayerInfo[playerid][PLAYER_ID];
				VehicleInfo[buy_veh][cMaxTank]	= car::GetModelMaxTank(buy_mod);
				VehicleInfo[buy_veh][cFuel]		= VehicleInfo[buy_veh][cMaxTank];
				VehicleInfo[buy_veh][cFueltype]	= car::GetModelFuelType(buy_mod);
				PutPlayerInVehicle(playerid,buy_veh,0);
				car::OnCarUpdate(buy_veh);
				format(string,sizeof(string),"{C87E35}Jason (Autohändler):\n\"{F1F7DC}Alles klar, viel Spaß mit deiner neuen Karre. \
				Vergiss nicht\nsie mit \"/auto\" zu parken. Im Menü kannst du weitere Einstellungen\n");
				format(string,sizeof(string),"%sfür deinen fahrbaren Untersatz vornehmen. Gegen Diebe musst du\nunbedingt die Türen verschließen. \
				Nachts empfiehlt es sich mit\nLicht zu fahren. Achja, stell den Motor ab, wenn du grade nicht\nfährst - sonst verbraucht er Benzin, auch wenn keiner drinnensitzt.{C87E35}\"",string);				
				car::ShowPlayerDialog(playerid,dialog_EX_car_MAIN,DIALOG_STYLE_LIST,"{C87E35}Gespräch mit Jason",string,"Bye","");
			}
			SetCameraBehindPlayer(playerid);
			format(string,sizeof(string),"");
		}
		case dialog_EX_car_BUY_SUC:
		{//Kauf erfolgreich
		}
	}
	return 1;
}

autohaus_DisplayMainDialog(playerid)
{
	if(EX_inuse[GetPVarInt(playerid,"EX_autohaus_nr")] == true) format(string,sizeof(string),"Autokauf zur Zeit nicht möglich, komme später wieder");
	else format(string,sizeof(string),"Auto kaufen");
	format(string,sizeof(string),"{F1F7DC}Informationen\n%s\nGespräch beenden",string);
	car::ShowPlayerDialog(playerid,dialog_EX_car_MAIN,DIALOG_STYLE_LIST,"{C87E35}Gespräch mit Jason",string,"Ok","Beenden");
	return 1;
}

autohaus_ResetToJason(playerid)
{
	new ah_id = GetPVarInt(playerid,"EX_autohaus_nr");
	HideInfoMessage(playerid);
	w_SetPlayerPos(playerid,AutohausBotPos[ah_id][0]+floatrandom(0.5)-0.25,AutohausBotPos[ah_id][1]+floatrandom(5)-0.25,AutohausBotPos[ah_id][2]);
	SetPlayerVirtualWorld(playerid,0);
	SetPlayerInterior(playerid,0);
	return 1;
}

autohaus_OnLeavePlayerAH(playerid)
{
	new ah_id = GetPVarInt(playerid,"EX_autohaus_nr");
	EX_inuse[ah_id] = false;
	if(EX_car[playerid] != -1)
	{
		car::DestroyVehicle(EX_car[playerid],false);
		EX_car[playerid] = -1;
	}
	DeletePVar(playerid,"npc_gespraech");
	DeletePVar(playerid,"talk_to_npc");	
	DeletePVar(playerid,"EX_autohaus_in");
	DeletePVar(playerid,"EX_autohaus_nr");
	DeletePVar(playerid,"EX_autohaus_showing");
	DeletePVar(playerid,"EX_autohaus_car");
	DeletePVar(playerid,"EX_autohaus_watchpos");	
	TogglePlayerControllable(playerid,true);
	SetCameraBehindPlayer(playerid);	
	return 1;
}

autohaus_EX_OnPlayerUpdate(playerid,ud,lr)
{
	if(GetPVarInt(playerid,"EX_autohaus_showing") != 1) return 0;
	new ah = GetPVarInt(playerid,"EX_autohaus_nr");
	///////////////////////////SICHT ÄNDERN//////////////////////////////////
    if(ud > 0 || ud < 0)
	{//Hoch oder runter
		new w_pos = GetPVarInt(playerid,"EX_autohaus_watchpos");
		if(ud > 0/*Hoch*/) 			{	if(++w_pos >= (EX_MAX_WATCH_POS-1)) w_pos = 0; 						}
		else if(ud < 0/*Runter*/) 	{ 	if(--w_pos < 0) 					w_pos = (EX_MAX_WATCH_POS-1); 	}
		switch(w_pos)
		{
			case 0: SetPlayerCameraPos(playerid,62.06022644,1231.08569336,-13.85814476);
			case 1: SetPlayerCameraPos(playerid,73.54911804,1224.06030273,-13.40624809);
			case 2: SetPlayerCameraPos(playerid,73.32716370,1231.20104980,-13.27999878);
			case 3: SetPlayerCameraPos(playerid,66.84183502,1225.27770996,-13.20041561);
		}
		SetPlayerCameraLookAt(playerid,AutohausShowVehPos[ah][0],AutohausShowVehPos[ah][1],AutohausShowVehPos[ah][2]);		
		SetPVarInt(playerid,"EX_autohaus_watchpos",w_pos);
	}
	//////////////////////////AUTO ÄNDERN////////////////////////////////////
    if(lr > 0 || lr < 0)
	{//Rechts o Links ====>> AUTO ÄNDERN
		new chosen_veh = GetPVarInt(playerid,"EX_autohaus_car");
		if(lr > 0/*Rechts*/) 	 { if(++chosen_veh > (EX_MAX_AUTOHAUS_CARS-1)) 	chosen_veh = 0; 						}
		else if(lr < 0/*Links*/) { if(--chosen_veh < 0) 						chosen_veh = (EX_MAX_AUTOHAUS_CARS-1); 	}
		if(EX_car[playerid] != -1)
		{
			car::DestroyVehicle(EX_car[playerid],false);
			EX_car[playerid] = -1;
		}
		
		EX_car[playerid] = car::CreateVehicle(AutohausInfo[chosen_veh][model],AutohausShowVehPos[ah][0],AutohausShowVehPos[ah][1],AutohausShowVehPos[ah][2],AutohausShowVehPos[ah][3], -1, -1, -1, false, true, false);

		format(string,sizeof(string),"~<~ %s ~>~~n~Hoch/Runter um Ansicht zu aendern~n~LEER = Kaufen, ENTER = Zurueck",AutohausInfo[chosen_veh][carname]);
		ShowInfoMessage(playerid,string);

		SetPVarInt(playerid,"EX_autohaus_car",chosen_veh);
	}
	return 1;
}