/*
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
{
    /// <summary>
    /// Micro controller brightness.  This manually alters the binocular suppression ratio in the EyeSkills CameraRig.
    /// Add this as a child script of the EyeSkills Camera Rig in the Unity Editor.
    /// </summary>
    public class MicroControllerBrightness : MicroController
    {
        protected AudioManager audioManager;
        private EyeSkillsCameraRigEditMode parentManager;
        public GameObject leftEye, rightEye;
        private bool active = false;
        private EyeSkillsCameraRig cameraRig;
        private float ratio = 0;
        public float ratioIncrement = 0.01f;

        public void Start()
        {
            audioManager = AudioManager.instance;
            cameraRig = gameObject.GetComponent<EyeSkillsCameraRig>();
        }

        public override void Shutdown()
        {
            active = false;
            //leftEye.transform.Rotate(Vector3.forward - leftEye.transform.eulerAngles);
            //rightEye.transform.Rotate(Vector3.forward - rightEye.transform.eulerAngles);
            parentManager.MicroControllerShutdown();
        }

        public override void Startup(EyeSkillsCameraRigEditMode _parent)
        {
            active = true;
            audioManager.Say("BinocularSuppressionRatioAvailable");
            parentManager = _parent;
        }

        /// <summary>
        /// Alters the camera ratio. 1 is full right zero left, -1 is full left and zero right.
        /// 0 is 0.5 for each eye.  What if we want both cameras to behave normally? 
        /// Should we actually use the boost function to 2 starting from brightness 1 in each eye? 
        /// That would imply that, in our binocular suppression scene, we need to set the brightess/color of the material to 0.5 to start with.
        /// If we use "boost" we may also oversaturate elements in the scene. 
        /// How do we apply it now?!?
        /// Ah, it actually applies it from 0 - 2 using that brightness script inside the CameraRig. That also needs to use the new internal function
        /// ================
        /// SO : We make everything consistent and use the brightness script from 0 - 1 which means adding an internal method to the camera, modifying the camera init, 
        /// changing the binocular suppression scene, adding a micro controller, and testing.
        /// ================
        /// </summary>
        /// <param name="ratio">Ratio.</param>
        private void alterSuppressionRatio(float ratio)
        {
            cameraRig.SetBinocularSuppressionRatio(ratio);
        }

        public void Update()
        {
            if (active)
            {
                if (Input.GetButton("EyeSkills Confirm") || (Input.GetButton("EyeSkills Cancel")))
                {
                    //Save the new ratio (?!?) -> Probably in a scene specific way - need to implement
                    Shutdown();
                }


                if (Input.GetButton("EyeSkills Right"))
                {
                    Debug.Log("Increasing ratio...");
                    ratio = Mathf.Clamp(ratio + ratioIncrement, -1f, 1f);
                    alterSuppressionRatio(ratio);
                }
                else if (Input.GetButton("EyeSkills Left"))
                {
                    Debug.Log("Decreasing ratio...");
                    ratio = Mathf.Clamp(ratio - ratioIncrement, -1f, 1f);
                    alterSuppressionRatio(ratio);
                }
            }
        }
    }
}