#include <sourcemod>
#include <shop>

enum BetType
{
    Bet_None,
    Bet_Color,
    Bet_Dozen,
    Bet_Number,
    Bet_Column,
    Bet_Row
};

int g_PlayerBet[MAXPLAYERS + 1];       // How much player has bet
int g_PlayerBetType[MAXPLAYERS + 1];   // What kind of bet player has made
int g_PlayerBetValue[MAXPLAYERS + 1];  // The value player has bet on
bool g_PlayerHasBet[MAXPLAYERS + 1] = { false, ... }; // Whether player has bet or not

public Plugin myinfo =
{
    name = "Roulette Plugin",
    author = "anon",
    description = "A simple roulette plugin for CS:Source",
    version = "1.0",
    url = "https://www.example.com"
};

public void OnPluginStart()
{
    RegConsoleCmd("sm_roul", Command_Roul);
}

public Action Command_Roul(int client, int args)
{
    if (!IsClientInGame(client))
    {
        return Plugin_Handled;
    }

    ShowRouletteMenu(client);
    return Plugin_Handled;
}


public void ShowRouletteMenu(int client)
{
    Menu menu = CreateMenu(MenuHandler_Roulette);

    menu.SetTitle("Выберите ставку");
    menu.AddItem("color", "На цвет");
    menu.AddItem("dozen", "На дюжину");
    menu.AddItem("number", "На число");
    menu.AddItem("column", "На колонну");
    menu.AddItem("row", "На столбец");

    menu.Display(client, 0);
}
public void SelectColorMenu(int client)
{
    Menu menu = CreateMenu(MenuHandler_SelectColor);
    menu.SetTitle("Выберите цвет");
    menu.AddItem("red", "Красный");
    menu.AddItem("black", "Черный");
    menu.Display(client, 0);
}

public void SelectDozenMenu(int client)
{
    Menu menu = CreateMenu(MenuHandler_SelectDozen);
    menu.SetTitle("Выберите дюжину");
    menu.AddItem("dozen1", "1-12");
    menu.AddItem("dozen2", "13-24");
    menu.AddItem("dozen3", "25-36");
    menu.Display(client, 0);
}

public void SelectNumberMenu(int client)
{
    Menu menu = CreateMenu(MenuHandler_SelectNumber);
    menu.SetTitle("Выберите число");
    for (int i = 0; i <= 36; i++)
    {
        char num[4];
        IntToString(i, num, sizeof(num));
        menu.AddItem(num, num);
    }
    menu.Display(client, 0);
}

public void SelectColumnMenu(int client)
{
    Menu menu = CreateMenu(MenuHandler_SelectColumn);
    menu.SetTitle("Выберите колонну");
    menu.AddItem("col1", "Колонна 1");
    menu.AddItem("col2", "Колонна 2");
    menu.AddItem("col3", "Колонна 3");
    menu.Display(client, 0);
}

public void SelectRowMenu(int client)
{
    Menu menu = CreateMenu(MenuHandler_SelectRow);
    menu.SetTitle("Выберите строку");
    for (int i = 1; i <= 12; i++)
    {
        char num[4];
        IntToString(i, num, sizeof(num));
        menu.AddItem(num, num);
    }
    menu.Display(client, 0);
}

public int MenuHandler_Roulette(Menu menu, MenuAction action, int client, int param)
{
    if (action == MenuAction_End)
    {
        menu.Close();
    }
    else if (action == MenuAction_Select)
    {
        char selection[32];
        menu.GetItem(param, selection, sizeof(selection));

        if (StrEqual(selection, "color"))
        {
            SelectColorMenu(client);
        }
        else if (StrEqual(selection, "dozen"))
        {
            SelectDozenMenu(client);
        }
        else if (StrEqual(selection, "number"))
        {
            SelectNumberMenu(client);
        }
        else if (StrEqual(selection, "column"))
        {
            SelectColumnMenu(client);
        }
        else if (StrEqual(selection, "row"))
        {
            SelectRowMenu(client);
        }
    }
    return 0;
}

// Определение функций выше

public Action Command_Bet(int client, int args)
{
    if (!IsClientInGame(client) || args < 1)
    {
        return Plugin_Handled;
    }

    char arg[32];
    GetCmdArg(1, arg, sizeof(arg));

    int bet = StringToInt(arg);
    int credits = Shop_GetClientCredits(client);

    if (bet <= 0 || bet > credits)
    {
        PrintToChat(client, "Некорректная ставка.");
        return Plugin_Handled;
    }

    g_PlayerBet[client] = bet;
    g_PlayerHasBet[client] = true;
    Shop_SetClientCredits(client, credits - bet);

    PrintToChatAll("Игрок %N поставил %d кредитов.", client, bet);

    CreateTimer(5.0, Timer_RouletteEnd);

    return Plugin_Handled;
}

public int MenuHandler_SelectColor(Menu menu, MenuAction action, int client, int param)
{
    if (action == MenuAction_End)
    {
        menu.Close();
    }
    else if (action == MenuAction_Select)
    {
        g_PlayerBetType[client] = BetType(Bet_Color);
        char selection[32];
        menu.GetItem(param, selection, sizeof(selection));
        g_PlayerBetValue[client] = StrEqual(selection, "red") ? 1 : 2;
        PrintToChat(client, "Введите свою ставку в чат (Пример: !ставка 50)");

        // Register a chat command to handle the bet
        RegConsoleCmd("sm_bet", Command_Bet);
    }
    return 0;
}

