Scriptum/Assets/Scripts/REFACTORING/Application/Shop/ShopItemCardKeeper.cs
2022-12-25 03:39:31 +01:00

196 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public enum ShopItemCardMode
{
Buy,
Sell
}
// TODO Bonusy nie sa wyswietlane poprawnie - poprawic po dodaniu poprawnie wypeklnionych itemow!!!
class ShopItemCardKeeper : MonoBehaviour
{
[Header("Current Item")]
public EquippableItem SelectedItem;
public int OriginSlotNumver;
[Header("Shopping Card")]
[SerializeField] private Button _buyButton;
[SerializeField] private Button _sellButton;
[Header("Item Preveiw Section")]
[SerializeField] public ShopItemCardMode ShopMode;
[SerializeField] private TextMeshProUGUI _itemPreviewName;
[SerializeField] private TextMeshProUGUI _itemPreviewDescription;
[SerializeField] private TextMeshProUGUI _itemPreviewStats;
[SerializeField] private TextMeshProUGUI _itemPreviewStatsValue;
[SerializeField] private Text _itemPreviewPrice;
[Header("Language phrases")]
public LanguageDetector<string> PricePhrase;
[SerializeField]
public List<IndexValuePair<ItemEffectsEnum, LanguageDetector<string>>> Phrases = new List<IndexValuePair<ItemEffectsEnum, LanguageDetector<string>>>();
public void ShowItemDetails(ItemSlot itemSlot, ShopItemCardMode mode = ShopItemCardMode.Buy)
{
SelectedItem = itemSlot.Item;
OriginSlotNumver = itemSlot.Number;
ShopMode = mode;
gameObject.active = true;
BildCardContent();
}
public void BildCardContent()
{
// Clear poanel
if (!SelectedItem)
{
_itemPreviewName.text = "";
_itemPreviewDescription.text = "";
_itemPreviewStats.text = "";
_itemPreviewStatsValue.text = "";
_itemPreviewPrice.text = "";
_buyButton.gameObject.SetActive(false);
_sellButton.gameObject.SetActive(false);
return;
}
_itemPreviewName.text = SelectedItem.Name;
_itemPreviewDescription.text = SelectedItem.Description;
_itemPreviewStats.text = BuildItemStats();
_itemPreviewStatsValue.text = SelectedItem.Value.ToString();
_itemPreviewPrice.text = $"{PricePhrase.DetectInstanceBasedOnLanguage()}: {CountPrice()}";
switch(ShopMode)
{
case ShopItemCardMode.Buy:
{
_sellButton.gameObject.SetActive(false);
_buyButton.gameObject.SetActive(true);
break;
}
case ShopItemCardMode.Sell:
{
_buyButton.gameObject.SetActive(false);
_sellButton.gameObject.SetActive(true);
break;
}
}
}
public string BuildItemStats()
{
if(SelectedItem.EquipmentType == EquipmentTypeEnum.Weapon)
return $"Attack: \n";
if (SelectedItem.EquipmentType == EquipmentTypeEnum.Helmet || SelectedItem.EquipmentType == EquipmentTypeEnum.Chest)
return $"Deffence: \n";
if (SelectedItem.EquipmentType == EquipmentTypeEnum.Boots)
return $"Speed: \n";
return "";
}
public int CountPrice()
{
return SelectedItem.Price;
}
public void BuyItem()
{
if (!_buyButton.IsActive())
return;
var itemPrice = SelectedItem.Price;
// Check player account balance
if (!CheckPlayerAccoutnBalance(itemPrice))
{
Debug.Log($"You don't have enouht gold - required {itemPrice}");
return;
}
// Change account balance status (refresh view)
AccountBalanceManager.Instance.DecreaseAccountBalanceValue(SelectedItem.Price);
// 1. Buy transaction
// 1.1.1 Add to player Inventory Panel + refresh view
InventoryUIManager.Instance.Add(SelectedItem);
if (InventoryUIManager.Instance.GetPanelStatus())
GameObject.FindObjectOfType<InventoryPanelController>().BuildPanelContent(InventoryUIManager.Instance.GetList());
// 1.1.2 Save chnages in Inventory Panel
SceneInventoryDataManager.Instance.SaveDynamicData();
// 1.2.1 Remove item from Shop Panel
ShopContentUIManager.Instance.RemoveByPosition(OriginSlotNumver);
if (ShopUIManager.Instance.GetPanelStatus())
GameObject.FindObjectOfType<ShopPanelController>().BuildPanelContent(ShopContentUIManager.Instance.GetList());
// 2 Save changes
// 2.1 Save chnages in Shop Panel
SceneInventoryDataManager.Instance.SaveDynamicData();
// 2.2 Save account balance current status
AccountBalanceManager.Instance.SaveValue();
}
public void SellItem()
{
if (!_sellButton.IsActive())
return;
// 1. Sell transaction
// 1.1.1 Remove from player Inventory Panel by position + refresh view
InventoryUIManager.Instance.RemoveByPosition(OriginSlotNumver);
if (InventoryUIManager.Instance.GetPanelStatus())
GameObject.FindObjectOfType<InventoryPanelController>().BuildPanelContent(InventoryUIManager.Instance.GetList());
// 2. Change account balance status (refresh view)
AccountBalanceManager.Instance.IncreaseAccountBalanceValue(SelectedItem.Price);
// 3 Save changes
// 3.1 Save chnages in Shop Panel
SceneInventoryDataManager.Instance.SaveDynamicData();
// 3.2 Save account balance current status
AccountBalanceManager.Instance.SaveValue();
}
public bool CheckPlayerAccoutnBalance(int requiredAmount)
{
return AccountBalanceManager.Instance.Gold >= requiredAmount;
}
}