#pragma semicolon 1
#include <sourcemod>
#include <sdktools>

public Plugin myinfo = 
{
	name = "Disguise Plugin",
	author = "Moltard / LightningZLaser",
	description = "A plugin to disguise players",
	version = "0.1",
	url = "https://steamcommunity.com/id/0123456789ABC/"
};

Menu g_MainMenu = null;
Menu g_ModelsMenu = null;
Menu g_PlayerMenuD = null;
Menu g_PlayerMenuU = null;

KeyValues g_Models;

int DisguiseStatus[MAXPLAYERS + 1] = 0; // List of players that have a disguise
char DisguisePickedOption[MAXPLAYERS + 1][32]; // Save the model id selected by the admin
char OriginalPlayerModel[MAXPLAYERS + 1][128]; // Save the playermodel of the player before changing it


public void OnPluginStart()
{
	LoadTranslations("common.phrases");
	char sModelsFile[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, sModelsFile, sizeof(sModelsFile), "configs/DisguisePlugin.cfg");
	
	if(!FileExists(sModelsFile))
	{
		SetFailState("Could not find config: \"%s\"", sModelsFile);
		return;
	}

	g_Models = new KeyValues("models");
	if(!g_Models.ImportFromFile(sModelsFile))
	{
		delete g_Models;
		SetFailState("ImportFromFile() failed!");
		return;
	}
	g_Models.Rewind(); // Go back to the root node
	
	HookEvent("round_start", OnRoundStart, EventHookMode_PostNoCopy); 
	RegAdminCmd("sm_disguise", Command_MainMenu, ADMFLAG_CHANGEMAP); //g
}

public void OnMapStart()
{
	g_Models.Rewind();
	g_MainMenu = BuildMainMenu();
	g_ModelsMenu = BuildModelsMenu();
	g_PlayerMenuD = InitPlayerMenu(true);
	g_PlayerMenuU = InitPlayerMenu(false);
}

public void OnMapEnd()
{
	if (g_MainMenu != null)
	{
		delete(g_MainMenu);
		g_MainMenu = null;
	}
	if (g_ModelsMenu != null)
	{
		delete(g_ModelsMenu);
		g_ModelsMenu = null;
	}
	if (g_PlayerMenuD != null)
	{
		delete(g_PlayerMenuD);
		g_PlayerMenuD = null;
	}
	if (g_PlayerMenuU != null)
	{
		delete(g_PlayerMenuU);
		g_PlayerMenuU = null;
	}
}

public OnRoundStart(Handle event, const String:name[], bool dontBroadcast) 
{ 
    for (int i = 1; i <= (GetMaxClients()); i++)
	{
		if (IsValidEntity(i))
		{
			DisguiseStatus[i] = 0;
			OriginalPlayerModel[i] = "";
			DisguisePickedOption[i] = "";
		}
	}
}  

public void OnClientDisconnect(int client){
	DisguiseStatus[client] = 0;
	OriginalPlayerModel[client] = "";
	DisguisePickedOption[client] = "";
}

/* ----------------------------------- */

Menu BuildMainMenu(){
	Menu menu = new Menu(MainMenuHandler);
	menu.SetTitle("Disguise Plugin");
	menu.AddItem("menu_disguise", "Disguise Player");
	menu.AddItem("menu_undisguise", "Undisguise Player");
	menu.ExitButton = true;
	return menu;
}


Menu BuildModelsMenu(){

	g_Models.Rewind(); // Go back to the root node
	Menu menu;
	menu = new Menu(ModelsMenuHandler);
	menu.SetTitle("Select Disguise");
	menu.AddItem("menu_undisguise", "Undisguise");
	
	for(int i = 0; i < 100000; i++){	
		char sName[32]; char sIndex[11]; 
		IntToString(i,sIndex, sizeof(sIndex)); // Index i of the model
		if (!(g_Models.JumpToKey(sIndex, false))) // if the key doesnt exist 
			break; // we stop the loop
		g_Models.GetString("name", sName, sizeof(sName)); // Name of the model
		menu.AddItem(sIndex,sName);
		g_Models.GoBack(); // First sub key of the model category
	}

	g_Models.Rewind(); // Go back to the root node
	menu.ExitBackButton = true;
	menu.ExitButton = true;
	return menu;
}

/* ----------------------------------- */

Menu InitPlayerMenu(bool bl_disguise){
	Menu menu = null;
	if(bl_disguise){
		menu = new Menu(DisguiseHandle);
		menu.SetTitle("Select Player to Disguise");
	}
	else{
		menu = new Menu(UndisguiseHandle);
		menu.SetTitle("Select Player to Undisguise");
	}
	menu.ExitBackButton = true;
	menu.ExitButton = true;
	return menu;
}



void BuildPlayerMenu(bool bl_disguise){

	if(bl_disguise){ // If we come from the Disguise menu
		g_PlayerMenuD.RemoveAllItems();
	}	
	else{ // If we come from the Undisguise
		g_PlayerMenuU.RemoveAllItems();
	}
	for (int i = 1; i <= GetMaxClients(); i++)
	{
		if ((IsClientInGame(i)) && IsPlayerAlive(i))
		{
			char sIndex[11]; 
			IntToString(GetClientSerial(i),sIndex, sizeof(sIndex)); // Serial of the player
			
			char playerName[64]; char labelName[64];
			GetClientName(i, playerName, sizeof(playerName));
			if (DisguiseStatus[i] == 1)
			{
				Format(labelName,sizeof(labelName),"[DISGUISED] %s",playerName); // [DISGUISED] Name
			}
			else if (DisguiseStatus[i] == 0)
			{
				labelName = playerName; // Name
			}
			if(bl_disguise){ // If we come from the Disguise menu
				g_PlayerMenuD.AddItem(sIndex,labelName);
			}	
			else{ // If we come from the Undisguise
				g_PlayerMenuU.AddItem(sIndex,labelName);
			}
		}
	}
}

