/*
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 conflict. This controller actually performs two functions - enabling the activation of conflict, but also control over the binocular suppression ratio.
    /// Add this as a child script of the EyeSkills Camera Rig in the Unity Editor.
    /// </summary>
    public class MicroControllerConflict : MicroController
    {
        protected AudioManager audioManager;
        private EyeSkillsCameraRigEditMode parentManager;
        private bool active = false;
        private float brightnessRatio = 0f;
        public float brightnessRatioInc = 0.01f;

        private EyeSkillsCameraRig cameraRig;
        private ConflictZoneModel conflictZoneModel;

        public void Start()
        {
            if( conflictZoneModel == null)
              conflictZoneModel = GetComponent<ConflictZoneModel>();
                            audioManager = AudioManager.instance;
              cameraRig = GetComponent<EyeSkillsCameraRig>();

            deactivateConflict();
        }

        public override void Shutdown()
        {
            active = false;
            //deactivateConflict();
            parentManager.MicroControllerShutdown();
        }

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

        private void activateConflict()
        {
            conflictZoneModel.Show(true);
            conflictZoneModel.IntoConflict();
        }

        private void deactivateConflict()
        {
            conflictZoneModel.Show(false);
        }

        /// <summary>
        /// Alters the luminance ratio. From -1 to 1
        /// </summary>
        /// <param name="_brightnessRatio">Brightness ratio.</param>
        private void alterLuminanceRatio(float _brightnessRatio)
        {
            Debug.Log("MicroControllerConflict setting brightnessRatio to " + _brightnessRatio);

            // float d = 0.5f * _brightnessRatio;
            // float d1 = 0.5f + d;
            // float d2 = 0.5f - d;
            //
            // conflictZoneModel.SetLeftEyeActiveColor(new Color(1f, 1f, 1f, d1));
            // conflictZoneModel.SetRightEyeActiveColor(new Color(1f, 1f, 1f, d2));
            conflictZoneModel.SetLuminanceRatio(_brightnessRatio);
        }

        public void Update()
        {
            // Here we have our actual control interface - to add and remove our cues.
            // This is really annoying having this extra Update logic here, but enabling/disabling the script was making
            // life really complex- it doesn't happen instantaneously, but requires waiting for a full iteration AFAIK
            if (active)
            {
                if (Input.GetButton("EyeSkills Up"))
                {
                    Debug.Log("Activating Conflict");
                    activateConflict();
                }
                else if (Input.GetButton("EyeSkills Down"))
                {
                    Debug.Log("Deactivating Conflict");
                    deactivateConflict();
                }
                else if (Input.GetButton("EyeSkills Right"))
                {
                    brightnessRatio = Mathf.Clamp(brightnessRatio + brightnessRatioInc, -1f, 1f);
                    alterLuminanceRatio(brightnessRatio);
                }
                else if (Input.GetButton("EyeSkills Left"))
                {
                    brightnessRatio = Mathf.Clamp(brightnessRatio - brightnessRatioInc, -1f, 1f);
                    alterLuminanceRatio(brightnessRatio);
                }
                else if (Input.GetButton("EyeSkills Confirm") || (Input.GetButton("EyeSkills Cancel")))
                {
                    Shutdown();
                }
            }
        }
    }
}
