using System;
using System.Linq;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class BulkOrderItemView : MonoBehaviour
{
    private static readonly string amountFormat = "x{0}";
    [SerializeField] private Button cookingButton;
    [SerializeField] private GameObject disabledObject;
    [SerializeField] private GameObject cookedObject;
    [SerializeField] private GameObject perfectCookedObject;
    [SerializeField] private RarityView rarityView;
    [SerializeField] private Text nameText;
    [SerializeField] private Text stockText;
    [SerializeField] private Text cornText;
    [SerializeField] private Text material1Text;
    [SerializeField] private Text material2Text;
    [SerializeField] private Transform material1IconTarget;
    [SerializeField] private Transform material2IconTarget;
    [SerializeField] private Transform packageIconTarget;
    
    private Color redColor;

    public IObservable<Unit> CookingButton => cookingButton.OnClickAsObservable().TakeUntilDestroy(this);

    private void Awake()
    {
        ColorUtility.TryParseHtmlString(Const.RedColorString, out redColor);
    }

    public void SetData(ProductData data)
    {
        stockText.text = data.volume.ToString();
        nameText.text = data.Name;
        cornText.text = string.Format(amountFormat, data.MaterialList[0].amount);
        packageIconTarget.DestroyAllChildrens();
        Instantiate(data.GetIconPrefab(), packageIconTarget);
        
        material1Text.text = string.Format(amountFormat, data.MaterialList[1].amount);
        material2Text.text = string.Format(amountFormat, data.MaterialList[2].amount);
        var shopData = SpreadsheetDataManager.Instance.GetBaseDataList<ShopData>(Const.ShopDataSheet);
        if (shopData.FirstOrDefault(sData => sData.itemId == data.MaterialList[1].id)?.GetIconPrefab() is Transform prefab1)
        {
            material1IconTarget.DestroyAllChildrens();
            Instantiate(prefab1, material1IconTarget);
        }
        if (shopData.FirstOrDefault(sData => sData.itemId == data.MaterialList[2].id)?.GetIconPrefab() is Transform prefab2)
        {
            material2IconTarget.DestroyAllChildrens();
            Instantiate(prefab2, material2IconTarget);
        }
        else
        {
            material2Text.transform.parent.gameObject.SetActive(false);
        }
    }
    
    public bool CheckAmount(ProductData productData)
    {
        var flag = true;
        var gameData = GameDataManager.GameData;
        if (gameData.CornSeed < productData.MaterialList[0].amount)
        {
            flag = false;
            cornText.color = redColor;
        }
        if ((gameData.Material.FirstOrDefault(data => data.Id == productData.MaterialList[1].id)?.Amount ?? 0) < productData.MaterialList[1].amount)
        {
            flag = false;
            material1Text.color = redColor;
        }
        if (productData.GetMaterialCount() == 3 && (gameData.Material.FirstOrDefault(data => data.Id == productData.MaterialList[2].id)?.Amount ?? 0) < productData.MaterialList[2].amount)
        {
            flag = false;
            material2Text.color = redColor;
        }
        return flag;
    }

    public void SetButtonActive(bool active)
    {
        cookedObject.SetActive(false);
        perfectCookedObject.SetActive(false);
        cookingButton.gameObject.SetActive(active);
        disabledObject.SetActive(!active);
    }

    public void SetCooked(ProductRarity rarity)
    {
        cookingButton.gameObject.SetActive(false);
        disabledObject.SetActive(false);
        cookedObject.SetActive(rarity == ProductRarity.Normal);
        perfectCookedObject.SetActive(!cookedObject.activeSelf);
        rarityView.SetRarity(rarity);
    }
}