using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.VFX.Utility;

public class EnemyCube : MonoBehaviour
{
    [SerializeField]
    private ExposedProperty m_baseColor = "Enemy Base Color";

    [SerializeField]
    private ExposedProperty m_emissionColor = "Enemy Emission Color";

    [SerializeField]
    private ExposedProperty m_kill = "Kill";

    [SerializeField]
    private VisualEffect m_enemyEffect;

    [SerializeField]
    private MagicMask m_mask;

    [SerializeField]
    private GameObject m_cube;

    [SerializeField]
    private AudioSource m_audioSource;

    private bool m_markedForDeath = false;
    private float m_timer = 3f;

    private void Awake()
    {
        RandomizeMask();
    }

    private void Update()
    {
        if (GameManager.Instance == null) return;
        if (GameManager.Instance.Player == null) return;
        if (GameManager.Instance.RunTimer == false) return;


        FollowPlayer(GameManager.Instance.Player.transform);

        if (m_markedForDeath)
        {
            m_timer -= Time.deltaTime;
            if (m_timer < 0) Destroy(gameObject);
        }
    }

    private void FollowPlayer(Transform target)
    {
        if (m_markedForDeath) return;

        var _dir = target.position - transform.position;
        _dir.y = 0.5f;
        var _lookRot = Quaternion.LookRotation(_dir, Vector3.up);
        var _rot = Quaternion.Lerp(transform.rotation, _lookRot, Time.deltaTime * 2f);
        transform.rotation = _rot;

        var fwd = transform.forward;
        fwd.y = 0f;
        transform.Translate(fwd * 1.2f * Time.deltaTime, Space.World);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (m_markedForDeath) return;

        if (collision.transform.tag == "FireBall")
        {
            m_cube.SetActive(false);
            m_markedForDeath = true;
            m_enemyEffect.SetUInt(m_kill, 1);
            DropMask();
            m_audioSource.Play();
        }

        if (collision.transform.tag == "Player")
        {
            GameManager.Instance.KillPlayer();
        }
    }

    private void RandomizeMask()
    {
        // Lazy weighted chance.
        var _randomList = new List<MaskType>()
        {
            MaskType.Red, MaskType.Red,MaskType.Red,MaskType.Red,MaskType.Red,MaskType.Red,MaskType.Red,MaskType.Red,MaskType.Red,MaskType.Red,
            MaskType.Green, MaskType.Green,MaskType.Green,MaskType.Green,MaskType.Green,MaskType.Green,MaskType.Green,
            MaskType.Cyan,MaskType.Cyan,MaskType.Cyan,MaskType.Cyan,
            MaskType.Purple,MaskType.Purple,
        };
        var _shuffledList = _randomList.OrderBy(x => Random.value).ToList();

        var _randomIndex = Random.Range(0, _shuffledList.Count);
        var _maskType = _shuffledList[_randomIndex];
        m_mask.ActivateMask(_maskType);
        /*
         * Red = 1,
            Green = 2,
            Cyan = 4,
            Purple = 5,
        */
    }

    private void DropMask()
    {
        m_mask.transform.SetParent(null);
        m_mask.DropMask();
    }
}
