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

public class RecipeView : MonoBehaviour
{
    [SerializeField] private GameObject lockPanel;
    [SerializeField] private Text lockPanelText;
    [SerializeField] private Button detailButton;
    [SerializeField] private Text flavorName;
    [SerializeField] private Text flavorPrice;
    [SerializeField] private Transform flavorImageTarget;
    [SerializeField] private Text cornAmountText;
    [SerializeField] private Text flavor1AmountText;
    [SerializeField] private Text flavor2AmountText;
    [SerializeField] private Transform flavor1IconTarget;
    [SerializeField] private Transform flavor2IconTarget;
    [SerializeField] private GameObject flavor2View;
    private static readonly string amountFormat = "x{0}";
    private Color redColor;
    public IObservable<Unit> RecipeClickObservable => detailButton.OnClickAsObservable().TakeUntilDestroy(this);

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

    public void SetRecipe(ProductData data)
    {
        flavorName.text = data.Name;
        flavorPrice.text = data.price.ToString();
        flavorImageTarget.DestroyAllChildrens();
        Instantiate(data.GetIconPrefab(), flavorImageTarget);
        cornAmountText.text = string.Format(amountFormat, data.MaterialList[0].amount);
        flavor1AmountText.text = string.Format(amountFormat, data.MaterialList[1].amount);
        var shopData = SpreadsheetDataManager.Instance.GetBaseDataList<ShopData>(Const.ShopDataSheet);
        if (shopData.FirstOrDefault(sData => sData.itemId == data.MaterialList[1].id)?.GetIconPrefab() is Transform prefab1)
        {
            flavor1IconTarget.DestroyAllChildrens();
            Instantiate(prefab1, flavor1IconTarget);
        }
        if (data.GetMaterialCount() == 3)
        {
            flavor2View.SetActive(true);
            flavor2AmountText.text = string.Format(amountFormat, data.MaterialList[2].amount);
            if (shopData.FirstOrDefault(sData => sData.itemId == data.MaterialList[2].id)?.GetIconPrefab() is Transform prefab2)
            {
                flavor2IconTarget.DestroyAllChildrens();
                Instantiate(prefab2, flavor2IconTarget);
            }
        }
        else
        {
            flavor2View.SetActive(false);
        }
        CheckAmount(data);

        if (data.shopLevel == Const.SpecialShopLevel)
        {
            lockPanelText.text = ScriptLocalization.UI.RecipeChoiceLockPanelShopping;
        }
        else
        {
            lockPanelText.text = string.Format(ScriptLocalization.UI.RecipeChoiceLockPanelShopLevel, data.shopLevel);
        }
    }
    
    private bool CheckAmount(ProductData productData)
    {
        var flag = true;
        var gameData = GameDataManager.GameData;
        if (gameData.CornSeed < productData.MaterialList[0].amount)
        {
            flag = false;
            cornAmountText.color = redColor;
        }
        if ((gameData.Material.FirstOrDefault(data => data.Id == productData.MaterialList[1].id)?.Amount ?? 0) < productData.MaterialList[1].amount)
        {
            flag = false;
            flavor1AmountText.color = redColor;
        }
        if (productData.GetMaterialCount() == 3 && (gameData.Material.FirstOrDefault(data => data.Id == productData.MaterialList[2].id)?.Amount ?? 0) < productData.MaterialList[2].amount)
        {
            flag = false;
            flavor2AmountText.color = redColor;
        }
        return flag;
    }

    public void SetLockPanel(bool hasRecipe)
    {
        lockPanel.SetActive(hasRecipe);
    }
}
