#pragma newdecls required
#pragma semicolon 1
#include <smrpg>
#include <sdkhooks>
#include <sdktools>

#define UPGRADE_SHORTNAME "pscout"
#define PLUGIN_VERSION "1.2.0"

int g_hDamageValue;
int g_hDelay;

public Plugin myinfo = 
{
	name = "SM:RPG Upgrade > Poison Scout",
	author = "WanekWest",
	description = "Poision Scout",
	version = PLUGIN_VERSION,
	url = "https://vk.com/wanek_west"
}

public void OnPluginStart()
{
	HookEvent("player_hurt", EventPlayerHurt);
}

void EventPlayerHurt(Event hEvent, const char[] sEvName, bool bDontBroadcast)
{
	int userid = hEvent.GetInt("userid"), attacker = hEvent.GetInt("attacker"), health = hEvent.GetInt("health");

	if(userid != attacker)
	{
		char sBuf[32];
		
		hEvent.GetString("weapon", sBuf, sizeof(sBuf));
		
		if(strcmp(sBuf, "ssg08") == 0)
		{
			int iLevel = SMRPG_GetClientUpgradeLevel(GetClientOfUserId(attacker), UPGRADE_SHORTNAME);
			int damage = iLevel * g_hDamageValue;
			if(iLevel)
			{
				DataPack hPack;
				CreateDataTimer(g_hDelay * 1.0, TimerHurt, hPack, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
				
				hPack.WriteCell(userid);
				hPack.WriteCell(attacker);
				hPack.WriteCell(iLevel);
				hPack.WriteCell(damage);
				hPack.WriteCell(health);		
			}
		}
	}
}

public void OnPluginEnd()
{
	if(SMRPG_UpgradeExists(UPGRADE_SHORTNAME))
	{
		SMRPG_UnregisterUpgradeType(UPGRADE_SHORTNAME);
	}
}

public void OnAllPluginsLoaded()
{
	OnLibraryAdded("smrpg");
}

public void OnLibraryAdded(const char[] name)
{
	if(StrEqual(name, "smrpg"))
	{
		SMRPG_RegisterUpgradeType("pscout", UPGRADE_SHORTNAME, "Poison effect after shoot", 10, true, 5, 15, 10);
		SMRPG_CreateUpgradeConVar(UPGRADE_SHORTNAME, "smrpg_posion_damage", "10", "Add a damage for every new level(Level*value)", _, true, 0.0);
		ConVar hDamageValue = SMRPG_CreateUpgradeConVar(UPGRADE_SHORTNAME, "smrpg_posion_damage", "10", "Add a damage for every new level(Level*value)", _, true, 0.0);
		hDamageValue.AddChangeHook(OnDMGChange);
		g_hDamageValue = hDamageValue.IntValue;
		SMRPG_CreateUpgradeConVar(UPGRADE_SHORTNAME, "smrpg_poison_time", "10", "Delay between poison effect", _, true, 0.0);
		ConVar hDelay = SMRPG_CreateUpgradeConVar(UPGRADE_SHORTNAME, "smrpg_poison_time", "10", "Delay between poison effect(Value* 1.0(const))", _, true, 0.0);
		hDelay.AddChangeHook(OnDelayChange);
		g_hDelay = hDelay.IntValue;
	}
}

public void OnDMGChange(ConVar hCvar, const char[] szOldValue, const char[] szNewValue)
{
	g_hDamageValue = hCvar.IntValue;
}

public void OnDelayChange(ConVar hCvar, const char[] szOldValue, const char[] szNewValue)
{
	g_hDelay = hCvar.IntValue;
}


public bool SMRPG_ActiveQuery(int client)
{
	int upgrade[UpgradeInfo];
	SMRPG_GetUpgradeInfo(UPGRADE_SHORTNAME, upgrade);
	return SMRPG_IsEnabled() && upgrade[UI_enabled] && SMRPG_GetClientUpgradeLevel(client, UPGRADE_SHORTNAME) > 0;
}

public void SMRPG_TranslateUpgrade(int client, const char[] shortname, TranslationType type, char[] translation, int maxlen)
{
	if(type == TranslationType_Name)
		Format(translation, maxlen, "%T", UPGRADE_SHORTNAME, client);
	else if(type == TranslationType_Description)
	{
		char sDescriptionKey[MAX_UPGRADE_SHORTNAME_LENGTH+12] = UPGRADE_SHORTNAME;
		StrCat(sDescriptionKey, sizeof(sDescriptionKey), " description");
		Format(translation, maxlen, "%T", sDescriptionKey, client);
	}
}

Action TimerHurt(Handle hTimer, DataPack hPack)
{
	hPack.Reset();

	int userid = hPack.ReadCell();
	int attacker = hPack.ReadCell();
	int iClient = GetClientOfUserId(userid), iAttacker = GetClientOfUserId(attacker);

	if(iClient && iAttacker && IsClientInGame(iClient) && IsClientInGame(iAttacker) && IsPlayerAlive(iClient))
	{
		int iCount = hPack.ReadCell();
		int maindamage = hPack.ReadCell();
		int ihealth = hPack.ReadCell();
		SetEntityRenderMode(iClient, RENDER_TRANSCOLOR);
		SetEntityRenderColor(iClient, 20, 255, 20, 255);
		int dhealth = ihealth - maindamage;
		SetEntityHealth(iClient, dhealth);


		if (--iCount > 0)
		{
			hPack.Reset();
			hPack.Position = view_as<DataPackPos>(2);
			hPack.WriteCell(iCount);
			hPack.Position = view_as<DataPackPos>(4);
			hPack.WriteCell(dhealth);
			return Plugin_Continue;
		}
	}
	
	SetEntityRenderMode(iClient, RENDER_TRANSCOLOR);
	SetEntityRenderColor(iClient, 255, 255, 0, 255);
	return Plugin_Stop;
}