﻿//----------------------------------------------------------------------------------
// 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;
using System.Collections;
using System.Collections.Generic;

namespace GVRNavi
{
    [RequireComponent(typeof(SpriteRenderer))]
    public class VRReticlePointer : MonoBehaviour
    {
        #region variables

        public VRReticlePointerType type = VRReticlePointerType.Press;
        public float SpriteSizeMax = 4f;

        private VRReticlePointerImpl reticlePointerImpl;
        private SpriteRenderer _sprite;
        private List<GameObject> _listGameObjects;
        private List<VRObjectGaze> _listGazeObjects;
        private Coroutine _coroutine;
        private Color _defaultColorOfSprite;
        private Vector3 _defaultLocalPositionOfSprite;

        #endregion


        void Awake()
        {
            reticlePointerImpl = new VRReticlePointerImpl();
            reticlePointerImpl.startCoroutine += NeedStartCoroutine;
            reticlePointerImpl.stopCoroutine += NeedStopCoroutine;
            reticlePointerImpl.type = type;
        }

        void Start()
        {
            _sprite = GetComponent<SpriteRenderer>();
            _defaultColorOfSprite = _sprite.color;
            _defaultLocalPositionOfSprite = _sprite.transform.localPosition;
            reticlePointerImpl.OnStart();
            UpdateReticleProperties();
        }

        void Update()
        {
            UpdateReticleProperties();
        }

        void OnDestroy()
        {
            if (reticlePointerImpl != null)
            {
                reticlePointerImpl.startCoroutine -= NeedStartCoroutine;
                reticlePointerImpl.stopCoroutine -= NeedStopCoroutine;
            }
        }

#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
        void Update()
        {
            Vector3 rotation = transform.rotation.eulerAngles;
            transform.rotation = Quaternion.Euler(rotation.x, rotation.y, 0);
        }
#endif

        private void UpdateReticleProperties()
        {
            if (reticlePointerImpl == null)
            {
                return;
            }
            //reticlePointerImpl.ReticleGrowthSpeed = reticleGrowthSpeed;
            reticlePointerImpl.PointerTransform = transform;
        }

        private void NeedStartCoroutine(VRObjectGaze gaze)
        {
            if (type == VRReticlePointerType.Gaze)
                _coroutine = StartCoroutine(TimeCoroutine(gaze));
        }

        private void NeedStopCoroutine()
        {
            if (_coroutine != null)
                StopCoroutine(_coroutine);
            if (_sprite != null)
            {
                _sprite.color = _defaultColorOfSprite;
                _sprite.transform.localPosition = _defaultLocalPositionOfSprite;
            }
        }

        private IEnumerator TimeCoroutine(VRObjectGaze gaze)
        {
            float time = gaze.timeToTrigger;
            float timeCurrent = gaze.timeToTrigger;
            Color c = _sprite.color;
            while (timeCurrent > 0)
            {
                float value = timeCurrent / time;
                c.a = 1 - value;
                _sprite.color = c;
                _sprite.transform.localPosition = _defaultLocalPositionOfSprite - new Vector3(0f, 0f, SpriteSizeMax * value);
                yield return new WaitForEndOfFrame();
                timeCurrent -= Time.deltaTime;
            }
            _sprite.color = _defaultColorOfSprite;
            _sprite.transform.localPosition = _defaultLocalPositionOfSprite;
            if (gaze != null)
                gaze.OnGazeTrigger();
        }
    }
}