using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UI : MonoBehaviour
{
    [Header("Health")]
    [SerializeField] private Slider healthSlider;
    [SerializeField] private Animator healthAnim;

    [Header("Health Potion")]
    [SerializeField] private GameObject[] healthPotionsUI;

    [Header("Weapon")]
    [SerializeField] private TextMeshProUGUI ammoText;
    [SerializeField] private Image weaponUI;

    [Header("Trade")]
    [SerializeField] private TextMeshProUGUI moneyText;
    [Space]
    [SerializeField] private TextMeshProUGUI healthPotionPriceText;

    [Header("Trade (Weapons)")]
    [SerializeField] private TextMeshProUGUI[] pistolsPriceText;
    [SerializeField] private TextMeshProUGUI[] machineGunsPriceText;
    [SerializeField] private TextMeshProUGUI[] shotGunsPriceText;
    [SerializeField] private TextMeshProUGUI[] riflesPriceText;

    [Header("Trade (Ammunitions)")]
    [SerializeField] private TextMeshProUGUI[] weaponAmmoPriceText;

    [Header("Trade (Armor)")]
    [SerializeField] private TextMeshProUGUI[] armorPriceText;

    [Header("Cursor")]
    public Texture2D[] crosshairTexture;
    public CursorMode crosshairMode = CursorMode.Auto;
    public Vector2[] crosshairHotSpot;

    // Эффект перехода
    [Header("Transition")]
    [SerializeField] private Image transitionGFX;
    private Animator transitionAnim;

    [Header("Help")]
    [SerializeField] private TextMeshProUGUI textHelp;

    [Header("Pause")]
    [SerializeField] private GameObject pauseMenu;
    [SerializeField] private GameObject deathScreen;
    [SerializeField] private GameObject outroScreen;

    void Start()
    {
        ChangeCursor(1);
    }

    void Update()
    {
        // Меню паузы
        if (Input.GetButtonDown("Cancel"))
        {
            if (Game.game.ui.GetPauseMenuActiveState() == true)
            {
                Game.game.ui.SetPauseMenuActive(false);
                Game.game.player.SetActivePlayer(true);
            }
            else if (Game.game.ui.GetPauseMenuActiveState() == false && deathScreen.activeSelf == false && outroScreen.activeSelf == false)
            {
                Game.game.ui.SetPauseMenuActive(true);
                Game.game.player.SetActivePlayer(false);
            }
        }
    }

    public void ChangeCursor(int value)
    {
        // Смена курсора
        Cursor.SetCursor(crosshairTexture[value], crosshairHotSpot[value], crosshairMode);
    }

    public void SetTextHelp(bool value)
    {
        textHelp.gameObject.SetActive(value);
    }

    // Установка максимального значения для слайдера здоровья
    public void SetHealthSliderMaxValue(int value)
    {
        healthSlider.maxValue = value;
        SetHealthSliderValue(value);
    }

    // Установка текущего значения для слайдера здоровья
    public void SetHealthSliderValue(int value)
    {
        healthSlider.value = value;
        SetHeartBeat();
    }

    // Эффект битья сердца (установка нужной скорости битья)
    void SetHeartBeat()
    {
        if (healthSlider.value <= healthSlider.maxValue && healthSlider.value >= ((healthSlider.maxValue / 100) * 75))
        {
            healthAnim.SetInteger("HeartBeat", 1);
        }
        else if (healthSlider.value < ((healthSlider.maxValue / 100) * 75) && healthSlider.value >= ((healthSlider.maxValue / 100) * 50))
        {
            healthAnim.SetInteger("HeartBeat", 2);
        }
        else if (healthSlider.value < ((healthSlider.maxValue / 100) * 50) && healthSlider.value >= ((healthSlider.maxValue / 100) * 25))
        {
            healthAnim.SetInteger("HeartBeat", 3);
        }
        else
        {
            healthAnim.SetInteger("HeartBeat", 4);
        }
    }

    public void SetHealthPotionsUI()
    {
        for (int i = 0; i < Game.game.player.GetMaxHealthPotions(); i++)
        {
            healthPotionsUI[i].SetActive(false);
        }

        for (int i = 0; i < Game.game.player.GetCurrentHealthPotions(); i++)
        {
            healthPotionsUI[i].SetActive(true);
        }
    }

    // Установка текущего количества патронов
    public void SetAmmoText()
    {
        int currentAmmo = Game.game.weaponManager.currentWeapon.currentAmmo;
        int maxAmmo = Game.game.weaponManager.currentWeapon.maxAmmo;
        int ammunition = Game.game.weaponManager.ammunition[Game.game.weaponManager.currentWeapon.weaponType];

        ammoText.SetText(currentAmmo.ToString() + "/" + maxAmmo.ToString() + ":" + ammunition);
    }

    public void SetUpWeaponUI()
    {
        weaponUI.sprite = Game.game.weaponManager.currentWeapon.gfx;
    }

    // Сколько сейчас денег у игрока
    public void SetMoneyText()
    {
        moneyText.SetText(Game.game.player.GetMoneyAmount().ToString());
    }

    // Установка всех цен
    public void SetHealthPotionPrice(int value)
    {
        healthPotionPriceText.SetText(value.ToString() + "$");
    }

    public void SetPistolPrice(int value)
    {
        int i = Game.game.weaponManager.pistolLevel;
        pistolsPriceText[i + 1].SetText(value.ToString() + "$");
    }

    public void SetMachineGunPrice(int value)
    {
        int i = Game.game.weaponManager.machineGunLevel;
        machineGunsPriceText[i + 1].SetText(value.ToString() + "$");
    }

    public void SetShotGunPrice(int value)
    {
        int i = Game.game.weaponManager.shotGunLevel;
        shotGunsPriceText[i + 1].SetText(value.ToString() + "$");
    }

    public void SetRiflePrice(int value)
    {
        int i = Game.game.weaponManager.rifleLevel;
        riflesPriceText[i + 1].SetText(value.ToString() + "$");
    }

    public void SetWeaponAmmoPrice(int index, int value)
    {
        weaponAmmoPriceText[index].SetText(value.ToString() + "$");
    }

    public void SetArmorPrice(int value)
    {
        int i = Game.game.player.armorLevel;
        armorPriceText[i + 1].SetText(value.ToString() + "$");
    }

    // Включение меню паузы
    public void SetPauseMenuActive(bool value)
    {
        pauseMenu.SetActive(value);

        if (value == true)
        {
            Time.timeScale = 0f;
            ChangeCursor(0);
        }
        else if (value == false)
        {
            Time.timeScale = 1f;
            ChangeCursor(1);
        }
    }

    public bool GetPauseMenuActiveState()
    {
        return pauseMenu.gameObject.activeSelf;
    }

    // Экран проигрыша
    public IEnumerator DeathScreen()
    {
        yield return new WaitForSeconds(2f);

        transitionGFX.gameObject.SetActive(true);

        transitionAnim = transitionGFX.GetComponent<Animator>();
        transitionAnim.SetTrigger("Start");

        yield return new WaitForSeconds(0.5f);

        ChangeCursor(0);
        deathScreen.SetActive(true);
    }

    public IEnumerator OutroScreen()
    {
        yield return new WaitForSeconds(1f);

        transitionGFX.gameObject.SetActive(true);

        transitionAnim = transitionGFX.GetComponent<Animator>();
        transitionAnim.SetTrigger("Start");

        yield return new WaitForSeconds(0.5f);

        ChangeCursor(0);
        outroScreen.SetActive(true);
        Game.game.player.SetActivePlayer(false);
    }

    // Эффект перехода между локациями
    public IEnumerator LocationTransitionEffect()
    {
        transitionGFX.gameObject.SetActive(true);

        transitionAnim = transitionGFX.GetComponent<Animator>();
        transitionAnim.SetTrigger("Start");

        yield return new WaitForSeconds(1f);

        transitionGFX.gameObject.SetActive(false);
    }
}