/* ----------------------------------- */
	
public Action Command_MainMenu(int client, int args)
{
	g_MainMenu.Display(client, MENU_TIME_FOREVER);
	return Plugin_Handled;
}

public MainMenuHandler(Menu menu, MenuAction action, int client, int param2)
{
	char info[32];
	menu.GetItem(param2, info, sizeof(info)); // Get the string of the picked option
	switch(action) { 
		case(MenuAction_Select): 
		{ 
			if(StrEqual(info, "menu_disguise")){
				g_ModelsMenu.Display(client, MENU_TIME_FOREVER);
			}
			else if(StrEqual(info, "menu_undisguise")){
				BuildPlayerMenu(false);
				g_PlayerMenuU.Display(client, MENU_TIME_FOREVER);
			}
		}
	}
}

public ModelsMenuHandler(Menu menu, MenuAction action, int client, int param2)
{
	char info[32];
	menu.GetItem(param2, info, sizeof(info)); // Get the string of the picked option
	switch(action) { 
		case(MenuAction_Select): 
		{ 
			if(StrEqual(info, "menu_undisguise"))
			{
				BuildPlayerMenu(false);
				g_PlayerMenuU.Display(client, MENU_TIME_FOREVER);
			}
			else{
				// Info has the index of the model
				DisguisePickedOption[client] = info; // we set the index selected by the admin
				
				BuildPlayerMenu(true);
				g_PlayerMenuD.Display(client, MENU_TIME_FOREVER);
			}
		}
		case(MenuAction_Cancel): 
		{  
			if(param2 == MenuCancel_ExitBack) {
				g_MainMenu.Display(client, MENU_TIME_FOREVER);
			}
		}
	}
}

public DisguiseHandle(Menu menu, MenuAction action, int client, int param2)
{
	char info[32]; // serial id of the player 
	menu.GetItem(param2, info, sizeof(info)); // Get the string of the picked option
	switch(action) { 
		case(MenuAction_Select): 
		{ 
			int serialId = StringToInt(info);
			int playerId = GetClientFromSerial(serialId); // 0 if invalid
			if ((IsClientInGame(playerId)) && (IsPlayerAlive(playerId)) && playerId != 0){
				if(g_Models.JumpToKey(DisguisePickedOption[client], false)){
					if (DisguiseStatus[playerId] == 0){ // We set the status of the player to disguised
						GetClientModel(playerId, OriginalPlayerModel[playerId], 128);
						DisguiseStatus[playerId] = 1;					
					}
					char sModelName[64];
					g_Models.GetString("name", sModelName, sizeof(sModelName)); // Name of the model 
					
					char sModelPath[128];
					g_Models.GetString("model", sModelPath, sizeof(sModelPath)); // Path of the model 
					
					PrecacheModel(sModelPath, false);
					SetEntityModel(playerId, sModelPath);
				
					ShowActivity2(client, "\x01[SM] \x04", "\x01disguised %N (Model: \x04%s\x01).",playerId,sModelName);
					LogAction(client, -1, "\"%L\" disguised \"%L\" (Model: %s).", client,playerId,sModelName);
				}
				g_Models.Rewind();
			}
			BuildPlayerMenu(true);
			g_PlayerMenuD.Display(client, MENU_TIME_FOREVER); // Display again the player menu 
		}
		case(MenuAction_Cancel): 
		{ 
			if(param2 == MenuCancel_ExitBack) { 
				DisguisePickedOption[client] = "";
				g_ModelsMenu.Display(client, MENU_TIME_FOREVER);
			}
		}
	}
}

public UndisguiseHandle(Menu menu, MenuAction action, int client, int param2)
{
	char info[32]; // serial id of the player 
	menu.GetItem(param2, info, sizeof(info)); // Get the string of the picked option
	switch(action) { 
		case(MenuAction_Select): 
		{ 
			int serialId = StringToInt(info);
			int playerId = GetClientFromSerial(serialId); // 0 if invalid
			if ((IsClientInGame(playerId)) && (IsPlayerAlive(playerId)) && playerId != 0){
				if (DisguiseStatus[playerId] == 0){ // We set the status of the player to disguised
					PrintToChat(client, "\x01[SM]\x04 Player is not disguised");	
				}
				else if (DisguiseStatus[playerId] == 1)
				{
					PrecacheModel(OriginalPlayerModel[playerId], false);
					SetEntityModel(playerId, OriginalPlayerModel[playerId]);
					DisguiseStatus[playerId] = 0;
					ShowActivity2(client, "\x01[SM] \x04", "\x01undisguised %N.",playerId);
					LogAction(client, -1, "\"%L\" undisguised \"%L\".", client,playerId);
				}
			}
			BuildPlayerMenu(false);
			g_PlayerMenuU.Display(client, MENU_TIME_FOREVER); // Display again the player menu 
		}
		case(MenuAction_Cancel): 
		{ 
			if(param2 == MenuCancel_ExitBack) { 
				g_MainMenu.Display(client, MENU_TIME_FOREVER);
			}
		}
	}
}