using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CareFusion.Lib.StorageSystem.Wwks2.Messages.Stock;
using Rowa.Observables;

namespace StorageSystem.Simulator
{

    public enum StockUpdateBehaviour
    {
        AccordingToStock,
        AlwaysAccept,
        AlwaysReject,
    }



    public readonly struct StockUpdateConfig
    {
        public readonly StockUpdateBehaviour Behaviour;
        public readonly string Reason;

        private StockUpdateConfig(StockUpdateBehaviour behaviour, string reason = null)
        {
            Behaviour = behaviour;
            Reason = reason;
        }

        public static StockUpdateConfig AlwaysAccept()
        {
            return new StockUpdateConfig(StockUpdateBehaviour.AlwaysAccept, null);
        }

        public static StockUpdateConfig AlwaysReject(string reason = null)
        {
            return new StockUpdateConfig(StockUpdateBehaviour.AlwaysReject, reason);
        }

        public static StockUpdateConfig AccordingToStock()
        {
            return new StockUpdateConfig(StockUpdateBehaviour.AccordingToStock);
        }

     
    }




    public partial class StockUpdateControl : UserControl
    {
        private readonly Subject<StockUpdateConfig> _configSubject = new();

        private readonly IDisposable _txtEnabledSubscription;
        private readonly IDisposable _statusFeedSubscripition;

        public IObservable<StockUpdateConfig> ConfigurationChanged => _configSubject;

        public StockUpdateControl()
        {
            InitializeComponent();

            acceptAccordingToStockRadioButton.Checked = true;
            _configSubject.Current = StockUpdateConfig.AccordingToStock();

            acceptAccordingToStockRadioButton.CheckedChanged += AcceptAccordingToStockRadioButton_CheckedChanged;
            alwaysRejectRadioButton.CheckedChanged += AlwaysRejectRadioButton_CheckedChanged;
            alwaysReplyRadioButton.CheckedChanged += AlwaysReplyRadioButton_CheckedChanged;

            _txtEnabledSubscription = _configSubject
                .Map(x => x.Behaviour == StockUpdateBehaviour.AlwaysReject)
                .Subscribe(new DelegateObserver<bool>(b => txtRejectReason.Enabled = b));

            Disposed += (_, _) =>
            {
                _txtEnabledSubscription.Dispose();
                _configSubject?.Complete();
            };

            txtRejectReason.TextChanged += TxtRejectReason_TextChanged;

        }

        private void AcceptAccordingToStockRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            _configSubject.Current = StockUpdateConfig.AccordingToStock();
        }

        private void TxtRejectReason_TextChanged(object sender, EventArgs e)
        {
            if (_configSubject.Current.Behaviour == StockUpdateBehaviour.AlwaysReject)
            {
                _configSubject.Current = StockUpdateConfig.AlwaysReject(txtRejectReason.Text);
            }
        }

        private void AlwaysReplyRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            _configSubject.Current = StockUpdateConfig.AlwaysAccept();
        }

        private void AlwaysRejectRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            _configSubject.Current = StockUpdateConfig.AlwaysReject(txtRejectReason.Text);
        }
    }
}
