// Define constants
const int GOLD_COST_PER_PIZZA = 250; // Cost of a pizza
const float PIZZA_PREP_DELAY = 60.0f; // 2 minutes in seconds for each step
const string RESREF_ORDER_NUMBER_ITEM = "cas_ordernum"; // ResRef of the order number item
const string RESREF_PIZZA_ITEM = "cas_custompizza"; // ResRef of the custom pizza item

// Function to notify the player that their pizza is ready and spawn the pizza item with the custom toppings
void PizzaReady(object oPC, int nOrderNumber, string sPizzaToppings)
{
    object oArea = GetArea(oPC); // Assuming the spawner is in the same area as the player
    object oPizzaSpawner = GetFirstObjectInArea(oArea);
    while (GetIsObjectValid(oPizzaSpawner))
    {
        if (GetTag(oPizzaSpawner) == "CAS_PIZZASPAWNER")
        {
            break;
        }
        oPizzaSpawner = GetNextObjectInArea(oArea);
    }

    if (!GetIsObjectValid(oPizzaSpawner))
    {
        return; // Spawner not found
    }

    // Create the custom pizza item
    object oPizzaItem = CreateItemOnObject(RESREF_PIZZA_ITEM, oPizzaSpawner);
    if (GetIsObjectValid(oPizzaItem))
    {
        // Set the name and description of the pizza to include the order number and toppings
        SetName(oPizzaItem, "Custom Pizza Order #" + IntToString(nOrderNumber));
        string sDescription = "This pizza contains the following toppings: " + sPizzaToppings;
        SetDescription(oPizzaItem, sDescription);
    }
}

// Function to handle pizza order and process custom toppings
void OrderPizza(object oPC)
{
    // Get the NPC handling the orders (C_CAS_PIZZANPC)
    object oOrderNPC = GetObjectByTag("C_CAS_PIZZANPC");

    // Check if the player has enough gold
    if (GetGold(oPC) < GOLD_COST_PER_PIZZA)
    {
        SendMessageToPC(oPC, "You do not have enough gold to order this pizza.");
        return;
    }

    // Charge the player for the pizza
    TakeGoldFromCreature(GOLD_COST_PER_PIZZA, oPC, TRUE);

    // Get the current order number and increment it
    int nCurrentOrderNumber = GetLocalInt(oOrderNPC, "nNumberOfOrders") + 1;
    SetLocalInt(oOrderNPC, "nNumberOfOrders", nCurrentOrderNumber);

    // Initialize the toppings list
    string sToppingsList = "";

    // Fetch the stored toppings from C_CAS_PIZZANPC (the order NPC)
    int i;
    for (i = 1; i <= 5; i++) // Loop through sTopping1 to sTopping5
    {
        string sTopping = GetLocalString(oOrderNPC, "sTopping" + IntToString(i));
        if (sTopping != "")
        {
            sToppingsList += sTopping;
            if (i < 5 && GetLocalString(oOrderNPC, "sTopping" + IntToString(i + 1)) != "")
            {
                sToppingsList += ", "; // Add a comma separator unless it's the last topping
            }
        }
    }

    // Debug message to confirm toppings list
    SendMessageToPC(oPC, "Your selected toppings are: " + sToppingsList);

    // Announce the order
    AssignCommand(oOrderNPC, SpeakString("Custom pizza order! Toppings: " + sToppingsList, TALKVOLUME_TALK));

    // NPC actions during pizza preparation
    object oPrepChef = GetObjectByTag("CAS_PREP_CHEF");
    if (GetIsObjectValid(oPrepChef))
    {
        DelayCommand(PIZZA_PREP_DELAY * 1.0, AssignCommand(oPrepChef, ActionSpeakString("[Adds the following ingredients to the pizza: " + sToppingsList + "].", TALKVOLUME_TALK)));
    }

    object oDarg = GetObjectByTag("cas_DargtheDretch");
    if (GetIsObjectValid(oDarg))
    {
        DelayCommand(PIZZA_PREP_DELAY * 2.0, AssignCommand(oDarg, ActionSpeakString("[Shoves the prepared pizza into the oven.]", TALKVOLUME_TALK)));
        DelayCommand(PIZZA_PREP_DELAY * 2.5, AssignCommand(oDarg, ActionSpeakString("[Yanks the pizza from the oven, pushing it through a hole in the wall into the display for the customer]", TALKVOLUME_TALK)));
    }

    // Final announcement and pizza spawn, passing the selected toppings for the pizza description
    DelayCommand(PIZZA_PREP_DELAY * 2.6, AssignCommand(oOrderNPC, PizzaReady(oPC, nCurrentOrderNumber, sToppingsList)));
}

// Function to store topping selections on C_CAS_PIZZANPC
void StoreToppingsOnNPC(object oPC, string sTopping)
{
    object oOrderNPC = GetObjectByTag("C_CAS_PIZZANPC");
    int nToppingCount = GetLocalInt(oOrderNPC, "ToppingCount");

    // Check if the topping count has reached the maximum limit
    if (nToppingCount >= 5)
    {
        SendMessageToPC(oPC, "You can only select up to 5 toppings.");
        return;
    }

    nToppingCount++; // Increment topping count
    SetLocalString(oOrderNPC, "sTopping" + IntToString(nToppingCount), sTopping); // Store topping on NPC
    SetLocalInt(oOrderNPC, "ToppingCount", nToppingCount); // Update topping count on NPC

    SendMessageToPC(oPC, sTopping + " added to your pizza.");
}

// Main function to start the pizza order
void main()
{
    object oPC = GetFirstPC(); // Get the player character
    SendMessageToPC(oPC, "cas_makepizza script called successfully.");

    // Example of ordering a pizza (assuming toppings are already stored)
    OrderPizza(oPC);
}
