//#include "DIALOGS\tailor_dialog.h"

int GetModelPrice(ref PChar, ref NPChar, string ModelPrice)
{
	int ModelBasePrice = sti(ModelPrice);
	int BuySkill		= sti(PChar.skill.Commerce);
	int SellSkill	  = sti(NPChar.skill.Commerce);
	int RandSeed;
	int RandomFactor;
	int SellPrice;
	if (BuySkill > SellSkill)
	{
		RandSeed	 = sti(ModelBasePrice / 30 * (BuySkill - SellSkill));
		RandomFactor = rand(RandSeed); // - 0-30%
		SellPrice	= sti(ModelBasePrice - RandomFactor);
	}
	else
	{
		if (BuySkill < SellSkill)
		{
			RandSeed	 = sti(ModelBasePrice / 5 * (SellSkill - BuySkill));
			RandomFactor = rand(RandSeed); // + 0-200% up
			SellPrice	= sti(ModelBasePrice + RandomFactor);
		}
		else
		{
			RandSeed	 = sti(ModelBasePrice / 20);
			RandomFactor = rand(RandSeed); // + 0-5% up
			SellPrice	= sti(ModelBasePrice + RandomFactor);
		}
	}
	if (SellPrice > 10000)
	{
		SellPrice = sti(SellPrice/1000)*1000;
	}
	else
	{
		if (SellPrice > 1000)
		{
			SellPrice = sti(SellPrice/100)*100;
		}
		else
		{
			if (SellPrice > 100)
			{
				SellPrice = sti(SellPrice/50)*50;
			}
			else
			{
				if (SellPrice > 10)
				{
					SellPrice = sti(SellPrice/10)*10;
				}
			}
		}
	}
	return SellPrice;
}

//bool IsInvalidModel(ref PChar, ref refTailor, int intModelIndex, int Slot)
//{
//	int i;
//	bool boolIsInvalid = true;
//	if (sti(Models[intModelIndex].assigned) == true)
//	{
//		if (sti(Models[intModelIndex].nation) == sti(refTailor.nation))
//		{
//			//if(GetRank(PChar, sti(refTailor.nation)) >= sti(Models[intModelIndex].minrank) && GetRMRelation(PChar,sti(refTailor.nation)) >= stf(Models[intModelIndex].minrel))
//			if(GetRank(PChar, sti(refTailor.nation)) >= sti(Models[intModelIndex].minrank) )
//			//{
//				boolIsInvalid = false; // NK
//			//}
//		}
//		else
//		{
//			if (sti(Models[intModelIndex].nation) == ALL_NATIONS)
//			{
//				if (Slot < MAX_MODELSLOTS/2)
//				{
//					boolIsInvalid = false;
//				}
//			}
//		}
//		if(GetLevel(PChar) < sti(Models[intModelIndex].minlevel))
//		{
//			boolIsInvalid = true; // NK
//		}
//		for(i = 0; i < Slot; i++)
//		{
//			if (ModelSlots[i].Index == intModelIndex)
//			{
//				boolIsInvalid = true; // NK
//			}
//		}
//	}
//	//do we already own this?
//	if(CheckAttribute(PChar,"clothes."+Models[intModelIndex].model)) boolIsInvalid = true;
//	return boolIsInvalid;
//}

bool IsInvalidModel(ref PChar, int intModelIndex, int Slot)
{
	int i;
	bool boolIsInvalid = false;
	if(GetLevel(PChar) < sti(Models[intModelIndex].minlevel))
	{
		boolIsInvalid = true; // NK
	}
	for(i = 0; i < Slot; i++)
	{
		if (ModelSlots[i].Index == intModelIndex)
		{
			boolIsInvalid = true; // NK
		}
	}
	if(CheckAttribute(PChar,"clothes."+Models[intModelIndex].model))
	{
		boolIsInvalid = true;
	}
	return boolIsInvalid;
}

