#include <amxmodx>
#include <cstrike>
#include <next21_DR_core>
#include <WPMGPrintChatColor>

#define	PLUGIN	"Money Transferer"
#define	VERSION	"1.3 (Next21 Edited)"
#define	AUTHOR	"kalash1k"

#define	BASE_MONEY_AMOUNT	1000.0		// Need to be a float for calculations
#define MAX_CURRENT_MONEY	500		// It's strongly recommended to not change these two values

#define TAG	"Денежный перевод"

new players_menu, money_menu, players[32], num, i
new accessmenu, sName[64], callback
new msg[128]

new money_coef, money_amount

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_clcmd("say /tm", "transfer_menu", ADMIN_ALL, "")	// TM - Transfer Money
	register_clcmd("say /transfer", "transfer_menu", ADMIN_ALL, "")
	register_clcmd("say /money", "transfer_menu", ADMIN_ALL, "")
	register_clcmd("say_team /tm", "transfer_menu", ADMIN_ALL, "")
	register_clcmd("say_team /transfer", "transfer_menu", ADMIN_ALL, "")
	register_clcmd("say_team /money", "transfer_menu", ADMIN_ALL, "")
	
	register_clcmd("transfer", "transfer_money", ADMIN_ALL, "")
}

public transfer_menu(id)
{
	get_players(players, num, "h")
	
	if (num <= 1)
	{
		PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tНекому передавать деньги", TAG) 	
		return PLUGIN_HANDLED
	}
	
	players_menu = menu_create("Игроки", "players_menu_handler")
	
	new tempname[32], info[10]
	
	for(i = 0; i < num; i++)
	{
		if(players[i] == id)
			continue
		
		get_user_name(players[i], tempname, 31)
		num_to_str(players[i], info, 9)
		menu_additem(players_menu, tempname, info, 0)
	}
	
	menu_setprop(players_menu, MPROP_EXIT, MEXIT_ALL)
	menu_setprop(players_menu, MPROP_EXITNAME, "Выход")
	menu_setprop(players_menu, MPROP_BACKNAME, "Назад")
	menu_setprop(players_menu, MPROP_NEXTNAME, "Далее")
	
	menu_display(id, players_menu, 0)
	return PLUGIN_CONTINUE
}

public players_menu_handler(id, players_menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(players_menu)
		return PLUGIN_HANDLED
	}
	
	new sData[6]	// target id
	
	menu_item_getinfo(players_menu, item, accessmenu, sData, 5, sName, 63, callback)
	
	money_menu = menu_create("Сколько?", "money_menu_handler")
	
	new user_money = cs_get_user_money(id)
	new str[6]
	
	if(user_money > MAX_CURRENT_MONEY)
	{

		money_coef = floatround(float(user_money) / BASE_MONEY_AMOUNT)
			
		for (new i = 1; i < 5; i++)
		{
			if(i == 4)
				money_amount = 500 * money_coef
			else
				money_amount = 100 * i * money_coef
				
			num_to_str(money_amount, str, 5)
			menu_additem(money_menu, str, sData, 0)
		}
	}
	
	num_to_str(user_money, str, 5)
	menu_additem(money_menu, str, sData, 0)
	
	menu_additem(money_menu, "Ввести суму", sData, 0)
	
	menu_setprop(money_menu, MPROP_EXIT, MEXIT_ALL)
	menu_setprop(money_menu, MPROP_EXITNAME, "Назад")
	
	menu_display(id, money_menu, 0)
	return PLUGIN_CONTINUE
}

public money_menu_handler(id, money_menu, item)
{
	if(item == MENU_EXIT)
	{
		menu_destroy(money_menu)
		menu_display(id, players_menu, 0)
		return PLUGIN_CONTINUE
	}
	
	new sData[6] //target id
	
	menu_item_getinfo(money_menu, item, accessmenu, sData, 5, sName, 63, callback)
	
	sData[0] = str_to_num(sData)
	
	new items = menu_items(money_menu)
	
	if(items < 5)
	{
		switch(item)
		{
			case 0:
			{
				new iName = str_to_num(sName) // money amount
				formatex(msg, 127, "transfer %i %i", sData[0], iName)
			}
			case 1:
			{
				formatex(msg, 127, "messagemode ^"transfer %i^"", sData[0])
			}
		}
	}
	else
	{
		switch(item)
		{
			case 0:
			{
				new iName = str_to_num(sName) // money amount
				formatex(msg, 127, "transfer %i %i", sData[0], iName)
			}
			case 1:
			{
				new iName = str_to_num(sName)
				formatex(msg, 127, "transfer %i %i", sData[0], iName)
			}
			case 2:
			{
				new iName = str_to_num(sName)
				formatex(msg, 127, "transfer %i %i", sData[0], iName)
			}
			case 3:
			{
				new iName = str_to_num(sName)
				formatex(msg, 127, "transfer %i %i", sData[0], iName)
			}
			case 4:
			{
				new iName = str_to_num(sName)
				formatex(msg, 127, "transfer %i %i", sData[0], iName)
			}
			case 5:
				formatex(msg, 127, "messagemode ^"transfer %i^"", sData[0])
		}
	}
	
	client_cmd(id, msg)
	
	return PLUGIN_CONTINUE
}

public transfer_money(id)
{
	new param[6]
	
	read_argv(1, param, 5)
	
	new target = str_to_num(param)
	
	if(target == id)
	{
		PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tНельзя передать деньги самому себе", TAG) 	
		return PLUGIN_HANDLED
	}
	
	if(!is_user_connected(target))
	{
		PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tТакого игрока нет на сервере", TAG)
		return PLUGIN_HANDLED
	}
	
	read_argv(2, param, 5)
	
	new p_len = strlen(param)
	
	for (new x; x < p_len; x++)
	{
		if(!isdigit(param[x]))
		{
			PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tНеверное число", TAG)
			return PLUGIN_HANDLED
		}
	}
	
	new amount = str_to_num(param)
	
	if(amount <= 0)
	{
		PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tНеверное число", TAG)
		return PLUGIN_HANDLED
	}
	
	new user_money = cs_get_user_money(id)
	
	if (user_money < amount)
	{
		PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tУ вас не хватает денег", TAG)
		return PLUGIN_HANDLED
	}
	
	new target_money = cs_get_user_money(target)
	new target_name[32]
	
	get_user_name(target, target_name, 31)
	
	if(target_money == n21_dr_get_maxmoney())
	{
		PrintChatColor(id, PRINT_COLOR_RED, "!g[%s] !tУ игрока ^4%s ^3уже ^4$%i", TAG, target_name, n21_dr_get_maxmoney())
		return PLUGIN_HANDLED
	}
	
	new user_name[32]
	
	get_user_name(id, user_name, 31)
	
	new maxmoney = n21_dr_get_maxmoney() - target_money
	
	if(maxmoney >= amount)
	{
		cs_set_user_money(id, user_money - amount, 1)
		cs_set_user_money(target, target_money + amount, 1)
		
		PrintChatColor(0, PRINT_COLOR_BLUE, "!g[%s] !t^1Игрок ^3%s ^1передал ^4$%i ^3%s", TAG, user_name, amount, target_name)
		
		return PLUGIN_HANDLED
	}
	
	cs_set_user_money(id, maxmoney, 1)
	cs_set_user_money(target, n21_dr_get_maxmoney(), 1)
	
	PrintChatColor(0, PRINT_COLOR_BLUE, "!g[%s] !t^1Игрок ^3%s ^1передал ^4$%i ^3%s", TAG, user_name, maxmoney, target_name)
	
	return PLUGIN_HANDLED
}
