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

namespace GVRNavi
{
    public class VRTweenColor : MonoBehaviour
    {
        #region variables

        private Dictionary<int, Coroutine> _dic;

        #endregion


        void Awake()
        {
            _dic = new Dictionary<int, Coroutine>();
        }

        public void StartColoring(Color colorFrom, Color colorTo, float tweenTime, params SpriteRenderer[] sprites)
        {
            if (sprites.Length > 0)
                foreach (var sprite in sprites)
                {
                    if (sprite != null)
                    {
                        int ID = sprite.GetInstanceID();
                        StopColoring(ID);
                        _dic.Add(ID, StartCoroutine(TweenCoroutine(colorFrom, colorTo, tweenTime, sprite)));
                    }
                    else
                        Debug.LogWarning("SpriteRenderer is null");
                }
        }

        public void StartColoring(Color colorFrom, Color colorTo, float tweenTime, params Image[] images)
        {
            if (images.Length > 0)
                foreach (var image in images)
                {
                    if (image != null)
                    {
                        int ID = image.GetInstanceID();
                        StopColoring(ID);
                        _dic.Add(ID, StartCoroutine(TweenCoroutine(colorFrom, colorTo, tweenTime, image)));
                    }
                    else
                        Debug.LogWarning("Image is null");
                }
        }

        public void StartColoring(Color colorFrom, Color colorTo, float tweenTime, string matColorName, params Material[] materials)
        {
            if (materials.Length > 0)
                foreach (var material in materials)
                {
                    if (material != null)
                    {
                        int ID = material.GetInstanceID();
                        StopColoring(ID);
                        _dic.Add(ID, StartCoroutine(TweenCoroutine(colorFrom, colorTo, tweenTime, matColorName, material)));
                    }
                    else
                        Debug.LogWarning("Material is null");
                }
        }

        public void StopColoring(params SpriteRenderer[] sprites)
        {
            if (sprites.Length > 0)
                foreach (var sprite in sprites)
                    if (sprite != null)
                        StopColoring(sprite.GetInstanceID());
        }

        public void StopColoring(params Image[] images)
        {
            if (images.Length > 0)
                foreach (var image in images)
                    if (image != null)
                        StopColoring(image.GetInstanceID());
        }

        public void StopColoring(params Material[] materials)
        {
            if (materials.Length > 0)
                foreach (var material in materials)
                    if (material != null)
                        StopColoring(material.GetInstanceID());
        }

        private void StopColoring(int ID)
        {
            if (_dic.ContainsKey(ID))
            {
                if (_dic[ID] != null)
                    StopCoroutine(_dic[ID]);
                _dic.Remove(ID);
            }
        }

        private IEnumerator TweenCoroutine(Color colorFrom, Color colorTo, float time, SpriteRenderer sprite)
        {
            int ID = sprite.GetInstanceID();
            sprite.color = colorFrom;
            float lerp = 0f;
            while (lerp < 1f)
            {
                yield return new WaitForSeconds(0.02f);
                lerp = Mathf.Clamp(lerp + (0.02f / time), 0f, 1f);
                if (sprite == null)
                    break;
                sprite.color = Color.Lerp(colorFrom, colorTo, lerp);
            }
            _dic.Remove(ID);
        }

        private IEnumerator TweenCoroutine(Color colorFrom, Color colorTo, float time, Image image)
        {
            int ID = image.GetInstanceID();
            image.color = colorFrom;
            float lerp = 0f;
            while (lerp < 1f)
            {
                yield return new WaitForSeconds(0.02f);
                lerp = Mathf.Clamp(lerp + (0.02f / time), 0f, 1f);
                if (image == null)
                    break;
                image.color = Color.Lerp(colorFrom, colorTo, lerp);
            }
            _dic.Remove(ID);
        }

        private IEnumerator TweenCoroutine(Color colorFrom, Color colorTo, float time, string matColorName, Material material)
        {
            int ID = material.GetInstanceID();
            material.SetColor(matColorName, colorFrom);
            float lerp = 0f;
            while (lerp < 1f)
            {
                yield return new WaitForSeconds(0.02f);
                lerp = Mathf.Clamp(lerp + (0.02f / time), 0f, 1f);
                if (material == null)
                    break;
                material.SetColor(matColorName, Color.Lerp(colorFrom, colorTo, lerp));
            }
            _dic.Remove(ID);
        }
    }
}