void InitModelSlots(ref PChar, ref refTailor, string strNow)
{
	int i;
	int intModelIndex;
	int intModelNation;
	int breakcounter = 0;
	bool boolGetNewModels = false; // Scheffnow: set default
	string strSlot;

	if(!CheckAttribute(refTailor, "AppearanceMod.Model_0"))
 	{
		boolGetNewModels = true;
 	}
 	else
 	{
		if(refTailor.AppearanceMod.LastSpeakDate != strNow)
		{
			boolGetNewModels = true;
			refTailor.AppearanceMod.LastSpeakDate = strNow; // NK
		}
		else
		{
			if(DEBUG) boolGetNewModels = true; // NK  // Scheffnow: When debug is true it should select new models
		}
	}
	boolGetNewModels = true;
	for(i = 0; i < 5; i++)
	{
		strSlot = "Model_" + i;
		DeleteAttribute(ModelSlots[i],""); // NK
		if (boolGetNewModels == true)
		{
			breakcounter = 0;
			if (i > 1)
			{
				intModelNation = refTailor.nation;
			}
			else
			{
				intModelNation = ALL_NATIONS;
			}
			intModelIndex = GetRandomModelForNation(intModelNation,GetRank(&PChar,intModelNation)); // NK 04-06
			while(IsInvalidModel(PChar, intModelIndex, i) == true)
			{
				intModelIndex = GetRandomModelForNation(intModelNation,GetRank(&PChar,intModelNation)); // NK 04-06
				breakcounter++; // NK just in case
				if(breakcounter > 1000)
				{
					break; // ditto
				}
			}
			ModelSlots[i].Index = intModelIndex;
			ModelSlots[i].model = Models[intModelIndex].model;
			ModelSlots[i].price = GetModelPrice(PChar, refTailor, Models[intModelIndex].price);
			ModelSlots[i].descr = Models[intModelIndex].description;
			refTailor.AppearanceMod.(strSlot).model = ModelSlots[i].model;
			refTailor.AppearanceMod.(strSlot).index = ModelSlots[i].index;
			refTailor.AppearanceMod.(strSlot).price = ModelSlots[i].price;
			refTailor.AppearanceMod.(strSlot).descr = ModelSlots[i].descr;
		}
		else
		{
			ModelSlots[i].index = refTailor.AppearanceMod.(strSlot).index;
			ModelSlots[i].model = refTailor.AppearanceMod.(strSlot).model;
			ModelSlots[i].price = refTailor.AppearanceMod.(strSlot).price;
			ModelSlots[i].descr = refTailor.AppearanceMod.(strSlot).descr;
		}
	}
}

void AppearanceMod_ModelLinks(ref PChar, ref d, aref Link)
{
	d.Text = DLG_TEXT[15];
	Link.l1 = DLG_TEXT[16];
	Link.l1.go = "AppearanceMod_Exit";
	// NK -->
	string linkstr;
	int i;
	for(i = 0; i < MAX_MODELSLOTS; i++)
	{
		linkstr = "l" + (i+2);
		//Link.(linkstr) = Models[sti(ModelSlots[i].Index)].description;
		int tmpDescFileID = LanguageOpenFile("models_description.txt");
		Link.(linkstr) = ModelSlots[i].price + DLG_TEXT[17] + LanguageConvertString(tmpDescFileID,ModelSlots[i].descr);
		Link.(linkstr).go = "AppearanceMod_Select_"+(i+1);
		LanguageCloseFile(tmpDescFileID);
	}
}
void AppearanceMod_AnswerLinks(ref PChar, ref d, aref Link, int intPrice)
{
	int intOfficerPos = sti(PChar.AppearanceMod.OfficerPos);
	int intMoney	  = sti(PChar.money);
	d.Text	 = DLG_TEXT[18];
	Link.l1	= DLG_TEXT[19];
	Link.l1.go = "AppearanceMod_SetOfficer"+intOfficerPos;
	if (intPrice > intMoney)
	{
		Link.l2	= DLG_TEXT[20];
		Link.l2.go = "AppearanceMod_SetOfficer"+intOfficerPos;
	}
	else
	{
		Link.l2	= DLG_TEXT[21];
		Link.l2.go = "AppearanceMod_Buy_choice";
		PChar.AppearanceMod.Price = intPrice;
	}
	Link.l3	= DLG_TEXT[22];
	Link.l3.go = "AppearanceMod_Exit";
}

// NK real func moved to models.h
// move setmodelfromarray to models.c 05-04-30

