using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ServingStation : Station
{
    [SerializeField]
    public DrinkHolder drinkHolder = null;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public override void InteractInitiated()
    {

    }

    public override void PlayerInteract()
    {
        PlayerInfo player = GetComponent<PlayerInfo>();

        Drink d = player.drinkHolder.GetHeldDrink();
        if (d != null)
        {
            if (d.Sealed)
            {
                player.drinkHolder.ReleaseHeldDrink();
                drinkHolder.SetHeldDrink(d);

                StartCoroutine(TryToSellDrink(d));


            }
            else
            {
                player.SayDialogue("I need to seal\nthe drink\nbefore serving");
            }
        }
    }

    private IEnumerator TryToSellDrink(Drink d)
    {
        yield return new WaitForSeconds(0.5f);

        drinkHolder.ReleaseHeldDrink();
        Customer servedPatron = GetComponent<CustomerManager>().ServeDrink(d);

        if (servedPatron != null)
        {
            MoneyUpdater moneyupdater = FindObjectOfType<MoneyUpdater>();

            PlayAssociatedAudio();


            int regularAmount = 5;
            moneyupdater.AddMoney(regularAmount);
            FloatingText ft = drinkHolder.gameObject.FloatText("+" + regularAmount);
            ft.TextColor = new Color(0.6f, 0.95f, 0.6f);
            ft.Drift = new Vector3(0.0f, 0.1f, 0.0f);

            // Calculate tip
            CustomerManager cm = GetComponent<CustomerManager>();
            int tipAmount = cm.CalculateTipAmount(servedPatron);

            if (tipAmount > 0)
            {
                moneyupdater.AddMoney(tipAmount);
                FloatingText tipText = drinkHolder.gameObject.FloatText("+" + tipAmount + " tip");
                tipText.TextColor = new Color(0.2f, 1.0f, 0.2f);
                tipText.Drift = new Vector3(0.0f, 0.1f, 0.0f);
                tipText.gameObject.transform.position += new Vector3(0, -0.15f, 0f);
            }
        }
        else
        {
        }

        GetComponent<DrinkManager>().DestroyDrink(d);
    }
}
