/*
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 EyeSkills;

namespace EyeSkills
{
    public class EyeSkillsVRHeadsetInput : MonoBehaviour
    {
        public GameObject trackedCamera;
        private EyeSkillsInput esInput;
        private Quaternion initialRotation;
        private Quaternion currentRotation;

        //Make these variables public and putting them at the class level for simpler inspection
        private Quaternion relativeRotation;
        //private float normalisedHorizontalDirection,normalisedVerticalDirection;
        private Vector3 euler;

        /// <summary>
        /// The direction of gaze, right is +ve, left if -ve.  This is normalised between 0-1 mapped from the 180deg right/left.
        /// </summary>
        private float direction;

        /// <summary>
        /// We record the intial direction of gaze, and report anything to the 180deg "right" of this as a direction,
        /// normalised to 1, and vice versa for left;
        /// </summary>
        public void Start()
        {
            initialRotation = trackedCamera.transform.localRotation;
        }

        private Vector3 calculateRelativeRotation(Quaternion initialRotation, Quaternion currentRotation)
        {
            relativeRotation = Quaternion.Inverse(initialRotation) * currentRotation;

            euler = relativeRotation.eulerAngles;

            return euler; //no need to return except for clarity of receiving code. Not creating a new Vector3 each time.
        }

        /// <summary>
        /// Calculates the normalised direction.
        /// </summary>
        /// <returns>The normalised direction.</returns>
        /// <param name="degrees">Degrees.</param>
        private float calculateNormalisedDirection(float degrees){
            //Everything above 1 or beneath -1 can be clamped by the consumer.

            if (degrees > 180)
            {
                degrees = degrees - 360; //we want to map continuous 0 -> 360 euler angles to 0->1 going right and 0->-1 going left.
            }

            float restrictMovementToDegrees = 45;
            float normalisedDirection = Mathf.Clamp((degrees / restrictMovementToDegrees),-1,1);
            return normalisedDirection;
        }

        private float calculateVerticalDirectionFromRotation()
        {

            euler = calculateRelativeRotation(initialRotation, currentRotation);

            return calculateNormalisedDirection(euler.x);

        }

        private float calculateHorizontalDirectionFromRotation()
        {

            euler = calculateRelativeRotation(initialRotation, currentRotation);

            return calculateNormalisedDirection(euler.y);
        }


        /// <summary>
        /// Gets the direction. The direction of gaze, right is +ve, left if -ve.  This is normalised between 0-1 mapped from the 180deg right/left.
        /// </summary>
        /// <returns>The direction.</returns>
        public float getVerticalDirection() 
        {
            currentRotation = trackedCamera.transform.localRotation;
            Debug.Log("Vertical: "+currentRotation.x);
            float normalisedVerticalDirection = calculateVerticalDirectionFromRotation();
            Debug.Log("Normalised Vertical Directon " + normalisedVerticalDirection);
            return normalisedVerticalDirection;


        }

        public float getHorizontalDirection()
        {
            currentRotation = trackedCamera.transform.localRotation;
            Debug.Log("Horizontal: "+currentRotation.y);
            float normalisedHorizontalDirection = calculateHorizontalDirectionFromRotation();
            Debug.Log("Normalised Horizontal Directon " + normalisedHorizontalDirection);
            return normalisedHorizontalDirection;
        }
    }

    public interface IImageIntensityRatioController
    {
        float getHorizontalDirection();
        float getVerticalDirection();
    }

}