//====================================================================================================
//
// Name: [entWatch] Tools
// Author: zaCade & Prometheum
// Description: Handle the tools of [entWatch]
//
//====================================================================================================
#include <multicolors>

#pragma newdecls required

#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#include <entWatch_core>
#include <entWatch_helpers>

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "[entWatch] Tools",
	author       = "zaCade & Prometheum",
	description  = "Handle the tools of [entWatch]",
	version      = "4.0.0"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	LoadTranslations("common.phrases");
	LoadTranslations("entWatch.tools.phrases");

	RegAdminCmd("sm_etransfer", Command_TransferItem, ADMFLAG_BAN);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_TransferItem(int client, int args)
{
	if (GetCmdArgs() < 2)
	{
		CReplyToCommand(client, "\x07%s[entWatch] \x07%sUsage: sm_etransfer <#userid/name> <#userid/name>", "E01B5D", "F16767");
		return Plugin_Handled;
	}

	char sArguments[2][32];
	GetCmdArg(1, sArguments[0], sizeof(sArguments[]));
	GetCmdArg(2, sArguments[1], sizeof(sArguments[]));

	if (strncmp(sArguments[0], "$", 1, false) == 0)
	{
		strcopy(sArguments[0], sizeof(sArguments[]), sArguments[0][1]);

		int reciever;
		if ((reciever = FindTarget(client, sArguments[1], true)) == -1)
			return Plugin_Handled;

		char sName[32];
		char sShort[32];
		char sColor[32];

		bool bTransfered;
		for (int index; index < EW_GetItemCount(); index++)
		{
			CItem item = EW_GetItemData(index);

			item.dConfig.GetName(sName, sizeof(sName));
			item.dConfig.GetShort(sShort, sizeof(sShort));
			item.dConfig.GetColor(sColor, sizeof(sColor));

			if (StrContains(sName, sArguments[0], false) != -1 || StrContains(sShort, sArguments[0], false) != -1)
			{
				if (item.bWeapon && !(item.dConfig.iSlot == SLOT_NONE || item.dConfig.iSlot == SLOT_KNIFE))
				{
					int iDisplay = item.dConfig.iDisplay;

					if (item.bClient)
					{
						SDKHooks_DropWeapon(item.iClient, item.iWeapon, NULL_VECTOR, NULL_VECTOR);

						char sWeaponClass[32];
						GetEntityClassname(item.iWeapon, sWeaponClass, sizeof(sWeaponClass));
						GivePlayerItem(item.iClient, sWeaponClass);
					}

					item.dConfig.iDisplay &= ~(1<<0);

					EquipPlayerWeapon(reciever, item.iWeapon);

					item.dConfig.iDisplay = iDisplay;
					bTransfered = true;
					break;
				}
			}
		}

		if (!bTransfered)
		{
			CReplyToCommand(client, "\x07%s[entWatch] \x07%sError: no transferable items found!", "E01B5D", "F16767");
			return Plugin_Handled;
		}

		CPrintToChatAll("\x07%s[entWatch] \x07%s%N\x07%s transfered \x07%s%s\x07%s to \x07%s%N\x07%s.", "E01B5D", "EDEDED", client, "F16767", sColor, sName, "F16767", "EDEDED", reciever, "F16767");
		LogAction(client, -1, "%L transfered %s to %L.", client, sName, reciever);
	}
	else
	{
		int target;
		if ((target = FindTarget(client, sArguments[0], true)) == -1)
			return Plugin_Handled;

		int reciever;
		if ((reciever = FindTarget(client, sArguments[1], true)) == -1)
			return Plugin_Handled;

		if (GetClientTeam(target) != GetClientTeam(reciever))
		{
			CReplyToCommand(client, "\x07%s[entWatch] \x07%sError: teams dont match!", "E01B5D", "F16767");
			return Plugin_Handled;
		}

		bool bTransfered;
		for (int index; index < EW_GetItemCount(); index++)
		{
			CItem item = EW_GetItemData(index);

			if (item.bClient && item.iClient == target)
			{
				if (item.bWeapon && !(item.dConfig.iSlot == SLOT_NONE || item.dConfig.iSlot == SLOT_KNIFE))
				{
					int iDisplay = item.dConfig.iDisplay;

					SDKHooks_DropWeapon(item.iClient, item.iWeapon, NULL_VECTOR, NULL_VECTOR);

					char sWeaponClass[32];
					GetEntityClassname(item.iWeapon, sWeaponClass, sizeof(sWeaponClass));
					GivePlayerItem(item.iClient, sWeaponClass);

					item.dConfig.iDisplay &= ~(1<<0);

					EquipPlayerWeapon(reciever, item.iWeapon);

					item.dConfig.iDisplay = iDisplay;
					bTransfered = true;
				}
			}
		}

		if (!bTransfered)
		{
			CReplyToCommand(client, "\x07%s[entWatch] \x07%sError: target has no transferable items!", "E01B5D", "F16767");
			return Plugin_Handled;
		}

		CPrintToChatAll("\x07%s[entWatch] \x07%s%N\x07%s transfered all items from \x07%s%N\x07%s to \x07%s%N\x07%s.", "E01B5D", "EDEDED", client, "F16767", "EDEDED", target, "F16767", "EDEDED", reciever, "F16767");
		LogAction(client, target, "%L transfered all items from %L to %L.", client, target, reciever);
	}

	return Plugin_Handled;
}