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

public class CornFieldReinforcementView : MonoBehaviour
{
    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 = GetHarvestText(nextHarvestCount);
    }

    public void SetRelease(int price)
    {
        reinforcementWindow.SetActive(false);
        releaseWindow.SetActive(true);
        maxWindow.SetActive(false);
        releasePriceText.text = price.ToString();
        releaseText.text = 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 $"{ScriptLocalization.UI.CornFireldReleaseTop}\n{GetHarvestText(1)}";
            case PlantLineType.Center:
                return $"{ScriptLocalization.UI.CornFireldReleaseCenter}\n{GetHarvestText(1)}";
            case PlantLineType.Bottom:
                return $"{ScriptLocalization.UI.CornFireldReleaseBottom}\n{GetHarvestText(1)}";
            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
        }
    }

    private string GetHarvestText(int value)
    {
        switch (value)
        {
            case 1:
                return ScriptLocalization.UI.CornFireldHarvestOne;
            case 2:
                return ScriptLocalization.UI.CornFireldHarvestTwo;
            case 3:
                return ScriptLocalization.UI.CornFireldHarvestThree;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}
