#include "cas_casinoinc"

void main() {
    // Get the player character involved in the conversation
    object oPC = GetPCSpeaker();
        EnsureTokenPouch(oPC);
    // Ensure the player has the handle
    object oHandle = GetItemPossessedBy(oPC, "I_CAS_HANDLE");
    if (!GetIsObjectValid(oHandle)) {
        SendMessageToPC(oPC, "You do not have the necessary handle item.");
        return; // Abort if no handle
    }

    // Check if the returned object is valid
    if (GetIsObjectValid(oPC)) {
        int nCost = 1000; // Cost for 1 token

        if (GetGold(oPC) >= nCost) {
            TakeGoldFromCreature(nCost, oPC, TRUE);
            
            // Increase nCAS_TOKEN by 1
            int nTokens = GetLocalInt(oHandle, "nCAS_TOKEN");
            SetLocalInt(oHandle, "nCAS_TOKEN", nTokens + 1);

            SendMessageToPC(oPC, "You have purchased a token. Total tokens: " + IntToString(nTokens + 1));

        } else {
            FloatingTextStringOnCreature("You do not have enough gold.", oPC, FALSE);
        }
    } else {
        // Optionally handle the error case if needed
        SendMessageToPC(oPC, "Error: Invalid object in conversation.");
    }
}