void ProcessDialogEvent()
{
	ref NPChar, PChar, d;
	PChar = GetMainCharacter();
	aref Link, Diag;
	string NPC_Area;
	string tempstr;
	ref refOfficer;
	int intOfficer;
	int i;
	int j;
	string iDay, iMonth;
	iDay = environment.date.day;
	iMonth = environment.date.month;
	string strNow = iMonth + " " + iDay;

	DeleteAttribute(&Dialog,"Links");

	makeref(NPChar,CharacterRef);
	makearef(Link, Dialog.Links);
	makeref(d, Dialog);
	makearef(Diag, NPChar.Dialog);

	//NPC_Area = NPChar.quest.area;

	switch(Dialog.CurrentNode)
	{
		case "exit":
			Diag.CurrentNode = Diag.TempNode;
			DialogExit();
		break;

		case "First time":
			Dialog.defAni = "dialog_stay1";
			Dialog.defCam = "1";
			Dialog.defSnd = "dialogs\0\017";
			Dialog.defLinkAni = "dialog_1";
			Dialog.defLinkCam = "1";
			Dialog.defLinkSnd = "dialogs\woman\024";
			Dialog.ani = "dialog_stay2";
			Dialog.cam = "1";

			Dialog.snd1 = "voice\USDI\USDI004";
			Dialog.snd2 = "voice\USDI\USDI005";
			Dialog.snd3 = "voice\USDI\USDI006";
			if(!CheckAttribute(NPChar, "AppearanceMod"))
			{
				NPChar.AppearanceMod.LastSpeakDate = strNow;
				d.Text = DLG_TEXT[23] + GetMyFullName(NPChar)  + DLG_TEXT[24] + GetMyAddressForm(NPChar, PChar, ADDR_POLITE, false, false) + ".";
				Link.l1 = DLG_TEXT[25];
				Link.l1.go = "AppearanceMod_MainDialog";
				Link.l2 = DLG_TEXT[26];
				Link.l2.go = "exit";
			}
			else
			{
				if (NPChar.AppearanceMod.LastSpeakDate != strNow)
				{
					// NK: lastspeakdate = strNow moved to InitModelSlots
					d.Text = DLG_TEXT[14] + GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, true) + DLG_TEXT[27];
					Link.l1 = DLG_TEXT[28] + GetMyName(NPChar) + DLG_TEXT[29];
					Link.l1.go = "AppearanceMod_MainDialog";
					Link.l2 = DLG_TEXT[30] + GetMyName(NPChar) + DLG_TEXT[31];
					Link.l2.go = "exit";
				}
				else
				{
					d.Text = DLG_TEXT[14] + GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, true) + DLG_TEXT[32];
					Link.l1 = DLG_TEXT[33] + GetMyName(NPChar) + DLG_TEXT[34];
					Link.l1.go = "AppearanceMod_MainDialog";
					Link.l2 = DLG_TEXT[35] + GetMyName(NPChar) + DLG_TEXT[36];
					Link.l2.go = "exit";
				}
			}
			if(DEBUG)
			{
				 PChar.AppearanceMod.ClothierModel = NPChar.model;
				 Link.l3 = DLG_TEXT[37];
				 Link.l3.go = "AppearanceMod_CircleThroughAllModels";
			}
		break;

		case "AppearanceMod_MainDialog":
			PChar.AppearanceMod.ClothierModel = NPChar.model;
			InitModelSlots(PChar,NPChar,strNow);
			d.Text = DLG_TEXT[38];
			Link.l1 = DLG_TEXT[39];
			Link.l1.go = "AppearanceMod_SetOfficer0"; // NK
			intOfficer = sti(GetOfficersIndex(PChar,1));
			if( intOfficer != -1)
			{
				 refOfficer = GetCharacter(intOfficer);
				 Link.l2 = DLG_TEXT[40] + refOfficer.name + " " +refOfficer.lastname + DLG_TEXT[59]; 
				 Link.l2.go = "AppearanceMod_SetOfficer1";
			}
			intOfficer = sti(GetOfficersIndex(PChar,2));
			if( intOfficer != -1)
			{
				 refOfficer = GetCharacter(intOfficer);
				 Link.l3 = DLG_TEXT[40] + refOfficer.name + " " +refOfficer.lastname + DLG_TEXT[59];
				 Link.l3.go = "AppearanceMod_SetOfficer2";
			}
			intOfficer = sti(GetOfficersIndex(PChar,3));
			if( intOfficer != -1)
			{
				 refOfficer = GetCharacter(intOfficer);
				 Link.l4 = DLG_TEXT[40] + refOfficer.name + " " +refOfficer.lastname + DLG_TEXT[59];
				 Link.l4.go = "AppearanceMod_SetOfficer3";
			}
		break;

		case "AppearanceMod_SetOfficer0":
			PChar.AppearanceMod.OfficerIndex = 0;
			PChar.AppearanceMod.OfficerPos	= 0;
			AppearanceMod_ModelLinks(PChar,d,Link);
		break;

		case "AppearanceMod_SetOfficer1":
			intOfficer = sti(GetOfficersIndex(PChar,1));
			PChar.AppearanceMod.OfficerIndex = intOfficer;
			PChar.AppearanceMod.OfficerPos	= 1;
			AppearanceMod_ModelLinks(PChar,d,Link);
		break;

		case "AppearanceMod_SetOfficer2":
			intOfficer = sti(GetOfficersIndex(PChar,2));
			PChar.AppearanceMod.OfficerIndex = intOfficer;
			PChar.AppearanceMod.OfficerPos	= 2;
			AppearanceMod_ModelLinks(PChar,d,Link);
		break;

		case "AppearanceMod_SetOfficer3":
			intOfficer = sti(GetOfficersIndex(PChar,3));
			PChar.AppearanceMod.OfficerIndex = intOfficer;
			PChar.AppearanceMod.OfficerPos	= 3;
			AppearanceMod_ModelLinks(PChar,d,Link);
		break;

		// NK add buy choice -->
		case "AppearanceMod_Buy_choice":
			int price = sti(Models[GetModelIndex(NPChar.model)].price);
			d.text = DLG_TEXT[41] + price + DLG_TEXT[42];
			if(GetCharacterMoney(PChar) >= price)
			{
				Link.l1 = DLG_TEXT[43];
				Link.l1.go = "AppearanceMod_Buy_now";
				Link.l2 = DLG_TEXT[44];
				Link.l2.go = "AppearanceMod_Buy";
			}
			Link.l3 = DLG_TEXT[45];
			Link.l3.go = "AppearanceMod_Exit";
		break;

		case "AppearanceMod_Buy": //NK: no model change, just buy
			// NK to buy the clothing -->
			tempstr = NPChar.model;
			PChar.clothes.(tempstr) = true;
			PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(PChar, -sti(Models[GetModelIndex(tempstr)].price));
			// NK <--
			SetModel(NPChar, PChar.AppearanceMod.ClothierModel, "man", "man", 1.8, false);
			DeleteAttribute(PChar,"AppearanceMod");
			DialogExit();
		break;

		case "AppearanceMod_Buy_now": //NK: buy _and_ change clothes
			// NK to buy the clothing, _and_ set current clothing bought -->
			tempstr = PChar.model;
			PChar.clothes.(tempstr) = true;
			tempstr = NPChar.model;
			PChar.clothes.(tempstr) = true;
			PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(PChar, -sti(Models[GetModelIndex(tempstr)].price));
			// NK <--
			intOfficer = sti(PChar.AppearanceMod.OfficerIndex);
			refOfficer = GetCharacter(intOfficer);
			SetModel(refOfficer, NPChar.model, NPChar.model.animation, NPChar.sex, stf(NPChar.model.height), true);
			SetModel(NPChar, PChar.AppearanceMod.ClothierModel, "man", "man", 1.8, false);
			DeleteAttribute(PChar,"AppearanceMod");
			DialogExit();
		break;

		case "AppearanceMod_Exit":
			SetModel(NPChar, PChar.AppearanceMod.ClothierModel, "man", "man", 1.8, false);
			DeleteAttribute(PChar,"AppearanceMod");
			DialogExit();
		break;

		case "AppearanceMod_Select_1":
			SetModelfromArray(NPChar, sti(ModelSlots[0].index));
			AppearanceMod_AnswerLinks(PChar,d,Link,ModelSlots[0].price));
		break;
		case "AppearanceMod_Select_2":
			SetModelfromArray(NPChar, sti(ModelSlots[1].index));
			AppearanceMod_AnswerLinks(PChar,d,Link,ModelSlots[1].price));
		break;
		case "AppearanceMod_Select_3":
			SetModelfromArray(NPChar, sti(ModelSlots[2].index));
			AppearanceMod_AnswerLinks(PChar,d,Link,ModelSlots[2].price));
		break;
		case "AppearanceMod_Select_4":
			SetModelfromArray(NPChar, sti(ModelSlots[3].index));
			AppearanceMod_AnswerLinks(PChar,d,Link,ModelSlots[3].price));
		break;
		case "AppearanceMod_Select_5":
			SetModelfromArray(NPChar, sti(ModelSlots[4].index));
			AppearanceMod_AnswerLinks(PChar,d,Link,ModelSlots[4].price));
		break;

		case "AppearanceMod_CircleThroughAllModels":
			if(!CheckAttribute(NPChar, "CircleThroughAllModels"))
			{
			  NPChar.CircleThroughAllModels.ModelIndex = 0;
			  NPChar.CircleThroughAllModels.Direction  = 1;
			}
			i = sti(NPChar.CircleThroughAllModels.ModelIndex);
			j = sti(NPChar.CircleThroughAllModels.Direction);
			if(j == 1)
			{
				NPChar.CircleThroughAllModels.ModelIndex = i + 1;
			}
			else
			{
				if (i > 0)
				{
					NPChar.CircleThroughAllModels.ModelIndex = i -1;
				}
			}
			if (Models[i].assigned == true)
			{
				SetModelfromArray(NPChar, i));
				int tmpDescFileID = LanguageOpenFile("models_description.txt");
				d.text = models[i].index  + ": " + models[i].model  + " : " + models[i].price + "/" + GetModelPrice(PChar, NPChar, Models[i].price)  + " : " +  LanguageConvertString(tmpDescFileID,models[i].description);
				LanguageCloseFile(tmpDescFileID);

				if(j == 1)
				{
					Link.l1 = DLG_TEXT[46];
					Link.l1.go = "AppearanceMod_CircleThroughAllModels";
				}
				else
				{
					Link.l1 = DLG_TEXT[47];
					Link.l1.go = "AppearanceMod_CircleThroughAllModels";
				}
				Link.l2 = DLG_TEXT[48];
				Link.l2.go = "AppearanceMod_CircleDirection";
				Link.l3 = DLG_TEXT[49];
				Link.l3.go = "AppearanceMod_CircleBuy_now";
				Link.l4 = DLG_TEXT[50];
				Link.l4.go = "AppearanceMod_Exit";
			}
			else
			{
				DeleteAttribute(NPChar,"CircleThroughAllModels");
				d.text = DLG_TEXT[51] + i  + DLG_TEXT[52];
				Link.l2 = DLG_TEXT[53];
				Link.l2.go = "AppearanceMod_CircleThroughAllModels";
				Link.l3 = DLG_TEXT[50];
				Link.l3.go = "AppearanceMod_Exit";
			}
		break;

		case "AppearanceMod_CircleDirection":
			if(NPChar.CircleThroughAllModels.Direction == 1)
			{
				NPChar.CircleThroughAllModels.Direction  = -1;
				NPChar.CircleThroughAllModels.ModelIndex = sti(NPChar.CircleThroughAllModels.ModelIndex) - 2;
			}
			else
			{
				NPChar.CircleThroughAllModels.Direction = 1;
				NPChar.CircleThroughAllModels.ModelIndex = sti(NPChar.CircleThroughAllModels.ModelIndex) + 2;
			}
			d.text	 = DLG_TEXT[54];
			Link.l1	= DLG_TEXT[55];
			Link.l1.go = "AppearanceMod_CircleThroughAllModels";
			Link.l2	= DLG_TEXT[50];
			Link.l2.go = "AppearanceMod_Exit";

		break;

		case "AppearanceMod_CircleBuy_now":
			tempstr = PChar.model;
			PChar.clothes.(tempstr) = true;
			tempstr = NPChar.model;
			PChar.clothes.(tempstr) = true;
			PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(PChar, -sti(Models[GetModelIndex(tempstr)].price));
			SetModel(PChar, NPChar.model, NPChar.model.animation, NPChar.sex, stf(NPChar.model.height), true);
			SetModel(NPChar, PChar.AppearanceMod.ClothierModel, "man", "man", 1.8, false);
			d.text	 = DLG_TEXT[56];
			Link.l1	= DLG_TEXT[57];
			Link.l1.go = "AppearanceMod_CircleThroughAllModels";
			Link.l2	= DLG_TEXT[58];
			Link.l2.go = "AppearanceMod_Exit";
		break;
	}
}

