#include "cas_casinoinc"

void main() {
    // Get the player character involved in the conversation
    object oPC = GetPCSpeaker();
    EnsureTokenPouch(oPC);
    // Check if the returned object is valid
    if (GetIsObjectValid(oPC)) {
        int nCost = 100000; // Cost for 100 tokens

        // Ensure the player has the handle
        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
        }

        if (GetGold(oPC) >= nCost) {
            TakeGoldFromCreature(nCost, oPC, TRUE);
            
            // Increase n_CAS_TOKEN by 1000
            int nTokens = GetLocalInt(oHandle, "nCAS_TOKEN");
            SetLocalInt(oHandle, "nCAS_TOKEN", nTokens + 100);
            
            string sPlayerName = GetName(oPC);
            string sCDKey = GetPCPublicCDKey(oPC, TRUE); // TRUE for short version of CD key
            string sLogMessage = sPlayerName + " (" + sCDKey + ") bought 100 token for " + IntToString(nCost) + " gold. Total tokens now: " + IntToString(nTokens + 100);
            LogDebug(sLogMessage, "cas");

            SendMessageToPC(oPC, "You have purchased 100 tokens. Total tokens: " + IntToString(nTokens + 100));
        } 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.");
    }
}
