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

public class Trade : MonoBehaviour
{
    [Header("General")]
    [SerializeField] private GameObject tradeMenu;

    [Header("Price")]
    [SerializeField] private int[] pistolPrices;
    [SerializeField] private int[] machineGunPrices;
    [SerializeField] private int[] shotGunPrices;
    [SerializeField] private int[] riflePrices;
    [Space]
    [SerializeField] private int[] weaponAmmoPrice;
    [Space]
    [SerializeField] private int[] armorPrice;
    [Space]
    [SerializeField] private int healthPotionPrice;

    [Header("WeaponsMenu")]
    [SerializeField] private GameObject[] pistols;
    [SerializeField] private GameObject[] machineGuns;
    [SerializeField] private GameObject[] shotGuns;
    [SerializeField] private GameObject[] rifles;

    [Header("Armormenu")]
    [SerializeField] private GameObject[] armors;

    // Активация торгового меню
    public void SetActiveTradeMenu(bool value)
    {
        tradeMenu.SetActive(value);

        if (value == true)
        {
            Game.game.ui.SetMoneyText();
            SetAllPrices();
            SetActiveWeaponToBuy();
            SetActiveArmorToBuy(true);
            Time.timeScale = 0f;
        }
        else if (value == false)
        {
            Time.timeScale = 1f;
        }
    }

    // Установка всех цен в магазине
    void SetAllPrices()
    {
        // Установка цен на оружие
        Game.game.ui.SetPistolPrice(pistolPrices[Game.game.weaponManager.pistolLevel + 1]);
        Game.game.ui.SetMachineGunPrice(machineGunPrices[Game.game.weaponManager.machineGunLevel + 1]);
        Game.game.ui.SetShotGunPrice(shotGunPrices[Game.game.weaponManager.shotGunLevel + 1]);
        Game.game.ui.SetRiflePrice(riflePrices[Game.game.weaponManager.rifleLevel + 1]);

        // Установка цен на аммуницию
        for (int i = 0; i < weaponAmmoPrice.Length; i++)
        {
            Game.game.ui.SetWeaponAmmoPrice(i, weaponAmmoPrice[i]);
        }

        // Установка цен на броню
        Game.game.ui.SetArmorPrice(armorPrice[Game.game.player.armorLevel + 1]);

        // Установка цены на зелье здоровья
        Game.game.ui.SetHealthPotionPrice(healthPotionPrice);
    }

    // Активация нужного оружия для покупки
    void SetActiveWeaponToBuy()
    {
        SetActivePistolToBuy(true);
        SetActiveMachineGunToBuy(true);
        SetActiveShotGunToBuy(true);
        SetActiveRifleToBuy(true);
    }

    void SetActivePistolToBuy(bool value)
    {
        int pistol = Game.game.weaponManager.pistolLevel;
        pistols[pistol + 1].SetActive(value);
    }

    void SetActiveMachineGunToBuy(bool value)
    {
        int machineGun = Game.game.weaponManager.machineGunLevel;
        machineGuns[machineGun + 1].SetActive(value);
    }

    void SetActiveShotGunToBuy(bool value)
    {
        int shotGun = Game.game.weaponManager.shotGunLevel;
        shotGuns[shotGun + 1].SetActive(value);
    }

    void SetActiveRifleToBuy(bool value)
    {
        int rifle = Game.game.weaponManager.rifleLevel;
        rifles[rifle + 1].SetActive(value);
    }

    // Покупка и улучшение пистолета
    public void BuyWeaponPistol(int value)
    {
        if (Game.game.player.SubtractMoney(pistolPrices[value]) == true)
        {
            Game.game.weaponManager.OwnNewWeapon(0);
            Game.game.weaponManager.pistolLevel = value;

            Game.game.ui.SetMoneyText();
            SetAllPrices();
            SetActiveWeaponToBuy();

            Game.game.player.ChangeToNewWeapon(0);
        }
    }

    // Покупка и улучшение автомата
    public void BuyWeaponMachineGun(int value)
    {
        if (Game.game.player.SubtractMoney(machineGunPrices[value]) == true)
        {
            Game.game.weaponManager.OwnNewWeapon(1);
            Game.game.weaponManager.machineGunLevel = value;

            Game.game.ui.SetMoneyText();
            SetAllPrices();
            SetActiveWeaponToBuy();
        }
    }

    // Покупка и улучшение дробовика
    public void BuyWeaponShotGun(int value)
    {
        if (Game.game.player.SubtractMoney(shotGunPrices[value]) == true)
        {
            Game.game.weaponManager.OwnNewWeapon(2);
            Game.game.weaponManager.shotGunLevel = value;

            Game.game.ui.SetMoneyText();
            SetAllPrices();
            SetActiveWeaponToBuy();
        }
    }

    // Покупка и улучшение винтовки
    public void BuyWeaponRifle(int value)
    {
        if (Game.game.player.SubtractMoney(riflePrices[value]) == true)
        {
            Game.game.weaponManager.OwnNewWeapon(3);
            Game.game.weaponManager.rifleLevel = value;

            Game.game.ui.SetMoneyText();
            SetAllPrices();
            SetActiveWeaponToBuy();
        }
    }

    // Покупка патронов для оружия
    public void BuyAmmoForWeapon(int value)
    {
        if (Game.game.player.SubtractMoney(weaponAmmoPrice[value]) == true)
        {
            int i = Game.game.weaponManager.GetWeaponMaxAmmo(value);
            Game.game.weaponManager.AddAmmo(value, i);

            Game.game.ui.SetMoneyText();
            Game.game.ui.SetAmmoText();
            SetAllPrices();
        }
    }

    void SetActiveArmorToBuy(bool value)
    {
        int armor = Game.game.player.armorLevel;
        armors[armor + 1].SetActive(value);
    }

    // Покупка брони
    public void BuyArmor(int value)
    {
        if (Game.game.player.SubtractMoney(armorPrice[value]) == true)
        {
            Game.game.player.UpgradeArmor();
            Game.game.player.ChangeArmor(value);

            Game.game.ui.SetMoneyText();
            SetAllPrices();
            SetActiveArmorToBuy(true);
        }
    }

    // Покупка зелья здоровья
    public void BuyHealthPotion()
    {
        if (Game.game.player.SubtractMoney(healthPotionPrice) == true && Game.game.player.CanAddHealthPotion() == true)
        {
            Game.game.player.AddHealthPotion();
            Game.game.ui.SetMoneyText();

            Game.game.ui.SetMoneyText();
            SetAllPrices();
        }
    }
}
