using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;

public enum TankState
{
    Unavailable,
    Lock,
    Unlock,
    Selected,
}
public class ProductManagementTankView : MonoBehaviour
{
    [SerializeField] private GameObject selectedIndicateObject;
    [SerializeField] private GameObject tankObject;
    [SerializeField] private Image popcornImage;
    [SerializeField] private Transform packageImageTarget;
    [SerializeField] private Text tankNumber;
    [SerializeField] private Text tankStock;
    private string tankStockFormat = "{0}/{1} 個";
    private string tankEmptyText = "空っぽ";
    [SerializeField] private TankRarityView rarityView;
    [SerializeField] private Button detailButton;
    [SerializeField] private GameObject arrow;
    // 未購入
    [SerializeField] private GameObject lockedTankObject;
    [SerializeField] private Text lockedTankNumber; 
    [SerializeField] private Text price;
    [SerializeField] private Button purchaseButton;
    [SerializeField] private Image buttonSubImage;
    private List<ProductData> productList;
    private int tankNumberInt;
    private readonly BoolReactiveProperty isEmply = new BoolReactiveProperty();
    private readonly BoolReactiveProperty detailButtonActive = new BoolReactiveProperty();
    public int TankNumber => tankNumberInt;

    private readonly ReactiveProperty<TankState> state = new ReactiveProperty<TankState>();
    public IReadOnlyReactiveProperty<TankState> State => state;
    public IObservable<Unit> DetailButtonObservable => detailButton.OnClickAsObservable().TakeUntilDestroy(this);
    public IObservable<Unit> PurchaseButtonObservable => purchaseButton.OnClickAsObservable().TakeUntilDestroy(this);
    public IObservable<Unit> TankClickObservable { get; private set; }
    

    private void Awake()
    {
        var eventTrigger = gameObject.AddComponent<ObservableEventTrigger>();
        TankClickObservable = eventTrigger.OnPointerClickAsObservable().TakeUntilDestroy(this).AsUnitObservable();
    }

    private void Start()
    {
        isEmply.AddTo(this);
        detailButtonActive.AddTo(this);
        state.Subscribe(x =>
        {
            tankObject.SetActive(x != TankState.Lock && x != TankState.Unavailable);
            lockedTankObject.SetActive(x == TankState.Lock || x == TankState.Unavailable);
            purchaseButton.gameObject.SetActive(x == TankState.Lock);
            selectedIndicateObject.SetActive(x == TankState.Selected);
        }).AddTo(this);
        isEmply.Subscribe(active =>
        {
            packageImageTarget.gameObject.SetActive(!active);
            popcornImage.gameObject.SetActive(!active);
            rarityView.gameObject.SetActive(!active);
        }).AddTo(this);
        isEmply.CombineLatest(detailButtonActive, (b, b1) => !b && b1)
            .Subscribe(active =>
            {
                detailButton.gameObject.SetActive(active);
            }).AddTo(this);
    }

    public void Initialize(int number, int tankPrice)
    {
        state.Value = TankState.Unavailable;
        tankNumberInt = number;
        tankNumber.text = $"{number}";
        lockedTankNumber.text = $"{number}";
        price.text = $"{tankPrice}";
        productList = SpreadsheetDataManager.Instance.GetBaseDataList<ProductData>(Const.ProductDataSheet);
    }

    public void SetData(StorageTank tank)
    {
        isEmply.Value = tank.Stock == 0;
        tankStock.text = isEmply.Value ? tankEmptyText : String.Format(tankStockFormat, tank.Stock, tank.Capacity);
        rarityView.SetHasRarity(tank.Stocks);
        packageImageTarget.DestroyAllChildrens();
        if (productList.FirstOrDefault(data => data.id == tank.FlavorId) is ProductData productData)
        {
            Instantiate(productData.GetIconPrefab(), packageImageTarget);
        }
    }

    public void SetState(TankState tankState)
    {
        state.Value = tankState;
    }

    public void SetArrow(bool active)
    {
        arrow.SetActive(active);
    }

    public void SetDetailButtonActive(bool active)
    {
        detailButtonActive.Value = active;
    }

    public void SetPurchaseButtonActive(bool active)
    {
        purchaseButton.interactable = active;
        buttonSubImage.color = active ? Color.white : purchaseButton.colors.disabledColor;
    }
}
