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

public class WeaponManager : MonoBehaviour
{
    [Header("General")]
    [HideInInspector] public WeaponData currentWeapon;
    [SerializeField] private SpriteRenderer weaponGFX;

    [Header("Equipment")]
    private bool hasPistol;
    private bool hasMachineGun;
    private bool hasShotGun;
    private bool hasRifle;

    [Header("Weapon Data")]
    [SerializeField] private WeaponData[] pistolsData;
    [SerializeField] private WeaponData[] machineGunsData;
    [SerializeField] private WeaponData[] shotGunsData;
    [SerializeField] private WeaponData[] riflesData;

    [Header("Types")]
    public int pistolLevel;
    public int machineGunLevel;
    public int shotGunLevel;
    public int rifleLevel;

    [Header("Ammunition")]
    public int[] ammunition;

    [Header("Weapon Reloading")]
    [SerializeField] private float[] reloadTime;

    [Header("Other")]
    [SerializeField] private Transform firePoint;

    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private GameObject shootEffect;

    void Start()
    {
        OwnNewWeapon(0);
        ChangeWeapon(0);
    }

    // Смена оружия
    public bool ChangeWeapon(int value)
    {
        if (value == 0)
        {
            if (hasPistol == true && currentWeapon != pistolsData[pistolLevel])
            {
                currentWeapon = pistolsData[pistolLevel];

                return true;
            }
            else
            {
                return false;
            }
        }
        else if (value == 1)
        {
            if (hasMachineGun == true && currentWeapon != machineGunsData[machineGunLevel])
            {
                currentWeapon = machineGunsData[machineGunLevel];

                return true;
            }
            else
            {
                return false;
            }
        }
        else if (value == 2)
        {
            if (hasShotGun == true && currentWeapon != shotGunsData[shotGunLevel])
            {
                currentWeapon = shotGunsData[shotGunLevel];

                return true;
            }
            else
            {
                return false;
            }
        }
        else if (value == 3)
        {
            if (hasRifle == true && currentWeapon != riflesData[rifleLevel])
            {
                currentWeapon = riflesData[rifleLevel];

                return true;
            }
            else
            {
                return false;
            }
        }

        return false;
    }

    // Настройка оружия (графика, точка выстрела и т.п.)
    public void SetUpWeapon()
    {
        weaponGFX.sprite = currentWeapon.gfx;
        firePoint.transform.localPosition = currentWeapon.firePointPosition;
    }

    // Приобретение нового оружия
    public void OwnNewWeapon(int value)
    {
        if (value == 0)
        {
            hasPistol = true;
        }
        else if (value == 1)
        {
            hasMachineGun = true;
        }
        else if (value == 2)
        {
            hasShotGun = true;
        }
        else if (value == 3)
        {
            hasRifle = true;
        }
    }

    // Добавление патронов для нужного оружия
    public void AddAmmo(int index, int value)
    {
        ammunition[index] += value;
    }

    // Выстрел из оружия
    public bool WeaponShoot()
    {
        if (currentWeapon.currentAmmo > 0)
        {
            currentWeapon.currentAmmo -= 1;

            GameObject proj = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
            Projectile projScript = proj.GetComponent<Projectile>();

            Vector3 mousePos = Game.game.player.GetMousePosition();

            projScript.damage = currentWeapon.damage;
            projScript.rig.velocity = (mousePos - transform.position).normalized * currentWeapon.bulletSpeed;

            GameObject smoke = Instantiate(shootEffect, firePoint.position, firePoint.rotation);
            Destroy(smoke, 3f);

            return true;
        }
        else
        {
            return false;
        }
    }
    
    // Перезарядка оружия
    public bool WeaponReload()
    {
        if (ammunition[currentWeapon.weaponType] > 0 && currentWeapon.currentAmmo < currentWeapon.maxAmmo)
        {
            if (currentWeapon.currentAmmo > currentWeapon.maxAmmo)
            {
                ammunition[currentWeapon.weaponType] += currentWeapon.currentAmmo - currentWeapon.maxAmmo;
                currentWeapon.currentAmmo = currentWeapon.maxAmmo;
            }
            else if (currentWeapon.currentAmmo <= currentWeapon.maxAmmo)
            {
                ammunition[currentWeapon.weaponType] -= currentWeapon.maxAmmo - currentWeapon.currentAmmo;
                currentWeapon.currentAmmo = currentWeapon.maxAmmo;

                if (ammunition[currentWeapon.weaponType] < 0)
                {
                    ammunition[currentWeapon.weaponType] += currentWeapon.currentAmmo;
                    ammunition[currentWeapon.weaponType] = 0;
                }
            }

            return true;
        }
        else
        {
            return false;
        }
    }

    public float GetWeaponReloadTime()
    {
        return reloadTime[currentWeapon.weaponType];
    }

    public int GetWeaponMaxAmmo(int value)
    {
        if (value == 0)
        {
            return pistolsData[pistolLevel].maxAmmo;
        }
        else if (value == 1)
        {
            return machineGunsData[machineGunLevel].maxAmmo;
        }
        else if (value == 2)
        {
            return shotGunsData[shotGunLevel].maxAmmo;
        }
        else if (value == 3)
        {
            return riflesData[rifleLevel].maxAmmo;
        }
        else
        {
            return 0;
        }
    }

    // Активация/Деактивация графики оружия
    public void SetActiveWeaponGFX(bool value)
    {
        weaponGFX.gameObject.SetActive(value);
    }

    public void SetLoadedValues(PlayerData data)
    {
        hasPistol = data.hasPistol;
        hasMachineGun = data.hasMachineGun;
        hasShotGun = data.hasShotGun;
        hasRifle = data.hasRifle;

        pistolLevel = data.pistolLevel;
        machineGunLevel = data.machineGunLevel;
        shotGunLevel = data.shotGunLevel;
        rifleLevel = data.rifleLevel;

        ammunition = new int[data.ammunition.Length];

        for (int i = 0; i < data.ammunition.Length; i++)
        {
            ammunition[i] = data.ammunition[i];
        }
    }

    public PlayerData GetSavedValues(PlayerData data)
    {
        data.hasPistol = hasPistol;
        data.hasMachineGun = hasMachineGun;
        data.hasShotGun = hasShotGun;
        data.hasRifle = hasRifle;

        data.pistolLevel = pistolLevel;
        data.machineGunLevel = machineGunLevel;
        data.shotGunLevel = shotGunLevel;
        data.rifleLevel = rifleLevel;

        data.ammunition = new int[ammunition.Length];

        for (int i = 0; i < ammunition.Length; i++)
        {
            data.ammunition[i] = ammunition[i];
        }

        return data;
    }
}
