#include <sourcemod>
#include <filenetmessages>

//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	RegConsoleCmd("sm_request", Command_Request);

	RegConsoleCmd("sm_create", Command_Create);

}

//----------------------------------------------------------------------------------------------------
public Action Command_Request(int client, int argc)
{
	FNM_RequestFile(client, "data/unloze/keychain.dat");

	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose: We requested the file, client had it and we recieved it.
//----------------------------------------------------------------------------------------------------
public void FNM_OnFileReceived(int client, const char[] filename, int transferid)
{
	char sUserHash[32];
	ReadUserHashFile(client, sUserHash, sizeof(sUserHash));

	PrintToChatAll("Returned hash for %N: %s", client, sUserHash);
}

//----------------------------------------------------------------------------------------------------
// Purpose: We requested the file, client didnt have it.
//----------------------------------------------------------------------------------------------------
public void FNM_OnFileDenied(int client, const char[] filename, int transferid)
{
	PrintToChatAll("Returned hash for %N: %s", client, "DEUS VULT");
}





//----------------------------------------------------------------------------------------------------
public Action Command_Create(int client, int argc)
{
	char sUserHash[32];

	for(int i; i < 30; i++)
	{
		switch(GetRandomInt(1, 3))
		{
			// Numbers
			case(1): sUserHash[i] = view_as<char>(GetRandomInt(48, 57));

			//Capitals
			case(2): sUserHash[i] = view_as<char>(GetRandomInt(65, 90));

			//Letters
			case(3): sUserHash[i] = view_as<char>(GetRandomInt(97, 122));
		}
	}

	PrintToChatAll("Creating user hash for %N: %s", client, sUserHash);

	SendUserHashFile(client, sUserHash);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
public void SendUserHashFile(client, const char[] sHash)
{
	if(FileExists("data/unloze/keychain.dat"))
	{
		LogError("%L hash file exists, stopping to prevent conflicts!", client);
		return;
	}

	File fFile;
	if((fFile = OpenFile("data/unloze/keychain.dat", "w")) != null)
	{
		fFile.WriteString(sHash, false);
		fFile.Close();
	}
	else
	{
		LogError("%L failed to create hash file.", client);
		return;
	}

	if(FNM_SendFile(client, "data/unloze/keychain.dat") == 0)
	{
		LogError("%L failed to send hash file.", client);
		return;
	}

	if(!DeleteFile("data/unloze/keychain.dat"))
	{
		LogError("%L failed to delete hash file.", client);
		return;
	}
}

//----------------------------------------------------------------------------------------------------
public void ReadUserHashFile(client, char[] sHash, int hashLength)
{
	if(!FileExists("download/data/unloze/keychain.dat"))
	{
		LogError("%L hash file does not exists, unable to read!", client);
		return;
	}

	File fFile;
	if((fFile = OpenFile("download/data/unloze/keychain.dat", "r")) != null)
	{
		fFile.ReadString(sHash, hashLength);
		fFile.Close();
	}
	else
	{
		LogError("%L failed to read hash file.", client);
		return;
	}

	if(!DeleteFile("download/data/unloze/keychain.dat"))
	{
		LogError("%L failed to delete hash file.", client);
		return;
	}
}





//----------------------------------------------------------------------------------------------------
// Purpose: Have yet to figure out when this fires.
//----------------------------------------------------------------------------------------------------
public void FNM_OnFileSent(int client, const char[] filename, int transferid)
{
	PrintToServer("FNM_OnFileSent: %N -> %s", client, filename);
}

//----------------------------------------------------------------------------------------------------
// Purpose: Client requested a file, so far only seems to fire for user_custom data files.
//----------------------------------------------------------------------------------------------------
public Action FNM_OnFileRequested(int client, const char[] filename, int transferid)
{
	PrintToServer("FNM_OnFileRequested: %N -> %s", client, filename);
}