#include "n_lib/nfile"
#define MAX_EDITOR_OBJECTS		(25)
#define EDITOR_PATH				#MISC_PATH"/mapeditor/"

enum (+=1)
{
	dialog_ME_MAINRE = 1,
	dialog_ME_MAIN,
	dialog_ME_FILENAME,
	dialog_ME_ARRAYNAME,
	dialog_ME_DRAWDISTANCE,
	dialog_ME_OBJECTLIST,
	dialog_ME_CREATEOBJ,
	dialog_ME_OBJECT,
	dialog_ME_CHANGENAME,
}

enum menumi
{
	meFilename[50],
	meUseArray[30],
	Float:meDrawdistance,
}
new MeInfo[menumi];

enum menum
{
	bool:meCreated,
	meName[30],
	meObjectID,
	meModel,
	Float:mePos[3],
	Float:meRot[3],
}
new meInfo[MAX_EDITOR_OBJECTS][menum];

ncom:med(playerid)
{
	format(string,sizeof(string),"Filepath (%s%s.map)\n\
	Array (%s)\n\
	Objektliste (%d Objekte)\n\
	Objekt erstellen\n\
	Drawdistance\n\
	Speichern",\
	EDITOR_PATH,MeInfo[meFilename],\
	MeInfo[meUseArray],\
	med::CountObjects());
	med::ShowPlayerDialog(playerid,dialog_ME_MAIN,DIALOG_STYLE_LIST,"MED - Main",string,"Weiter","Zurück");
	return 1;
}
stock med::CountFreeSlots() return (MAX_EDITOR_OBJECTS-med::CountObjects());
stock med::ShowPlayerDialog(playerid, dialogid, dialogstyle, header[], info[], but1[], but2[])
{
	SetPVarInt(playerid,"use_med",1);
	ShowPlayerDialog(playerid,dialogid,dialogstyle,header,info,but1,but2);
	return 1;
}

stock med::OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(GetPVarInt(playerid,"use_med") != 1) return 0;
	SetPVarInt(playerid,"use_med",0);
	switch(dialogid)
	{
		case dialog_ME_MAIN:
		{
			if(!response) return 1;
			switch(listitem)
			{
				case 0:
				{//Filename
					format(string,sizeof(string),"Filename ändern\nPath: \"%s\"\nName: \"%s\"",EDITOR_PATH,MeInfo[meFilename]);
					med::ShowPlayerDialog(playerid,dialog_ME_FILENAME,DIALOG_STYLE_INPUT,"MED - Filename",string,"Ändern","Zurück");
					return 1;
				}
				case 1:
				{//Array
					format(string,sizeof(string),"Arrayname ändern\nName: %s\n\nnew %s[%d];\n%s[0] = CreateOb....",MeInfo[meUseArray],MeInfo[meUseArray],MAX_EDITOR_OBJECTS,MeInfo[meUseArray]);
					med::ShowPlayerDialog(playerid,dialog_ME_ARRAYNAME,DIALOG_STYLE_INPUT,"MED - Arrayname",string,"Ändern","Zurück");
					return 1;
				}
				case 2:
				{//Objektliste
					format(string,sizeof(string),"");
					for(new i; i<MAX_EDITOR_OBJECTS; i++)
					{
						if(i) format(string,sizeof(string),"%s\n",string);
						format(string,sizeof(string),"%s%d - %s",string,meInfo[i][meModel],meInfo[i][meName]);
					}
					med::ShowPlayerDialog(playerid,dialog_ME_OBJECTLIST,DIALOG_STYLE_LIST,"MED - Objectlist",string,"Select","Zurück");
					return 1;
				}
				case 3:
				{//Erstellen
					med::ShowPlayerDialog(playerid,dialog_ME_CREATEOBJ,DIALOG_STYLE_INPUT,"MED - Create","Gib eine ObjektID ein","Create","Zurück");
					return 1;
				}
				case 4:
				{//Drawdistance
					med::ShowPlayerDialog(playerid,dialog_ME_DRAWDISTANCE,DIALOG_STYLE_INPUT,"MED - Drawdistance","input = Float [0.0-)","Select","Zurück");
					return 1;
				}
				case 5:
				{//Speichern
					if(med::Save())
					{
						format(string,sizeof(string),"Es wurden %d Objekte in die Datei \"%s%s.map\" gespeichert",med::CountObjects(),EDITOR_PATH,MeInfo[meFilename]);
						med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Save",string,"Zurück","");
						return 1;
					}
					med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Save","Fehler, bitte wähle einen anderen Speicherort","Zurück","");
					return 1;
				}
			}
			return 1;
		}
		case dialog_ME_FILENAME:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}
			format(MeInfo[meFilename],50,inputtext);
			format(string,sizeof(string),"Neuer Dateiname \"%s\"",MeInfo[meFilename]);
			med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Filename",string,"Zurück","");
			return 1;
		}
		case dialog_ME_ARRAYNAME:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}
			format(MeInfo[meUseArray],50,inputtext);
			format(string,sizeof(string),"Neuer Arrayname: %s",MeInfo[meUseArray]);
			med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Arrayname",string,"Zurück","");
			return 1;
		}
		case dialog_ME_OBJECTLIST:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}
			if(!meInfo[listitem][meCreated])
			{
				ncom_med(playerid);
				return 1;
			}
			SetPVarInt(playerid,"med_object",listitem);
			med::ShowPlayerDialog(playerid,dialog_ME_OBJECT,DIALOG_STYLE_LIST,"MED - Objekt editieren","Objekt editieren\nName ändern\nObjekt löschen","Zurück","");
			return 1;
		}
		case dialog_ME_CREATEOBJ:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}			
			new slot = med::GetFreeSlot();
			if(slot != -1)
			{
				new Float:p[3],modid=strval(inputtext);
				GetPlayerPos(playerid,p[0],p[1],p[2]);
				new obj = CreateObject(modid,p[0],p[1]+15.0,p[2],0.0,0.0,0.0,MeInfo[meDrawdistance]);
				meInfo[slot][meObjectID] 	= obj;
				meInfo[slot][meModel]		= modid;
				meInfo[slot][mePos][0]		= p[0];
				meInfo[slot][mePos][1]		= p[1]+15.0;
				meInfo[slot][mePos][2]		= p[2];
				meInfo[slot][meRot][0]		= 0.0;
				meInfo[slot][meRot][1]		= 0.0;
				meInfo[slot][meRot][2]		= 0.0;
				format(meInfo[slot][meName],50,"Objekt");				
				meInfo[slot][meCreated]		= true;
				format(string,sizeof(string),"Object erstellt\nModel %d\nSlot %d",modid,slot);
				med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Objekt erstellt",string,"Zurück","");
				return 1;
			}
			med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Fehler","kein platz mehr frei!","Zurück","");
			return 1;
		}
		case dialog_ME_DRAWDISTANCE:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}
			if(floatstr(inputtext) >= 0.0) 	MeInfo[meDrawdistance] = floatstr(inputtext);
			else 							MeInfo[meDrawdistance] = 0.0;
			format(string,sizeof(string),"Neue Drawdistance: %0.3f",MeInfo[meDrawdistance]);
			med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Drawdistance",string,"Zurück","");
			return 1;
		}
		case dialog_ME_OBJECT:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}
			switch(listitem)
			{
				case 0:
				{//Ändern
					new oid = GetPVarInt(playerid,"med_object");
					if(!meInfo[oid][meCreated]) return 1;
					EditObject(playerid,meInfo[oid][meObjectID]);
					return 1;
				}
				case 1:
				{//Name
					med::ShowPlayerDialog(playerid,dialog_ME_CHANGENAME,DIALOG_STYLE_INPUT,"MED - Name ändern","Gib einen neuen Namen ein","Ändern","Zurück");
					return 1;
				}
				case 2:
				{//Löschen
					new oid = GetPVarInt(playerid,"med_object");
					if(!meInfo[oid][meCreated]) return 1;
					DestroyObject(meInfo[oid][meObjectID]);
					meInfo[oid][meObjectID] = 0;
					meInfo[oid][meModel]	= 0;
					format(meInfo[oid][meName],50,"");
					meInfo[oid][meCreated] = false;
					med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Löschung","objekt gelöscht","Zurück","");
					return 1;
				}
			}
			return 1;
		}
		case dialog_ME_CHANGENAME:
		{
			if(!response)
			{
				ncom_med(playerid);
				return 1;
			}
			new oid = GetPVarInt(playerid,"med_object");
			if(!meInfo[oid][meCreated]) return 1;
			format(meInfo[oid][meName],50,inputtext);
			format(string,sizeof(string),"Neue Name: %s",inputtext);
			med::ShowPlayerDialog(playerid,dialog_ME_MAINRE,DIALOG_STYLE_MSGBOX,"MED - Name geändert",string,"Zurück","");
			return 1;
		}
		case dialog_ME_MAINRE:
		{
			ncom_med(playerid);
			return 1;
		}
	}
	return 1;
}

