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

public class BlackTeaStation : 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()
    {

    }

    private bool OperatingMachine = false;

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

        if (OperatingMachine)
        {
        }
        else if (player.drinkHolder.CurrentlyHoldingADrink && !drinkHolder.CurrentlyHoldingADrink)
        {
            if  (player.drinkHolder.GetHeldDrink().TeaType != TeaType.None || player.drinkHolder.GetHeldDrink().Sealed)
            {
                // There's already tea in here, or the drink is alraedy sealedplace without doing anything
                Drink d = player.ReleaseDrink();
                drinkHolder.SetHeldDrink(d);
            }
            else
            {
                OperatingMachine = true;
                PlayAssociatedAudio();

                StartCoroutine(OperateMachine());
            }
        }
        else if (!player.drinkHolder.CurrentlyHoldingADrink && drinkHolder.CurrentlyHoldingADrink)
        {
            // Pick up the drink from the machine
            Drink drink = drinkHolder.ReleaseHeldDrink();
            player.drinkHolder.SetHeldDrink(drink);
        }
    }

    private IEnumerator OperateMachine()
    {
        PlayerInfo player = GetComponent<PlayerInfo>();

        // Take the drink from the player
        Drink d = player.ReleaseDrink();
        drinkHolder.SetHeldDrink(d);

        // pause for time
        float operationTime = 4.0f;

        d.progressFillDuration = operationTime;
        d.progressFillTime = Time.time + operationTime;
        d.drinkOrigination = DrinkRenderMode.Progress;

        yield return new WaitForSeconds(operationTime);

        d.TeaType = TeaType.Black;
        d.TeaLevel = 1.0f;

        d.drinkOrigination = DrinkRenderMode.Physical;

        OperatingMachine = false;
    }

}
