using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.PlayerLoop;

public class Gun : MonoBehaviour
{
    public GameObject bullet;

    /// <summary>
    /// gun stats
    /// </summary>
    public int magSize;
    public int damage;
    public float timeBtwShots;
    public float range;
    public float reloadTime;
    public int bulletsPerFire;
    public bool automaticFire;

    /// <summary>
    /// gun status
    /// </summary>
    private bool reloading;
    private bool shooting;
    public bool readyToShot;
    private int bulletsLeft;

    /// <summary>
    /// Refrencing
    /// </summary>
    [SerializeField]
    Camera fpsCam;
    public Transform bulletSpawn;
    public TextMeshProUGUI bulletCounter;
    public bool allowInvoke = true;

    [SerializeField]
    private GameObject mag;
    public AudioSource source;

    void Awake()
    {
        bulletsLeft = magSize;
        readyToShot = true;
    }

    public void Shoot()
    {
        if (readyToShot && !reloading && bulletsLeft > 0)
        {
            Debug.Log("Bang");
            Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hit;
            Vector3 targetPoint;
            if (Physics.Raycast(ray, out hit))
            {
                targetPoint = hit.point;
            }
            else
            {
                targetPoint = ray.GetPoint(150);
            }
            source.PlayOneShot(GameManager.instance.bang);

            Vector3 direction = targetPoint - bulletSpawn.position;
            GameObject currentBullet = Instantiate(
                bullet,
                bulletSpawn.position,
                Quaternion.identity
            );
            currentBullet.GetComponent<BulletCollider>().damage = damage;
            currentBullet.transform.forward = direction.normalized;
            currentBullet
                .GetComponent<Rigidbody>()
                .AddForce(direction.normalized * range, ForceMode.Impulse);
            bulletsLeft--;
            if (allowInvoke)
            {
                Invoke("ResetShot", timeBtwShots);
                allowInvoke = false;
            }
        }
    }

    void Update()
    {
        if (bulletsLeft == 0)
        {
            Reload();
        }
        if (fpsCam == null)
        {
            fpsCam = GameObject.Find("MainCamera").GetComponent<Camera>();
        }
        if (bulletCounter == null)
        {
            bulletCounter = GameObject.Find("bullet counter").GetComponent<TextMeshProUGUI>();
        }
        bulletCounter.text = magSize.ToString() + "/" + bulletsLeft.ToString();
    }

    private void ResetShot()
    {
        //Allow shooting and invoking again
        readyToShot = true;
        allowInvoke = true;
    }

    public void Reload()
    {
        if (bulletsLeft < magSize && !reloading)
        {
            Debug.Log("reload");
            reloading = true;
            readyToShot = false;
            Invoke("Reloaded", reloadTime);
            mag.SetActive(false);
            source.PlayOneShot(GameManager.instance.reload1);
        }
    }

    void Reloaded()
    {
        reloading = false;
        readyToShot = true;
        bulletsLeft = magSize;
        mag.SetActive(true);
        source.PlayOneShot(GameManager.instance.reload2);
    }
}
