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

public class helicopterEnemy : MonoBehaviour
{
    public float speed = 60;
    public float rotSpeed = 100;
    public Transform playerTransform;
    public GameObject explosion;
    private float TimeBtwSpawn;
    public float StartTimeSpawn;
    public GameObject missleLauncher;
    public GameObject missle;
    public float retreatDis;
    public float shootDist;
    private Camera cam;
    public GameObject missleDrop;
    public float targetUItimer;
    public GameObject playerMissle;
    public bool chased;

    // Start is called before the first frame update
    void Start()
    {
        targetUI = Instantiate(targetUI);
        targetUI.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform);
        targetUI.GetComponent<TargetingButton>().enemy = transform;
        playerTransform = GameObject.FindWithTag("Player").transform;
        if(playerTransform.gameObject.GetComponent<PickupManager>().missleEnabled)
            ShowTargeting();
        cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
        targetUI.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
       
        Vector3 direction = playerTransform.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(direction);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);

        float dist = Vector3.Distance(playerTransform.position, transform.position);

        if (dist <= retreatDis)
        {
            transform.Translate(0, 0, -speed * Time.deltaTime);
        }

        if (dist >= retreatDis)
        {
            
           
            transform.Translate(0, 0, speed * Time.deltaTime);
        }

        if(dist <= shootDist)
        {
            if (TimeBtwSpawn <= 0)
            {
                Instantiate(missle, missleLauncher.transform.position, missleLauncher.transform.rotation);
                TimeBtwSpawn = StartTimeSpawn;
            }
            else
            {
                TimeBtwSpawn -= Time.deltaTime;
            }
        }
        if(targetUI!=null)
            targetUI.transform.position = cam.WorldToScreenPoint(transform.position);

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

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

    }

    private void OnTriggerEnter(Collider other)
    {

        if (other.CompareTag("missle"))
        {
            Instantiate(explosion, transform.position, transform.rotation);
            int i = Random.Range(0, 3);
            if (i == 0)
                Instantiate(missleDrop, transform.position, transform.rotation);
            Destroy(targetUI);
            Destroy(gameObject, 0);

        }

        if (other.CompareTag("Player"))
        {
            Instantiate(explosion, transform.position, transform.rotation);
            int i = Random.Range(0, 3);
            if (i == 0)
                Instantiate(missleDrop, transform.position, transform.rotation);
            Destroy(targetUI);
            Destroy(gameObject, 0);

        }
    }

    public GameObject targetUI;

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

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

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