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

public class FacilityExpantion : MonoBehaviour
{
    [SerializeField] private GameObject content;
    [SerializeField] private Button menuButton;
    [SerializeField] private Button closeButton;

    private void Start()
    {
        content.transform.parent.gameObject.SetActive(false);
        menuButton.OnClickAsObservable().Subscribe(_ =>
        {
            content.transform.parent.gameObject.SetActive(true);
        });
        closeButton.OnClickAsObservable().Subscribe(_ =>
        {
            content.transform.parent.gameObject.SetActive(false);
        });
        // 設備一覧を表示
        var items = content.GetComponentsInChildren<FacilityExpantionItem>();
        // 適用できるかどうか判定
        foreach (var item in items)
        {
            if (!GameDataManager.GameData.PlantLines.Exists(x => x.Type == item.Type))
            {
                item.SetItem(false, CornFieldRank.Rank1);
                return;
            }
            item.SetItem(true, GameDataManager.GameData.PlantLines.First(x => x.Type == item.Type).Level);
            item.ClickObservable.Subscribe(_ =>
            { 
                // 購入時コイン比較後、コイン減算（ここコイン増加と処理がかぶるとややこしい（Batchとかで処理するといいかも
                // 購入した設備はセーブする(設備ごとの処理int増やすなど
            }).AddTo(this);
        }
    }

    public int GetPrice(bool hasField, CornFieldRank level)
    {
        if (!hasField)
        {
            return 500;
        }

        switch (level)
        {
            case CornFieldRank.Rank1:
                return 500;
            case CornFieldRank.Rank2:
                return 500;
            case CornFieldRank.Rank3:
                return 500;
            default:
                throw new ArgumentOutOfRangeException(nameof(level), level, null);
        }
    }
}
