#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <zombieplague>

#define PLUGIN "[ZP] Extra : Heal Rain"
#define VERSION "1.0.1"
#define AUTHOR "Kia"

#define PREFIX "[Heal Rain]"

/* Integer */

new g_iItemID
new g_iChosenPlayers[33]
new g_iUsed[33]

/* CVars */

new cvar_maxplayers, cvar_hp, cvar_limit

/* Booleans */

new bool:g_bWillBeHealed[33]

/* Screenfade */

new msgScreenFade

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	/* Registering Item */
	
	g_iItemID = zp_register_extra_item("Heal Rain", 25, ZP_TEAM_ZOMBIE)
	
	/* CVars */
	
	cvar_maxplayers		= register_cvar("zp_hr_maxplayers","3")
	cvar_hp			= register_cvar("zp_hr_healing","550")
	cvar_limit		= register_cvar("zp_hr_limitpr","3")
	
	/* Screenfade */
	
	msgScreenFade = get_user_msgid("ScreenFade")
	
	/* Events */
	
	register_event("HLTV", "func_HLTVNewRound", "a", "1=0", "2=0") 
}

public client_putinserver(id)
{
	g_iChosenPlayers[id] = 0
	g_bWillBeHealed[id] = false
}

public client_disconnect(id)
{
	g_iChosenPlayers[id] = 0
	g_bWillBeHealed[id] = false
}

public func_HLTVNewRound(id)
{
	new players[32], pnum, tempid
	get_players(players, pnum, "")
	
	for( new i; i<pnum; i++ ) 
	{
		tempid = players[i]
		g_iUsed[tempid] = 0
	}
}

public zp_extra_item_selected(id, ItemID)
{
	if(ItemID == g_iItemID )
	{
		if(g_iUsed[id] <= get_pcvar_num(cvar_limit))
			func_OpenPlayerChoose(id)
	}
}

public func_OpenPlayerChoose(id)
{
	g_iUsed[id]++
	
	static szMenuTitle[64]
	formatex(szMenuTitle, charsmax(szMenuTitle), "Add a player to be healed from you - %i / %i",g_iChosenPlayers[id], get_pcvar_num(cvar_maxplayers))
	
	new menu = menu_create(szMenuTitle, "func_OpenPlayerChoose_handler"); 
	
	new players[32], pnum, tempid
	new szName[32], szTempid[10]
	
	get_players(players, pnum, "ae", "TERRORIST")
	for( new i; i<pnum; i++ ) 
	{
		tempid = players[i]
		
		if(!g_bWillBeHealed[tempid])
		{
			get_user_name(tempid, szName, 31)
			num_to_str(tempid, szTempid, 9);
			menu_additem(menu, szName, szTempid, 0)
		}
	} 
	
	menu_display(id, menu) 
	return PLUGIN_HANDLED
}

public func_OpenPlayerChoose_handler(id, menu, item)
{
	if(item == MENU_EXIT || !is_user_alive(id))
	{
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	
	new data[6], access, callback, szName[32]
	
	menu_item_getinfo(menu, item, access, data, charsmax(data), szName, charsmax(szName), callback)
	
	g_bWillBeHealed[str_to_num(data)] = true
	g_iChosenPlayers[id]++
	
	if(g_iChosenPlayers[id] == get_pcvar_num(cvar_maxplayers) || g_iChosenPlayers[id] >= get_non_healed_players())
		func_GottaHealThemAll(id)
	else
		func_OpenPlayerChoose(id)
	
	return PLUGIN_HANDLED
}

public func_GottaHealThemAll(id)
{
	new players[32], pnum, tempid
	get_players(players, pnum, "ae", "TERRORIST")
	for( new i; i<pnum; i++ ) 
	{ 
		tempid = players[i]
		if(g_bWillBeHealed[tempid])
		{
			set_user_health(tempid, get_user_health(tempid) + get_pcvar_num(cvar_hp))
			flash_user_screen_red(tempid, 150)
			client_print(tempid, print_chat, "%s You have been healed from a Heal Rain.", PREFIX)
			g_bWillBeHealed[tempid] = false
		}
	}
}

stock flash_user_screen_red(id, duration)
{    
    message_begin(MSG_ONE_UNRELIABLE, msgScreenFade, {0,0,0}, id)
    write_short(duration * 100)   
    write_short(0)      
    write_short(0)       
    write_byte(255)    
    write_byte(0)    
    write_byte(0)    
    write_byte(duration)    
    message_end()
}  

stock get_non_healed_players()
{
	new iNonHealedPlayers
	
	new players[32], pnum, tempid
	get_players(players, pnum, "ae", "TERRORIST")
	for( new i; i<pnum; i++ ) 
	{ 
		tempid = players[i]
		if(!g_bWillBeHealed[tempid])
			iNonHealedPlayers++
	}
	
	return iNonHealedPlayers
}
