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

public class CornFieldReinforcementView : MonoBehaviour
{
    private static readonly string ReinforceFormat = "1株から{0}本収穫できます";
    private static readonly string ReleaseFormat = "畑の{0}が耕されます\n1株から1本収穫できます";
    private static readonly string SigbBoardFormat = "x{0}";
    [SerializeField] private PlantLineType lineType;
    public PlantLineType LineType => lineType;
    [SerializeField] private Button dialogButton;
    [SerializeField] private Button dialogButton2;
    [SerializeField] private GameObject disableButton;
    [SerializeField] private GameObject disableButton2;
    [SerializeField] private GameObject reinforcementWindow;
    [SerializeField] private GameObject releaseWindow;
    [SerializeField] private GameObject maxWindow;
    [SerializeField] private ReinforcementSeedlingView currentSeedling;
    [SerializeField] private ReinforcementSeedlingView nextSeedling;
    [SerializeField] private TextMeshProUGUI currentSignBoard;
    [SerializeField] private TextMeshProUGUI nextSignBoard;
    [SerializeField] private Text reinforceText;
    [SerializeField] private Text releaseText;
    [SerializeField] private Text reinforcePriceText;
    [SerializeField] private Text releasePriceText;

    public IObservable<Unit> ButtonClickObserve => dialogButton.OnClickAsObservable().Merge(dialogButton2.OnClickAsObservable()).TakeUntilDestroy(this);

    public void SetReinforcement(PlantLineData data, int price)
    {
        reinforcementWindow.SetActive(true);
        releaseWindow.SetActive(false);
        maxWindow.SetActive(false);
        var nextRank = CornFieldReinforcement.GetNextRank(data.Level);
        var upgradeType = FieldUpgradeData.PlantTypeToUpgradeType(data.Type);
        var fieldData = SpreadsheetDataManager.Instance.GetBaseDataList<FieldUpgradeData>(Const.FieldUpgradeDataSheet);
        var currentHarvestCount = fieldData.FirstOrDefault(x => x.Type == upgradeType && x.level == (int)data.Level)?.harvested ?? 1;
        var nextHarvestCount = fieldData.FirstOrDefault(x => x.Type == upgradeType && x.level == (int)nextRank)?.harvested ?? 1;
        reinforcePriceText.text = price.ToString();
        currentSeedling.SetSeedling(data.Level);
        currentSignBoard.text = string.Format(SigbBoardFormat, currentHarvestCount);
        nextSeedling.SetSeedling(nextRank);
        nextSignBoard.text = string.Format(SigbBoardFormat, nextHarvestCount);
        reinforceText.text = string.Format(ReinforceFormat, nextHarvestCount);
    }

    public void SetRelease(int price)
    {
        reinforcementWindow.SetActive(false);
        releaseWindow.SetActive(true);
        maxWindow.SetActive(false);
        releasePriceText.text = price.ToString();
        releaseText.text = string.Format(ReleaseFormat, GetPlantTypeText(lineType));
    }

    public void SetMax()
    {
        reinforcementWindow.SetActive(false);
        releaseWindow.SetActive(false);
        maxWindow.SetActive(true);
    }
    
    public void SetButtonActive(bool active)
    {
        dialogButton.gameObject.SetActive(active);
        dialogButton2.gameObject.SetActive(active);
        disableButton.SetActive(!active);
        disableButton2.SetActive(!active);
    }
    
    private string GetPlantTypeText(PlantLineType type)
    {
        switch (type)
        {
            case PlantLineType.Top:
                return "上段";
            case PlantLineType.Center:
                return "中段";
            case PlantLineType.Bottom:
                return "下段";
            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
        }
    }
}
