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

public class LevelElement : MonoBehaviour
{
    [Header("References")]
    public AudioSource sfx;
    public AudioClip[] sfxClips;

    [Header("General Properties")]
    public float[] Rotations = new float[1];
    public int CurrentRotationIndex = 0;

    [Header("Prism")]
    public float prismOutFactor = 1f;
    public float[] prismInFactors = new float[] { 1.5f, 1.6f, 1.7f };

    [Header("Filter")]
    public Config.RayColor filterColor = Config.RayColor.Red;


    [ContextMenu("Change State")]
    public void ChangeState()
    {
        CurrentRotationIndex = (CurrentRotationIndex + 1) % Rotations.Length;
#if UNITY_EDITOR
        if (UnityEditor.EditorApplication.isPlaying)
        {
            StartCoroutine(rotateItem(Rotations[CurrentRotationIndex]));
        }
        else
        {
            transform.eulerAngles = new Vector3(0, 0, Rotations[CurrentRotationIndex]);
        }
#else
            StartCoroutine(rotateItem(Rotations[CurrentRotationIndex]));

#endif
    }

    IEnumerator rotateItem(float degree)
    {
        float startAng = transform.eulerAngles.z;
        yield return StartCoroutine(Utils.DoOverTime(0.5f, (dt) =>
        {
            Vector3 vec = transform.eulerAngles;
            vec.z = Mathf.LerpAngle(startAng, degree, dt);
            transform.eulerAngles = vec;
        }));
        Vector3 vec2 = transform.eulerAngles;
        vec2.z = degree;
        transform.eulerAngles = vec2;
    }

    public Vector2 Refract(Vector2 inVector, Vector2 normal, bool wasInside, Config.RayColor rayCol)
    {
        float prismInFactor = prismInFactors[(int)rayCol];
        float n = prismOutFactor / prismInFactor;
        if (!wasInside)
        {
            float sinJ = (1 - Vector2.Dot(inVector, normal) * Vector2.Dot(inVector, normal)) / n;
            Vector2 v = new Vector2(sinJ, Mathf.Sqrt(1 - sinJ * sinJ));
            return v;
        }
        else
        {
            float sinJ = (1 - Vector2.Dot(inVector, normal) * Vector2.Dot(inVector, normal)) * n;
            Vector2 v = new Vector2(sinJ, Mathf.Sqrt(1 - sinJ * sinJ));
            return v;
        }
    }


    List<Collider2D> entered = new List<Collider2D>();
    void OnTriggerEnter2D(Collider2D other)
    {
        if (entered.Contains(other))
            return;
        if (other.CompareTag("SpeakerWave"))
        {
            ChangeState();
        }
        else if (other.CompareTag("Ray"))
        {
            if (!sfx.isPlaying)
            {
                sfx.clip = sfxClips[Random.Range(0, sfxClips.Length)];
                sfx.Play();
            }
        }

        print("ElementTrigger, tag: " + other.tag);
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (entered.Contains(other))
            entered.Remove(other);
        for (int i = 0; i < entered.Count; i++)
        {
            if (entered[i] == null)
            {
                entered.RemoveAt(i);
                i--;
            }
        }
    }
}
