using NUnit.Framework;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
using System.Collections;

public class SimpleExcution : MonoBehaviour
{
    public List<GameObject> gbs = new List<GameObject>();
    private GameObject target;
    [SerializeField] private Rigidbody2D rigid;
    [SerializeField] private GameObject effect;
    [SerializeField] private GameObject speedeffect;
    private bool isex = true;
    Animator anim;
    PlayerController pc;

    private void Start()
    {
        anim = GetComponent<Animator>();
        pc = GetComponent<PlayerController>();
    }

    void Update()
    {
        if (Keyboard.current.cKey.wasPressedThisFrame && isex == true)
        {
            Vector2 pos = gameObject.transform.position;
            float distance = Mathf.Infinity;
            float currentdistance = 0f;
            foreach(GameObject e in gbs)
            {
                currentdistance = Vector2.Distance(pos, e.transform.position);
                if(currentdistance < distance)
                {
                    distance = currentdistance;
                    target = e.gameObject;
                }
            }
            GameObject seff = Instantiate(speedeffect);
            seff.GetComponent<SimpleSpeedLine>().playerpos1 = gameObject.transform.position;
            //gameObject.GetComponent<Cinemachine>().isex = true;
            gameObject.transform.position = target.transform.position + new Vector3(0f, 0.5f, 0f);
            seff.GetComponent<SimpleSpeedLine>().playerpos2 = gameObject.transform.position;
            GameObject eff = Instantiate(effect);
            eff.transform.position = target.transform.position;
            anim.SetBool("AT", true);
            rigid.linearVelocity = new Vector2(rigid.linearVelocity.x, 0f);
            rigid.AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
            gbs.Remove(target);
            Destroy(target.gameObject);
            //pc.canDJ = true;
            isex = false;
            StartCoroutine(DJ_delay(0.2f));
        }
    }

    IEnumerator DJ_delay(float time)
    {
        yield return new WaitForSeconds(time);
        anim.SetBool("AT", false);
        isex = true;
    }
}
