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

public enum ProductRefillMode
{
    OneByOne,
    TankByTank,
}
public class ProductManagementView : MonoBehaviour
{
    [SerializeField] private List<ProductManagementTankView> tankList = new List<ProductManagementTankView>();
    [SerializeField] private GameObject oneByOneOn;
    [SerializeField] private GameObject tankByTankOn;
    [SerializeField] private Button oneByOneOff;
    [SerializeField] private Button tankByTankOff;

    public ReactiveProperty<ProductRefillMode> RefillType => refillType;
    private readonly ReactiveProperty<ProductRefillMode> refillType = new ReactiveProperty<ProductRefillMode>();
    private void Start()
    {
        refillType.AddTo(this);
        oneByOneOff.OnClickAsObservable().Select(_ => ProductRefillMode.OneByOne)
            .Merge(tankByTankOff.OnClickAsObservable().Select(_ => ProductRefillMode.TankByTank))
            .Subscribe(type =>
            {
                refillType.Value = type;
            }).AddTo(this);
        refillType.Subscribe(type =>
        {
            oneByOneOn.SetActive(type == ProductRefillMode.OneByOne);
            oneByOneOff.gameObject.SetActive(type != ProductRefillMode.OneByOne);
            tankByTankOn.SetActive(type == ProductRefillMode.TankByTank);
            tankByTankOff.gameObject.SetActive(type != ProductRefillMode.TankByTank);
        }).AddTo(this);
    }

    public List<ProductManagementTankView> SetTanks(List<int> priceList)
    {
        var i = 1;
        foreach (var tankView in tankList)
        {
            tankView.Initialize(i, priceList[i - 1]);
            i++;
        }
        return tankList;
    }

    public void SetRefillType(ProductRefillMode mode)
    {
        refillType.Value = mode;
    }
}
