// Combined NWScript for Pizzaria order system with order number tracking

// Define constants for order handling
const int GOLD_COST_PER_PIZZA = 350; // Cost of a pizza
const float PIZZA_PREP_DELAY = 120.0f; // 2 minutes in seconds for each step
const string RESREF_ORDER_NUMBER_ITEM = "cas_ordernum"; // ResRef of the order number item

// Function to notify the player that their order is ready and spawn the pizza item
void PizzaReady(object oPC, int nOrderNumber, string sPizzaResRef, string sPizzaName)
{
    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);
    }

    // Proceed with the rest of the script if spawner is found
    if (!GetIsObjectValid(oPizzaSpawner))
    {
        return;
    }

    // Original functionality
    object oPizzaItem = CreateItemOnObject(sPizzaResRef, oPizzaSpawner);
    if (GetIsObjectValid(oPizzaItem))
    {
        SetName(oPizzaItem, sPizzaName + " Order Number " + IntToString(nOrderNumber));
    }
}

// Function to handle pizza order
void OrderPizza(object oPC, string sPizzaName, string sPizzaResRef, string sPizzaIngredients)
{
    // Check if the player has enough gold
    if (GetGold(oPC) < GOLD_COST_PER_PIZZA)
    {
        // Not enough gold, notify player
        SendMessageToPC(oPC, "You do not have enough gold to order this pizza.");
        return;
    }

    // Charge the player
    TakeGoldFromCreature(GOLD_COST_PER_PIZZA, oPC, TRUE);

    // Get the NPC handling the orders
    object oOrderNPC = GetObjectByTag("C_CAS_PIZZANPC");
    
    // Get the current order number from the NPC
    int nCurrentOrderNumber = GetLocalInt(oOrderNPC, "nNumberOfOrders") + 1;

    // Update the order count on the NPC
    SetLocalInt(oOrderNPC, "nNumberOfOrders", nCurrentOrderNumber);

    // Give the player an order number item
    object oOrderItem = CreateItemOnObject(RESREF_ORDER_NUMBER_ITEM, oPC);
    if (GetIsObjectValid(oOrderItem))
    {
        // Rename the order item to reflect the order number
        SetName(oOrderItem, "Order " + IntToString(nCurrentOrderNumber));
    }

    // Shout out order details
    AssignCommand(oOrderNPC, SpeakString("Order for a " + sPizzaName + "!", TALKVOLUME_TALK));

    // Various NPC actions during pizza preparation
    object oHugo = GetObjectByTag("cas_HugoHamantula");
    if (GetIsObjectValid(oHugo))
    {
        DelayCommand(PIZZA_PREP_DELAY * 0.5, AssignCommand(oHugo, SpeakString("Get to work on that Pizza Natalya! The customer is HUNGRY!", TALKVOLUME_TALK)));
    }

    object oFred = GetObjectByTag("CAS_PIZZA_FRED");
    if (GetIsObjectValid(oFred))
    {
        DelayCommand(PIZZA_PREP_DELAY * 1.0, AssignCommand(oFred, SpeakString("Making ORDER " + IntToString(nCurrentOrderNumber) + ", a " + sPizzaName, TALKVOLUME_TALK)));
    }

    object oPrepChef = GetObjectByTag("CAS_PREP_CHEF");
    if (GetIsObjectValid(oPrepChef))
    {
        DelayCommand(PIZZA_PREP_DELAY * 1.5, AssignCommand(oPrepChef, ActionSpeakString("[Grates a bunch of fresh cheese and " + sPizzaIngredients, 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, now passing the pizza name as well
    DelayCommand(PIZZA_PREP_DELAY * 2.6, AssignCommand(oOrderNPC, PizzaReady(oPC, nCurrentOrderNumber, sPizzaResRef, sPizzaName)));
}

// Main function to be called from dialogue
void main()
{
    object oPC = GetPCSpeaker(); // Get the player character speaking

    // Example for ordering a "Margherita" pizza
    // You can replace these values with different ones for different pizzas
    OrderPizza(oPC, "Pizza Natalya!!", "cas_pizzanat", "[While wearing thick gloves and goggles, chops up some Dracolich Peppers, Onion, large disc Pepperoni, and spreads Ghost Pepper Cheese,]");
}
