using System;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

public class missle : MonoBehaviour
{
    public bool kamikaze;
    public float speed = 60;
    public float rotSpeed = 100;
    public Transform playerTransform;
    public GameObject explosion;
    public Transform targetFlare;
    private Camera cam;
    public float targetUItimer;
    public GameObject trail;
    public GameObject playerMissle;
    public bool chased;

    PickupManager pm;

    // Start is called before the first frame update
    void Start()
    {
        pm = GameObject.FindObjectOfType<PickupManager>();

        targetUItimer = 10;

        playerTransform = GameObject.FindWithTag("Player").transform;

        if(!kamikaze)
        {
            SearchForFlares();
        }
        else
        {
            targetUI = Instantiate(targetUI);
            targetUI.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform);
            if (playerTransform.gameObject.GetComponent<PickupManager>().missleEnabled)
                ShowTargeting();
            targetUI.GetComponent<TargetingButton>().enemy = transform;
            cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
            targetUI.SetActive(false);
        }

        if(chased && playerMissle == null)
        {
            targetUI.GetComponent<RectTransform>().localScale = new Vector3(2f, 2f, 2f);
            HideTargeting();
            chased = false;
        }
        
    }

    // Update is called once per frame
    void Update()
    {

        if (targetFlare == null)
        {
            SearchForFlares();

            transform.Translate(0, 0, speed * Time.deltaTime);

            Vector3 direction = playerTransform.position - transform.position;
            Quaternion rotation = Quaternion.LookRotation(direction);
            transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
        }
        else
        {
            transform.Translate(0, 0, speed * Time.deltaTime);

            Vector3 direction = targetFlare.position - transform.position;
            Quaternion rotation = Quaternion.LookRotation(direction);
            transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSpeed * 2 * Time.deltaTime);
        }

        if (targetUI != null)
            targetUI.transform.position = cam.WorldToScreenPoint(transform.position);

        if (kamikaze && targetUI.activeSelf)
        {
            if (targetUItimer > 0)
                targetUItimer -= Time.deltaTime;
            else
                targetUI.SetActive(false);
        }
    }

    private void OnTriggerEnter(Collider other)
    {

        if (other.CompareTag("missle"))
        {
            if (trail != null) {
                trail.transform.SetParent(null);
                trail.GetComponent<missleTrail>().StartTimer();
            }
            
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(targetUI);
            Destroy(gameObject, 0);
        }

        if (other.CompareTag("Player"))
        {
            if (trail != null)
            {
                trail.transform.SetParent(null);
                trail.GetComponent<missleTrail>().StartTimer();
            }
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(targetUI);
            Destroy(gameObject, 0);

        }

        if (other.CompareTag("flareProjectile")&&!kamikaze)
        {
            pm.flarePickup.RemoveFlareCount(1);

            if (trail != null)
            {
                trail.transform.SetParent(null);
                trail.GetComponent<missleTrail>().StartTimer();
            }
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(targetUI);
            Destroy(other.gameObject, 0);
            Destroy(gameObject, 0);

        }
    }

    public void SearchForFlares()
    {
        if (targetFlare == null && pm.flarePickup.numOfFlares > 0)
        {
            GameObject[] flaresArr = GameObject.FindGameObjectsWithTag("flareProjectile");
            int closestIndex = 0;
            if (flaresArr.Length > 0)
            {
                for (int i = 0; i < flaresArr.Length; i++)
                {
                    if (flaresArr[i] != null)
                    {
                        float dist1 = Vector3.Distance(transform.position, flaresArr[i].transform.position);
                        float dist2 = Vector3.Distance(transform.position, flaresArr[closestIndex].transform.position);

                        if (dist1 < dist2)
                        {
                            closestIndex = i;
                        }
                    }
                    targetFlare = flaresArr[closestIndex].transform;
                }
            }
        }
    }

    public GameObject targetUI;

    public void ShowTargeting()
    {
        if(kamikaze)
            targetUI.SetActive(true);
    }

    public void HideTargeting()
    {
        if (kamikaze)
            targetUI.SetActive(false);
    }

    public void ChasedByPlayerMissle(GameObject playerMissle)
    {
        this.playerMissle = playerMissle;
        chased = true;
    }
}