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

public class Portal : MonoBehaviour 
{
    public Transform ExitDir;

    [HideInInspector] public bool Open = false;

    private BoxCollider _collider;

    [SerializeField] private ParticleSystem _particles;
    [SerializeField] private ParticleSystem _particlesMail;
    private Vector2 strengthRange = new Vector2(6, 12);

    private int _identifier = -1;

    public void Setup(int id)
    {
        _identifier = id;

        _collider = GetComponent<BoxCollider>();
        ParticleSystem[] particles = GetComponentsInChildren<ParticleSystem>();

        foreach(ParticleSystem p in particles)
        {
            if (p != _particlesMail)
            {
                var main = p.main;
                main.startColor = new ParticleSystem.MinMaxGradient(GameController.Instance.PortalColorPresets[id].PortalColorA, GameController.Instance.PortalColorPresets[id].PortalColorB);
            }
        }

        Activate(false);
    }

    public void Activate(bool value)
    {
        Open = value;
        transform.GetChild(0).gameObject.SetActive(value);

        if (Open)
        {
            _collider.center = new Vector3(_collider.center.x, _collider.center.y, -2.7f);
            _collider.isTrigger = true;

            GameController.Instance.SendPortalsToRenderers();
        }
        else
        {
            _collider.center = new Vector3(_collider.center.x, _collider.center.y, -1.75f);
            _collider.isTrigger = false;
        }
    }

    public void Spawn(GameObject prefab, int targetType)
    {
        var package = Instantiate(prefab, ExitDir.position, Quaternion.identity).GetComponent<Package>();
        package.Setup(targetType, ExitDir.forward * Random.Range(strengthRange.x, strengthRange.y));
        GameController.Instance.Packages.Add(package);

        _particlesMail.Play();
    }

    public void Spawn(GameObject prefab)
    {
        var spawn = Instantiate(prefab, ExitDir.position, Quaternion.identity).GetComponent<Grabable>();

        spawn.Setup(ExitDir.forward * Random.Range(strengthRange.x, strengthRange.y));
    }

    public void SpawnDude(GameObject prefab)
    {
        var spawn = Instantiate(prefab, ExitDir.position, Quaternion.identity).GetComponent<FireDude>();

        spawn.Setup(this);
    }

    public int GetID()
    {
        return _identifier;
    }

    void OnTriggerEnter(Collider other)
    {
        var package = other.gameObject.GetComponent<Package>();
        if (package != null && package.LifeTime > 2)
        {
            if (package.TargetType == _identifier)
                GameController.Instance.PackageCorrect();
            else
                GameController.Instance.PackageFailed();


            GameController.Instance.Packages.Remove(package);

            package.DestroyMe();
            Cursor.visible = true;
        }
        else
        {
            var grabable = other.gameObject.GetComponent<Grabable>();
            if (grabable != null)
                grabable.DestroyMe();

            //var bomb = other.gameObject.GetComponent<Bomb>();
            //if (bomb != null)
            //{
            //    Destroy(bomb.gameObject);
            //    Cursor.visible = true;
            //}
            //else
            //{
            //    var enemy = other.gameObject.GetComponent<Enemy>();
            //    if (enemy != null)
            //    {
            //        Destroy(enemy.gameObject);
            //        Cursor.visible = true;
            //    }
            //}
        }
    }


}
