using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ToadieTechnika.GGJ2018Game
{
    public class TransmissionObject : MonoBehaviour
    {
        bool 
            m_isInit,
            m_isLocked;

        [SerializeField]
        bool m_isLockedAtFirst;

        Vector2 m_NormalPos;

        [SerializeField]
        Transform m_Transform;

        [SerializeField]
        LineRenderer m_LineRenderer;

        [Space]
        [SerializeField]
        GameObject m_LockIcon;

        [SerializeField]
        Transform m_SquareParent;

        [SerializeField]
        Transform[] m_Rings;

        Color 
            m_NormalLineStartColor,
            m_NormalLineEndColor;

        float 
            m_RingSpeed,
            m_NormalRingSpeed;

        #region Properties
        public Vector2 NormalPos
        {
            get { return m_NormalPos; }
        }

        public bool IsInit
        {
            get { return m_isInit; }
        }

        public bool IsLocked
        {
            get { return m_isLocked; }
            set 
            {
                m_isLocked = value;
                m_LockIcon.SetActive(value);
            }
        }
        #endregion

        void Start()
        {
            m_NormalPos = transform.position;
            m_NormalLineStartColor = m_LineRenderer.startColor;
            m_NormalLineEndColor = m_LineRenderer.endColor;
            m_LineRenderer.sortingOrder = 11;

            NormalizeTransmission();
            m_isInit = true;
        }

        void OnEnable()
        {
            if (m_isInit)
                NormalizeTransmission();
        }

        void Update()
        {
            m_SquareParent.Rotate(0, 0, -Time.deltaTime * (m_RingSpeed / 2f));
            foreach (Transform _Ring in m_Rings)
                _Ring.Rotate(0, 0, Time.deltaTime * m_RingSpeed);
        }

        public void ToggleLocked()
        {
            IsLocked = !IsLocked;
            GameController.instance.PlaySound(IsLocked ? SFX.TransmissionLock : SFX.TransmissionUnlock);
        }

        void NormalizeTransmission()
        {
            m_RingSpeed = Random.Range(50, 200);
            m_NormalRingSpeed = m_RingSpeed;

            IsLocked = m_isLockedAtFirst;
            m_LineRenderer.enabled = false;
            StopCoroutine("FadeOutLineRenderer");
        }

        public void ComeToUnit(Vector2 _PhoneUnitPos)
        {
            m_RingSpeed = m_NormalRingSpeed * 5;
            transform.position = Vector2.MoveTowards(transform.position, _PhoneUnitPos, Time.deltaTime * 4.5f);

            //Set Line Renderer
            m_LineRenderer.SetPosition(0, transform.position);
            m_LineRenderer.SetPosition(1, _PhoneUnitPos);

            //Lines
            StopCoroutine("FadeOutLineRenderer");
            StartCoroutine("FadeOutLineRenderer");
        }

        IEnumerator FadeOutLineRenderer()
        {
            m_LineRenderer.enabled = true;

            m_LineRenderer.startColor = m_NormalLineStartColor;
            m_LineRenderer.endColor = m_NormalLineEndColor;

            yield return new WaitForSeconds(0.01f);

            while (m_LineRenderer.endColor.a > 0)
            {
                yield return new WaitForEndOfFrame();
                float _LastAlpha = m_LineRenderer.endColor.a;
                _LastAlpha = Mathf.MoveTowards(_LastAlpha, 0, Time.deltaTime * 10.5f);

                yield return new WaitForEndOfFrame();
                Color _SetEndColor = m_LineRenderer.endColor;
                _SetEndColor.a = _LastAlpha;
                m_LineRenderer.endColor = _SetEndColor;

                yield return new WaitForEndOfFrame();
                Color _SetStartColor = m_LineRenderer.startColor;
                _SetStartColor.a = _LastAlpha;
                m_LineRenderer.startColor = _SetStartColor;

                yield return new WaitForEndOfFrame();
                m_RingSpeed = Mathf.Lerp(m_RingSpeed, m_NormalRingSpeed,
                    (_LastAlpha + 1)/ (m_LineRenderer.endColor.a + 1));
            }

            yield return new WaitForEndOfFrame();
            m_LineRenderer.enabled = false;

            yield return null;
        }

        void OnTriggerEnter2D(Collider2D _GetCollider)
        {
            if (_GetCollider.CompareTag("Interference"))
                GameplayController.instance.PhoneUnitHarmed(_isInterence: true);
        }

        #if UNITY_EDITOR
        void OnDrawGizmos()
        {
            if (m_isLockedAtFirst)
            {
                Gizmos.color = Color.green;
                Gizmos.DrawWireSphere(transform.position, 1.05f);
            }
        }
        #endif
    }
}
