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

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

    public GameObject[] DamagedStateObjects = null;

    // Start is called before the first frame update
    void Start()
    {
        foreach (GameObject go in DamagedStateObjects)
            go.SetActive(false);
        
    }


    float spentSeconds = 0.0f;
    const float operationTime = 4.0f;

    float nextImportantEvent = 0.0f;

    private bool WaitingForRepair = false;
    // Update is called once per frame
    void Update()
    {
        if (OperatingMachine)
        {
            // pause for time
            Drink d = drinkHolder.GetHeldDrink();
            if (Time.time > nextImportantEvent)
            {
                if (spentSeconds >= operationTime)
                {
                    FinishOperatingMachine();
                }
                else
                {
                    if (!WaitingForRepair)
                    {
                        WaitingForRepair = true;
                        foreach (GameObject go in DamagedStateObjects)
                            go.SetActive(true);
                    }
                }
            }
        }

    }

    public void DoRepair()
    {
        WaitingForRepair = false;
        foreach (GameObject go in DamagedStateObjects)
            go.SetActive(false);

        PickNextBreakdown();
    }

    private void StartOperatingMachine()
    {
        OperatingMachine = true;

        PlayerInfo player = GetComponent<PlayerInfo>();

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

        spentSeconds = 0.0f;

        d.progressFillDuration = operationTime;
        d.drinkOrigination = DrinkRenderMode.Progress;

        PlayAssociatedAudio();

        PickNextBreakdown();
    }

    private void PickNextBreakdown()
    {
        Drink d = drinkHolder.GetHeldDrink();

        float breakdownIn = Random.Range(1.0f, 6.0f);
        d.progressStopAt = spentSeconds + breakdownIn;

        d.progressFillTime = Time.time + operationTime - spentSeconds;

        if (spentSeconds + breakdownIn > operationTime)
        {
            nextImportantEvent = Time.time + (operationTime - spentSeconds);
        }
        else
        {
            nextImportantEvent = Time.time + breakdownIn;
        }
        spentSeconds += breakdownIn;
    }


    private void FinishOperatingMachine()
    {
        PlayerInfo player = GetComponent<PlayerInfo>();

        Drink d = drinkHolder.GetHeldDrink();

        // Do good work
        d.HasBoba = true;

        // Turn off progress bar mode
        d.drinkOrigination = DrinkRenderMode.Physical;
        d.progressStopAt = 9999.0f;

        OperatingMachine = false;
    }

    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().HasBoba || player.drinkHolder.GetHeldDrink().Sealed)
            {
                // Swap the drink position
                Drink drink = player.drinkHolder.ReleaseHeldDrink();
                drinkHolder.SetHeldDrink(drink);
            }
            else
            {
                StartOperatingMachine();
            }
        }
        else if (!player.drinkHolder.CurrentlyHoldingADrink && drinkHolder.CurrentlyHoldingADrink)
        {
            // Pick up the drink from the machine
            Drink drink = drinkHolder.ReleaseHeldDrink();
            player.drinkHolder.SetHeldDrink(drink);
        }
    }

}
