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

public class TeleportCamera : MonoBehaviour
{

    private Transform sceneCamera;
    private List<GameObject> sceneObjects;

    [Space(10)]
    [Header("Teleportation Tools")]
    public GameObject TapToTeleport;
    public AnimationCurve teleportCurve;


    private void OnEnable()
    {
        sceneCamera = Camera.main.transform;
    }

    /// <summary>
    /// Turn on the TapToPlace Component to Begin Setting the Teleport Point
    /// </summary>
    public void StartManualTeleport()
    {
        TapToTeleport.GetComponent<TapToPlace>().enabled = false;
        TapToTeleport.GetComponent<TapToPlace>().enabled = true;
        TapToTeleport.GetComponent<TapToPlace>().StartPlacement();
    }


    void GetSceneObjects()
    {
        GameObject[] gameObject = SceneManager.GetActiveScene().GetRootGameObjects();
        sceneObjects = gameObject.ToList();
        sceneObjects.RemoveAll((e) => 
        e.name == sceneCamera.transform.root.name ||
        e.name == this.transform.root.name);
    }

    /// <summary>
    /// Parent the Object to Move to the Teleport Point and then Move the Teleport Point to the Camera with Height Subtracted from the Camera's Location
    /// </summary>
    public void TeleportToPoint()
    {
        Vector3 moveVector = sceneCamera.position - TapToTeleport.transform.position;
        moveVector.y = 0;
        GetSceneObjects();
        StartCoroutine(StartTeleport(moveVector));
    }

    IEnumerator StartTeleport(Vector3 moveVector)
    {
        for (float i = 0; i < 0.25; i+= Time.deltaTime)
        {
            sceneObjects.ForEach((e) => {
                e.transform.position +=
            ((moveVector * (Time.deltaTime / 0.25f))); });
            yield return null;
        }
    }

   
}
