#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <nvault>
#include <next21_DR_core>
#include <next21_DR_secondary>

#define PLUGIN_NAME	"Life_Menu"
#define PLUGIN_VERSION	"0.1 (Next21 Edited)"
#define PLUGIN_AUTHOR	"Bos93"

#define MAX_STATS_SAVED			64

#if cellbits == 						32 
    #define OFFSET_CSMONEY 		115 
#else 
    #define OFFSET_CSMONEY 		140 
#endif 

#define OFFSET_LINUX 				5

new g_Life[33]
new gSteamID[33]
new vKey[MAX_STATS_SAVED]
new vData[MAX_STATS_SAVED]

new gVault

public plugin_init ( )
{
	register_plugin ( PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR )
	
	register_clcmd("say /life", "Life_Shop")
	register_clcmd("say_team /life", "Life_Shop")
	register_clcmd("life", "Life_Shop")
}

public client_connect(id)
	load_client_Life(id)
	
public client_disconnect(id)
	save_client_Life(id)
	
public Life_Shop(id) 
{
	new szText[ 1024 char ]
		
	formatex(szText, charsmax( szText ), "\yВаши деньги\w: \r%d\y $^nВаши жизни\w: \r%d" ,fm_cs_get_user_money(id) , g_Life[id] )
	
	new menu = menu_create(szText, "Life_handler" )
			
	formatex( szText, charsmax( szText ), "Купить жизнь \y-  \w%d\y $" ,n21_dr_get_lifecost())
	menu_additem( menu, szText, "1", 0 )
	
	
	formatex( szText, charsmax( szText ), "Продать жизнь \y+ \w%d\y $" ,n21_dr_get_lifesell())
	menu_additem( menu, szText, "2", 0 )
		
	if(!n21_in_duel() && !is_user_alive(id) && g_Life[id])
		formatex( szText, charsmax( szText ), "Использовать жизнь \y- \r1\w Life" )
	else
		formatex( szText, charsmax( szText ), "\dИспользовать жизнь - 1 Life" )
		
	menu_additem( menu, szText, "3", 0 )

	menu_setprop(menu, MPROP_EXITNAME, "Выход")
	
	menu_display(id, menu, 0)
 }
	
public Life_handler(id, menu, item)
{
	if (item == MENU_EXIT)
	{
		client_cmd(id,"menu")
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}

	new szName[32]
	get_user_name(id, szName, 31)

	new money = fm_cs_get_user_money(id)
	
	switch(item)
	{
			case 0:
			{
				if(money < n21_dr_get_lifecost())
				{
					Color_Print(id, "!g[Меню жизней] !team%s !yу вас недостаточно !gденег." ,  szName )
					return PLUGIN_HANDLED
				}
				
				if(g_Life[id] >= n21_dr_get_lifelimit())
				{
					Color_Print(id, "!g[Меню жизней] !team%s !yу вас достигнут !gлимит." ,  szName )
					return PLUGIN_HANDLED
				}
				
				Color_Print(id, "!g[Меню жизней] !team%s !yвы купили !gжизнь." ,  szName )
				
				g_Life[id] += 1
			
				set_fix_money(id,0-n21_dr_get_lifecost())
				Life_Shop(id) 
			}
			case 1:
			{
				if ( g_Life[id] == 0 )
				{
					Color_Print(id, "!g[Меню жизней] !team%s !yу вас нет !gжизней.", szName)
					return PLUGIN_HANDLED
				}
				
				Color_Print(id, "!g[Меню жизней] !team%s !yвы продали !gжизнь.", szName)
				
				g_Life[id] -= 1
					
				set_fix_money(id, n21_dr_get_lifesell())
				Life_Shop(id) 
			}
			case 2:
			{
				if (is_user_alive(id))
				{
					Color_Print(id, "!g[Меню жизней] !team%s !yвы должны быть !gмертвы.", szName)
					return PLUGIN_HANDLED
				}
	
				if (g_Life[id] == 0)
				{
					Color_Print(id, "!g[Меню жизней] !team%s !yу вас нет !gжизней.", szName)
					return PLUGIN_HANDLED
				}
				
				if(n21_in_duel())
				{
					Color_Print(id, "!g[Меню жизней] !team%s !yсейчас ведеться !gдуэль." , szName)
					return PLUGIN_HANDLED
				}
				
				g_Life[id] -= 1

				Color_Print(id, "!g[Меню жизней] !team%s !yвы использовали !gвозрождение.", szName)
				
				ExecuteHam(Ham_CS_RoundRespawn, id)
			}
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}

stock save_client_Life( index )
{
	gVault = nvault_open( "Life_Save" )
	
	if( gVault == INVALID_HANDLE )
	{
		set_fail_state( "[Life_Shop] nValut ERROR: =-> Invalid-Handle" )
	}
	
	get_user_authid( index, gSteamID, charsmax( gSteamID ) )

	formatex( vKey, charsmax( vKey ), "%sLife", gSteamID )
	formatex( vData, charsmax( vData ), "%d", g_Life[ index ] )
	nvault_set( gVault, vKey, vData )
	nvault_close( gVault )
}

stock load_client_Life( index )
{
	gVault = nvault_open( "Life_Save" )
	
	if( gVault == INVALID_HANDLE )
	{
		set_fail_state( "[DrShop] nValut ERROR: =-> Invalid-Handle" )
	}
	
	get_user_authid( index, gSteamID, charsmax( gSteamID ) )
	
	formatex( vKey, charsmax( vKey ), "%sLife", gSteamID )
	g_Life[ index ] = nvault_get( gVault, vKey )
	nvault_close( gVault )
}

stock fm_cs_get_user_money(id) 
        return get_pdata_int(id, OFFSET_CSMONEY, OFFSET_LINUX)

stock set_fix_money(id,money)
{
	new MAX_MONEY = n21_dr_get_maxmoney()
	new curr_money = get_pdata_int(id, OFFSET_CSMONEY)
	if(curr_money+money > MAX_MONEY)
		money = MAX_MONEY-curr_money
		
	if(curr_money+money < 0)
		money = 0-curr_money
			
	set_pdata_int(id, OFFSET_CSMONEY, curr_money+money, OFFSET_LINUX)
			
	message_begin(MSG_ONE, get_user_msgid("Money"), _, id)
	write_long(curr_money+money)
	write_byte(1)
	message_end()
	
	return money
}
	
stock Color_Print(const id, const input[], any:...)
{
	new count = 1, players[32]
	static msg[191]
	vformat(msg, 190, input, 3)
	
	replace_all(msg, 190, "!g", "^4")
	replace_all(msg, 190, "!y", "^1")
	replace_all(msg, 190, "!team", "^3")
	
	if (id) players[0] = id; else get_players(players, count, "ch")
	{
		for (new i = 0; i < count; i++)
		{
			if (is_user_connected(players[i]))
			{
				message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i])
				write_byte(players[i]);
				write_string(msg);
				message_end();
			}
		}
	}
}