public int MenuHandler_SelectDozen(Menu menu, MenuAction action, int client, int param)
{
    if (action == MenuAction_End)
    {
        menu.Close();
    }
    else if (action == MenuAction_Select)
    {
        g_PlayerBetType[client] = BetType(Bet_Dozen);
        g_PlayerBetValue[client] = param + 1;
        PrintToChat(client, "Введите свою ставку в чат (Пример: !ставка 50)");
        RegConsoleCmd("sm_bet", Command_Bet);
    }
    return 0;
}

public int MenuHandler_SelectNumber(Menu menu, MenuAction action, int client, int param)
{
    if (action == MenuAction_End)
    {
        menu.Close();
    }
    else if (action == MenuAction_Select)
    {
        g_PlayerBetType[client] = BetType(Bet_Number);
        char number[4];
        menu.GetItem(param, number, sizeof(number));
        g_PlayerBetValue[client] = StringToInt(number);
        PrintToChat(client, "Введите свою ставку в чат (Пример: !ставка 50)");
        RegConsoleCmd("sm_bet", Command_Bet);
    }
    return 0;
}

public int MenuHandler_SelectColumn(Menu menu, MenuAction action, int client, int param)
{
    if (action == MenuAction_End)
    {
        menu.Close();
    }
    else if (action == MenuAction_Select)
    {
        g_PlayerBetType[client] = BetType(Bet_Column);
        g_PlayerBetValue[client] = param + 1;
        PrintToChat(client, "Введите свою ставку в чат (Пример: !ставка 50)");
        RegConsoleCmd("sm_bet", Command_Bet);
    }
    return 0;
}

public int MenuHandler_SelectRow(Menu menu, MenuAction action, int client, int param)
{
    if (action == MenuAction_End)
    {
        menu.Close();
    }
    else if (action == MenuAction_Select)
    {
        g_PlayerBetType[client] = BetType(Bet_Row);
        g_PlayerBetValue[client] = param + 1;
        PrintToChat(client, "Введите свою ставку в чат (Пример: !ставка 50)");
        RegConsoleCmd("sm_bet", Command_Bet);
    }
    return 0;
}

public Action Timer_RouletteEnd(Handle timer)
{
    int result = GetRandomInt(0, 36);

    PrintToChatAll("Рулетка прокрутилась! Выпало число: %d", result);

    for (int i = 1; i <= MaxClients; i++)
    {
        if (g_PlayerHasBet[i])
        {
            bool won = false;
            int payout = 0;

            switch (g_PlayerBetType[i])
            {
                case Bet_Color:
                {
                    if ((g_PlayerBetValue[i] == 1 && IsRed(result)) ||
                        (g_PlayerBetValue[i] == 2 && !IsRed(result)))
                    {
                        won = true;
                        payout = 2 * g_PlayerBet[i];
                    }
                    break;
                }

                case Bet_Dozen:
                {
                    if (ResultMatchesDozen(result, g_PlayerBetValue[i]))
                    {
                        won = true;
                        payout = 3 * g_PlayerBet[i];
                    }
                    break;
                }

                case Bet_Number:
                {
                    if (result == g_PlayerBetValue[i])
                    {
                        won = true;
                        payout = 36 * g_PlayerBet[i];
                    }
                    break;
                }

                case Bet_Column:
                {
                    if (ResultMatchesColumn(result, g_PlayerBetValue[i]))
                    {
                        won = true;
                        payout = 3 * g_PlayerBet[i];
                    }
                    break;
                }

                case Bet_Row:
                {
                    if (ResultMatchesRow(result, g_PlayerBetValue[i]))
                    {
                        won = true;
                        payout = 3 * g_PlayerBet[i];
                    }
                    break;
                }
            }

            if (won)
            {
                PrintToChat(i, "Вы выиграли! Ваш выигрыш: %d кредитов.", payout);
                Shop_SetClientCredits(i, Shop_GetClientCredits(i) + payout);
            }
            else
            {
                PrintToChat(i, "Вы проиграли.");
            }

            g_PlayerHasBet[i] = false;
            g_PlayerBet[i] = 0;
            g_PlayerBetType[i] = Bet_None;
            g_PlayerBetValue[i] = 0;
        }
    }

    return Plugin_Continue;
}

const int RED_NUMBERS_COUNT = 18;
int redNumbers[RED_NUMBERS_COUNT] = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36};

bool IsRed(int number)
{
    for (int i = 0; i < RED_NUMBERS_COUNT; i++)
    {
        if (redNumbers[i] == number)
        {
            return true;
        }
    }
    return false;
}

bool ResultMatchesDozen(int result, int dozen)
{
    switch (dozen)
    {
        case 1:
            return result >= 1 && result <= 12;
        case 2:
            return result >= 13 && result <= 24;
        case 3:
            return result >= 25 && result <= 36;
        default:
            return false;
    }
}

bool ResultMatchesColumn(int result, int column)
{
    // Example implementation; adjust as per actual column rules
    switch (column)
    {
        case 1:
            return (result % 3 == 1);
        case 2:
            return (result % 3 == 2);
        case 3:
            return (result % 3 == 0);
        default:
            return false;
    }
}

bool ResultMatchesRow(int result, int row)
{
    // Example implementation; adjust as per actual row rules
    return ((result - 1) / 3) + 1 == row;
}