﻿using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;

public class SimpleMeasuringController : MonoBehaviour
{
    [Space(10)]
    [Header("Tape Measure Components")]
    public GameObject MeasuringTapePrefab;

    public List<GameObject> MeasuringTapesFree;
    public List<GameObject> FreeMeasurements;

    [SerializeField]
    private Transform MeasuringTapeParent;

    [Space(10)]
    [Header("Measuring Units")]
    public bool InchesUnit;
    public bool FeetUnit;
    public bool MetersUnit;

    private int TapeMeasureFreeNum = 0;

    [SerializeField]
    private int FreeMeasurementPrefabNum = 1;

    [SerializeField]
    private GameObject MeasurementTool;

    public GameObject ButtonCollection;

    [SerializeField]
    private GameObject CurrentFreeWindowToRemove;
    [SerializeField]
    private List<GameObject> FreeWindowRemoveList;

    [SerializeField]
    private string CurrentTypeOfMeasurement;

    private void OnEnable()
    {
        MeasuringTapeParent = new GameObject("Measuring Tape Parent").transform;
    }

    /// <summary>
    /// Instantiate an Instance of the Tape Measure, Add it to the List, and Set the Measuring Units
    /// </summary>
    public void InstantiateTapeMeasure(string TypeOfTapeMeasure)
    {
        // Instantiate at position (0, 0, 0) and zero rotation.
        GameObject TapeMeasure = Instantiate(MeasuringTapePrefab, new Vector3(0, 0, 0), Quaternion.identity);
        TapeMeasure.transform.parent = MeasuringTapeParent;
        if (TypeOfTapeMeasure == "Free")
        {
            CurrentTypeOfMeasurement = "Free";
            MeasuringTapesFree.Add(TapeMeasure);
            TapeMeasure.name = MeasuringTapePrefab.name + "_" + "Free_" + TapeMeasureFreeNum++;
        }

        if (InchesUnit)
        {
            TapeMeasure.GetComponent<MeasuringTape>().SetInches();
        }
        if (FeetUnit)
        {
            TapeMeasure.GetComponent<MeasuringTape>().SetFeet();
        }
        if (MetersUnit)
        {
            TapeMeasure.GetComponent<MeasuringTape>().SetMeters();
        }

        ResetMeasuringUnits();
    }

    private void ResetMeasuringUnits()
    {
        InchesUnit = false;
        FeetUnit = false;
        MetersUnit = false;
    }

    public void SetInches()
    {
        ResetMeasuringUnits();
        InchesUnit = true;
    }

    public void SetFeet()
    {
        ResetMeasuringUnits();
        FeetUnit = true;
    }

    public void SetMeters()
    {
        ResetMeasuringUnits();
        MetersUnit = true;
    }

    /// <summary>
    /// Undo the Last Measurement by Removing it From the List and Destroying it.
    /// </summary>
    public void UndoLastMeasurement()
    {
        //Debug.Log("Removing last measurement");

        if (CurrentTypeOfMeasurement == "Free")
        {
            if (MeasuringTapesFree.Count > 0)
            {
                TapeMeasureFreeNum--;
                //FreeMeasurements[FreeMeasurements.Count - 1].GetComponent<FreeMeasurementController>().AssociatedMeasuringTapesFree.RemoveAt(FreeMeasurements[FreeMeasurements.Count - 1].GetComponent<FreeMeasurementController>().AssociatedMeasuringTapesFree.Count - 1);
                GameObject.Destroy(MeasuringTapesFree[MeasuringTapesFree.Count - 1]);
                MeasuringTapesFree.RemoveAt(MeasuringTapesFree.Count - 1);
            }
        }
    }

    /// <summary>
    /// Clear the Whole Lists of Measurements by Destroying Each Object and reducing the list to 0.
    /// </summary>
    public void ClearMeasurementList()
    {
        for (int i = MeasuringTapesFree.Count - 1; i > -1; i--)
        {
            FreeMeasurementPrefabNum = 1;
            GameObject.Destroy(MeasuringTapesFree[i]);
            MeasuringTapesFree.RemoveAt(i);
        }

        ButtonCollection.GetComponent<GridObjectCollection>().Rows = 0;
    }

    /// <summary>
    /// Remove Specific Free Measurement by Removing it From the List and Destroying it.
    /// </summary>
    public void RemoveSpecificFreeMeasurement(int SpecificMeasurement)
    {
        if (MeasuringTapesFree.Count > 0)
        {
            Debug.Log("Remove Measuring Tape = " + MeasuringTapesFree[SpecificMeasurement].name);
            GameObject.Destroy(MeasuringTapesFree[SpecificMeasurement]);
            MeasuringTapesFree.RemoveAt(SpecificMeasurement);
        }
    }

    public void AddFreeMeasurement()
    {
        if (MeasuringTapesFree.Count == 0)
        {
            SetFeet();
            InstantiateTapeMeasure("Free");
        }
        if (MeasuringTapesFree.Count >= 1)
        {
            if (!MeasuringTapesFree[MeasuringTapesFree.Count - 1].GetComponent<MeasuringTape>().isBeingPlaced)
            {
                SetFeet();
                InstantiateTapeMeasure("Free");
            }
            else
            {
                Debug.Log("You haven't completed the current Free Measurement");
            }
        }
    }

    public void RemoveFreeMeasurement(float waitTime)
    {
        // Delay is created to avoid multiple Free Removals Upon Collection Update
        GameObject PanelContent = CurrentFreeWindowToRemove.transform.Find("PanelContent").gameObject;
        GameObject RemoveText = PanelContent.transform.Find("TPM_RemovingFree").gameObject;
        RemoveText.SetActive(true); //Create method to find specific FreeMeasurement and find the Remove Child text.
        CurrentFreeWindowToRemove.transform.Find("PressableButton_RemoveFree").gameObject.SetActive(false);
        Invoke("DelayedFreeMeasurementRemoval", waitTime);
    }

    public void RemoveButtonDetermineFreeParent(GameObject Button)
    {
        CurrentFreeWindowToRemove = Button.transform.parent.gameObject;
        FreeWindowRemoveList.Add(Button.transform.parent.gameObject);
    }
}