stock med::CountObjects()
{
	new count;
	for(new i; i<MAX_EDITOR_OBJECTS; i++)
	{
		if(!meInfo[i][meCreated]) continue;
		count++;
	}
	return count;
}

med::GetFreeSlot()
{
	for(new i; i<MAX_EDITOR_OBJECTS; i++) if(!meInfo[i][meCreated]) return i;
	return -1;
}

stock med::Save()
{
	new path[100];
	format(path,100,"%s%s.map",EDITOR_PATH,MeInfo[meFilename]);
	if(nfile::Exists(path)) return 0;
	nfile::Switch(path);
	
	new date[3];
	getdate(date[0],date[1],date[2]);
	new time[3];
	gettime(time[0],time[1],time[2]);
	
	format(string,sizeof(string),"Datum %d.%d.%d - %d:%d",date[2],date[1],date[0],time[0],time[1]);
	format(string,sizeof(string),"%s\nObjekte: %d\n\n",string,med::CountObjects());
	if(MeInfo[meUseArray][0]) format(string,sizeof(string),"%snew %s[%d];",string,MeInfo[meUseArray],med::CountObjects());
	nfile::Append(string);
	
	new aid = 0;
	for(new i; i<MAX_EDITOR_OBJECTS; i++)
	{
		if(!meInfo[i][meCreated]) continue;
		new objectid = meInfo[i][meObjectID];
		new Float:x, Float:y, Float:z;
		GetObjectPos(objectid, x, y, z);
		new Float:RotX,Float:RotY,Float:RotZ;
		GetObjectRot(objectid, RotX, RotY, RotZ);
		if(MeInfo[meUseArray][0]) format(string,sizeof(string),"\n%s[%d] = ",MeInfo[meUseArray],aid);
		else format(string,sizeof(string),"\n");		
		format(string,sizeof(string),"%sCreateObject(%d,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.1f);//%s",string,
		meInfo[i][meModel],\
		x, y, z,\
		RotX, RotY, RotZ,\
		MeInfo[meDrawdistance],meInfo[i][meName]);
		nfile::Append(string);
		aid++;
	}
	return 1;
}