using FSI.Lib.MVVM;
using RoboSharp;
using System.Text;
using System.Windows.Input;

namespace FSI.Lib.Guis.IbaDirSync
{
    public class ViewModel : ViewModelBase
    {
        private ICommand _cmdStart;
        private ICommand _cmdStop;

        public ViewModel()
        {
            _cmdStart = new RelayCommand<object>(ExecuteStart, CanExecuteStart);
            _cmdStop = new RelayCommand<object>(ExecuteStop, CanExecuteStop);
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            RoboCopy = new RoboSharp.RoboCommand();           
        }

        public IInterface Data { get; set; }

        public void Init()
        {
            if (Data.AutoStart)
            {
                ExecuteStart(null);
            }
        }

        public RoboCommand RoboCopy { get; set; }

        public ICommand CmdStart
        {
            get { return _cmdStart; }
            set => _cmdStart = value;
        }

        private bool CanExecuteStop(object obj)
        {
            return RoboCopy.IsRunning;
        }

        private void ExecuteStop(object obj)
        {
            if (RoboCopy != null)
            {
                RoboCopy.Stop();
                //  RoboCopy.Dispose();
            }
        }

        public ICommand CmdStop
        {
            get { return _cmdStop; }
            set => _cmdStop = value;
        }


        private bool CanExecuteStart(object obj)
        {
            return !RoboCopy.IsRunning;
        }

        private void ExecuteStart(object obj)
        {
            RoboCopy.CopyOptions.Source = Data.Source;
            RoboCopy.CopyOptions.Destination = Data.Destination;
            RoboCopy.CopyOptions.CopySubdirectories = true;
            RoboCopy.CopyOptions.MonitorSourceChangesLimit = 1;
            RoboCopy.StopIfDisposing = true;

            RoboCopy.Start();
        }
    }
}
