#include "cas_casinoinc"


void main()
{
    object oPC = GetPlaceableLastClickedBy();
    object oClickedObject = OBJECT_SELF;

    // Prevent queued movement or actions from player clicking
    SetCommandable(FALSE, oPC);
    DelayCommand(0.2, SetCommandable(TRUE, oPC)); // Re-enable after short delay

    ClearAllActions(TRUE); // Optional: force-stop anything already queued

    EnsureTokenPouch(oPC);

    // ...rest of your script
}


    // Check for creatures with specific tags in the area
    object oArea = GetArea(oPC);
    object oCreature = GetFirstObjectInArea(oArea);
    while (GetIsObjectValid(oCreature))
    {
        if (GetObjectType(oCreature) == OBJECT_TYPE_CREATURE)
        {
            string sTag = GetTag(oCreature);
            if (sTag == "CAS_ARENARED" || sTag == "CAS_ARENABLACK")
            {
                SendMessageToPC(oPC, "You cannot bet if there is a monster currently in the arena.");
                return;
            }
        }
        oCreature = GetNextObjectInArea(oArea);
    }
    
    // Check for the handle in the player's inventory
    object oHandle = GetItemPossessedBy(oPC, "I_CAS_CASINOHANDLE");
    if (!GetIsObjectValid(oHandle)) 
    {
        SendMessageToPC(oPC, "You do not have the necessary handle item.");
        return; // Abort if no handle
    }

    // Get bet amount and existing bets
    int nBetAmount = GetLocalInt(oClickedObject, "BET_AMOUNT"); 
    int nExistingBetRed = GetLocalInt(oPC, "I_BET_ON_RED");
    int nExistingBetBlack = GetLocalInt(oPC, "I_BET_ON_BLACK");

    if (nExistingBetRed > 0 || nExistingBetBlack > 0)
    {
        SendMessageToPC(oPC, "You have an existing bet. Resolve it before placing a new one.");
        return;
    }

    if (nBetAmount <= 0)
    {
        SendMessageToPC(oPC, "Invalid bet amount.");
        return;
    }

    // Determine the bet color variable based on the tag of the clicked object
    string sColorBetVar = "";
    if (GetTag(oClickedObject) == "BET_ON_RED") { sColorBetVar = "I_BET_ON_RED"; }
    else if (GetTag(oClickedObject) == "BET_ON_BLACK") { sColorBetVar = "I_BET_ON_BLACK"; }
    else 
    {
        SendMessageToPC(oPC, "Invalid betting option.");
        return;
    }

    // Check player's token count
    int nCurrentTokens = GetLocalInt(oHandle, "nCAS_TOKEN");
    if (nCurrentTokens >= nBetAmount)
    {
        // Deduct the bet amount from the player's tokens
        SetLocalInt(oHandle, "nCAS_TOKEN", nCurrentTokens - nBetAmount);

        // Set the bet color and amount
        SetLocalInt(oPC, sColorBetVar, nBetAmount);

        SendMessageToPC(oPC, "You have placed a bet of " + IntToString(nBetAmount) + " tokens on " + ((sColorBetVar == "I_BET_ON_RED") ? "Red" : "Black") + ". Remaining tokens: " + IntToString(nCurrentTokens - nBetAmount));
    }
    else
    {
        SendMessageToPC(oPC, "You do not have enough tokens to place this bet.");
    }
}
