﻿//----------------------------------------------------------------------------------
// GVRNavi | Garpix Virtual Reality Navigation
// Copyright (c) 2017 Garpix Ltd.
//
// Plugin Homepage: https://gvrnavi.garpix.com
// Author Homepage: https://garpix.com
// Support: support@garpix.com
// License: Asset Store Terms of Service and EULA
// License URI: See LICENSE file in the project root for full license information.
//----------------------------------------------------------------------------------

using UnityEngine;

namespace GVRNavi
{
    [RequireComponent(typeof(BoxCollider))]
    public class VRRingSimple : VRRingBase
    {
        #region variables

        public float closedHeight = 0.1f, openedHeight = 0.2f;
        public Color NormalColor = Color.white, HoveredColor = Color.white, ActiveColor = Color.white;

        public VRTweenColor tweenColor
        {
            get
            {
                if (_tweenColor == null)
                {
                    _tweenColor = gameObject.GetComponent<VRTweenColor>();
                    if (_tweenColor == null)
                        _tweenColor = gameObject.AddComponent<VRTweenColor>();
                }
                return _tweenColor;
            }
        }

        protected Color _currentColor;
        protected VRTweenColor _tweenColor;
        private Coroutine _coroutineScale;

        #endregion


        protected override void Start()
        {
            base.Start();
            SetColor(NormalColor);
            UpdateGlobalAlphaForChilds();
        }

        #region IGvrGazeResponder

        public override void OnGazeEnter()
        {
            base.OnGazeEnter();
            if (_coroutineScale != null)
                StopCoroutine(_coroutineScale);
            _coroutineScale = StartCoroutine(ScaleCoroutine(openedHeight));
            SetColor(HoveredColor);
        }

        public override void OnGazeExit()
        {
            base.OnGazeExit();
            if (_coroutineScale != null)
                StopCoroutine(_coroutineScale);
            _coroutineScale = StartCoroutine(ScaleCoroutine(closedHeight));
            SetColor(NormalColor);
            _globalAlpha = 1f;
            UpdateGlobalAlphaForChilds();
        }

        public override void OnGazeTrigger()
        {
            base.OnGazeTrigger();
            SetColor(ActiveColor);
            _globalAlpha = 1f;
            UpdateGlobalAlphaForChilds();
        }

        #endregion IGvrGazeResponder

        #region IVRColored

        public override void SetGlobalAlpha(float alpha)
        {
            base.SetGlobalAlpha(alpha);
            SetColor(_currentColor);
        }

        #endregion IVRColored

        public virtual void UpdateColor()
        {
            SetColor(Application.isPlaying ? _currentColor : NormalColor);
        }

        protected virtual void SetColor(Color color)
        {
            _currentColor = color;
            color.a *= _globalAlpha;
            collider.enabled = color.a > 0;
            if (meshRing != null)
            {
                if (Application.isPlaying)
                    tweenColor.StartColoring(meshRing.material.GetColor("_Color"), color, 0.2f, "_Color", meshRing.material);
                else
                {
                    var tempMaterial = new Material(meshRing.sharedMaterial);
                    tempMaterial.SetColor("_Color", color);
                    meshRing.sharedMaterial = tempMaterial;
                }
            }
        }
    }
}