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

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

    private PlayerInfo player = null;
    // Start is called before the first frame update
    void Start()
    {
        player = FindObjectOfType<PlayerInfo>();
        
    }

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

    public override void InteractInitiated()
    {

    }

    private bool OperatingMachine = false;

    public override void PlayerInteract()
    {
        if (OperatingMachine)
        {
        }
        else if (player.drinkHolder.CurrentlyHoldingADrink && !drinkHolder.CurrentlyHoldingADrink)
        {
            if (player.drinkHolder.GetHeldDrink().Sealed)
            {
                // Take the drink from the player
                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()
    {
        // Take the drink from the player
        Drink d = player.ReleaseDrink();
        drinkHolder.SetHeldDrink(d);

        // pause for time
        float operationTime = 5.0f;

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

        yield return new WaitForSeconds(operationTime);

        d.Sealed = true;
        drinkHolder.UpdateSubObjectState();

        d.drinkOrigination = DrinkRenderMode.Physical;

        OperatingMachine = false;
    }
}
