const int MAX_TOPPINGS = 5; // Maximum number of toppings allowed

// Function to return available toppings as a single string
string GetAvailableToppings()
{
    return "Pepperoni,Bacon,Sausage,Pineapple,Spinach,Peppers,Green Pepper,Yellow Pepper,Chipotles,Sardines,Tomatoes,Mushrooms,Olives,Onions,Ham,Jalapeņos,Jalapenos,Extra Cheese,Basil";
}

// Function to check if a topping is available
int IsToppingAvailable(string sTopping)
{
    string sAvailableToppings = GetAvailableToppings();
    int nPos = FindSubString(sAvailableToppings, sTopping);
    if (nPos != -1) return TRUE;
    return FALSE;
}

// Function to delete stored toppings from the NPC after a delay
void DeleteToppingsFromNPC(object oNPC)
{
    int i;
    for (i = 1; i <= MAX_TOPPINGS; i++)
    {
        DeleteLocalString(oNPC, "sTopping" + IntToString(i)); // Delete each stored topping
        DeleteLocalString(oNPC, "ToppingCount");
    }
    AssignCommand(oNPC, ActionSpeakString("Topping selections have been cleared.", TALKVOLUME_TALK));
}

// Function to process topping selection after the delay
void ProcessToppingInput(object oPC, string sSaid)
{
    // Get the NPC involved in the conversation
    object oNPC = GetObjectByTag("C_CAS_PIZZANPC");

    if (IsToppingAvailable(sSaid))
    {
        int nToppingCount = GetLocalInt(oNPC, "ToppingCount");

        // Check if the topping count has reached the maximum limit
        if (nToppingCount >= MAX_TOPPINGS)
        {
            AssignCommand(oNPC, ActionSpeakString("You can only select up to " + IntToString(MAX_TOPPINGS) + " toppings.", TALKVOLUME_TALK));
            return;
        }

        nToppingCount++; // Increment topping count
        SetLocalString(oNPC, "sTopping" + IntToString(nToppingCount), sSaid); // Store topping on NPC
        SetLocalInt(oNPC, "ToppingCount", nToppingCount); // Update topping count on NPC

        AssignCommand(oNPC, ActionSpeakString(sSaid + " added to your pizza. You now have " + IntToString(nToppingCount) + " toppings.", TALKVOLUME_TALK));
    }
    else if (sSaid == "finished" || sSaid == "Finished")
    {
        AssignCommand(oNPC, ActionSpeakString("You've finished selecting toppings. Proceeding to pizza creation.", TALKVOLUME_TALK));
        
        // Break the conversation
        AssignCommand(oPC, ActionPauseConversation());

        // Trigger pizza creation
        ExecuteScript("cas_makepizza", oPC); 
        
        // Set a delay of 10 seconds to delete the stored toppings from the NPC
        DelayCommand(10.0, DeleteToppingsFromNPC(oNPC)); 
    }
    else
    {
        AssignCommand(oNPC, ActionSpeakString("Invalid input. Please choose a valid topping or say 'finished' when you're done.", TALKVOLUME_TALK));
    }
}

// Main function to handle player chat input and store toppings with a delay
void main()
{
    object oPC = GetPCSpeaker(); // Get the player character speaking
    string sSaid = GetLocalString(oPC, "I_SAID"); // Get what the player said

    ProcessToppingInput(oPC, sSaid);
}
