/*
Copyright 2019 Dr. Thomas Benjamin Senior, Michael Zoeller 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

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

namespace EyeSkills
{
    public class EyeSkillsCameraPatcher : MonoBehaviour
    {
        [Tooltip("Reference to the disabled GvrInstantPreviewMain scene object")]
        public GameObject instantPreview;

        public bool patchInstantPreview;

        void Start()
        {
            StartCoroutine(_Start());
        }

        IEnumerator _Start()
        {
            //_patchCamera (StereoTargetEyeMask.Left);
            //_patchCamera (StereoTargetEyeMask.Right);

            //Camera.main.gameObject.GetComponent<Camera> ().enabled = false;

            //if (!patchInstantPreview)
            //			yield break;

            yield return new WaitForSeconds(1);

            // only activate google instant preview after camera setup is finished
            instantPreview.SetActive(true);

            // yield one frame to make sure googles instant preview sets up all game objects
            yield return 0;

            // disable instant preview cameras for editor main camera (mono)
            GameObject.Find("Main Camera:Instant Preview Left").SetActive(false);
            GameObject.Find("Main Camera:Instant Preview Right").SetActive(false);

            // disable instant preview cameras for left respkt right cameras
            GameObject.Find("EyeSkills Camera Left:Instant Preview Right").SetActive(false);
            GameObject.Find("EyeSkills Camera Right:Instant Preview Left").SetActive(false);
        }

        void _patchCamera(StereoTargetEyeMask eye)
        {
            // create empty game object we use to rotate eye
            GameObject offset = new GameObject("Eyeskills Offset " + eye);
            // use SetParent to make transform keep its local orientation
            offset.transform.SetParent(Camera.main.transform, false);

            // create single eye camera based on scenes main camera
            GameObject camera = new GameObject("EyeSkills Camera " + eye);
            camera.transform.SetParent(offset.transform, false);

            camera.AddComponent<Camera>();
            camera.GetComponent<Camera>().CopyFrom(Camera.main);
            camera.GetComponent<Camera>().stereoTargetEye = eye;

            camera.AddComponent<TrackedPoseDriver>();
        }
    }
}