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

public class MobTurret : MonoBehaviour
{

    // lock on?
    [SerializeField]
    bool lockOnTurret;

    // bullet prefab
    [SerializeField]
    GameObject bulletPrefab;

    private Rigidbody rb;

    // bullet spawn
    //[SerializeField]
    //Transform bulletSpawnPoint;
    public int health;
    public Transform pivot;
    public float speed;
    // shoots variable bullets in burst
    [SerializeField]
    int bulletsPerShot = 1;

    //dleay between bullets
    [SerializeField]
    float shotDelay = .2f;

    // bullet speed
    [SerializeField]
    float bulletSpeed = 5f;

    [SerializeField]
    int bulletDamage = 5;

    // delay between attacks
    [SerializeField]
    float attackCooldown = 3f;

    //timer
    float attackTimer = 0f;

    // target
    GameObject bulletTarget;

    GameObject player1;
    GameObject player2;

    public AudioClip clip;
    public GameObject explosion;



    private void Start()
    {
        rb = this.GetComponent<Rigidbody>();
        bulletTarget = GetAPlayer();
    }

    private void Update()
    {
        if (bulletTarget != null)
        {
            if (this.transform.position.x > bulletTarget.transform.position.x)
            {
                this.transform.LookAt(bulletTarget.transform);
                //increment Attack timer
                attackTimer += Time.deltaTime;
                if (attackTimer > attackCooldown)
                {
                    DelegateShot();
                    attackTimer = 0f;
                }
            }
        }

    }
    public void TakeDamage(int a)
    {
        health -= a;
        if (health <= 0)
        {
            GameObject newExp = Instantiate(explosion, this.transform.position, Quaternion.identity);
            Destroy(newExp, 0.25f);
            AudioSource.PlayClipAtPoint(clip, this.transform.position);
            Destroy(this.gameObject);
        }
    }
    private void FixedUpdate()
    {
        if(bulletTarget!=null)
        {
            if (this.transform.position.x - 3 > bulletTarget.transform.position.x)
            {
                rb.velocity = Vector3.right * speed;
            }
            else
            {
                rb.velocity = Vector3.right * speed * -5;
            }
        }
        else
        {
            rb.velocity = Vector3.right * speed * -5;
        }
        

    }

    void DelegateShot()
    {
        if (bulletsPerShot > 1)
        {
            StartCoroutine(MultiShot(bulletsPerShot));
        }
        else
        {
            Shoot();
        }
    }

    IEnumerator MultiShot(int _bulletsPerShot)
    {
        int remainingShots = _bulletsPerShot;
        while (remainingShots > 0)
        {

            // instantiate bullet
            Shoot();
            --remainingShots;
            yield return new WaitForSeconds(shotDelay);
        }
    }

    void Shoot()
    {
        GameObject bullet = Instantiate(bulletPrefab, pivot.position, Quaternion.Euler(0, 0, pivot.eulerAngles.x));
    }

    GameObject GetAPlayer()
    {

        List<GameObject> targets = InputManager.INPUTMANAGER.players;

        return targets[Random.Range(0, targets.Count)].gameObject;
    }
}
