using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VSview
{
    public class ServiceViewModel : BaseViewModel
    {
        /// <summary>
        /// Reference to main view model
        /// </summary>
        private MainViewModel ViewModel;

        public List<string> CalibrationTypes { get; set; }

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public ServiceViewModel(MainViewModel mainViewModel)
        {
            //add reference to main view model
            ViewModel = mainViewModel;
            //create command list
            SetupCommands();

            CalibrationTypes = new List<string>() { "Internal", "External" };
        }
        #endregion

        #region Commands

        /// <summary>
        /// Collection of all service commands
        /// </summary>
        public Dictionary<string, RelayCommand> Command { get; set; }

        /// <summary>
        /// Method for creating command collection
        /// </summary>
        private void SetupCommands()
        {
            Command = new Dictionary<string, RelayCommand>();

            Command.Add("ResetTurbo", new RelayCommand(ResetTurbo));
            Command.Add("Calibrate", new RelayCommand(Calibrate));
            Command.Add("ToggleSoftStart", new RelayCommand(ToggleSoftStart));
            Command.Add("ToggleAutozero", new RelayCommand(ToggleAutozero));
            Command.Add("ToggleZero", new RelayCommand(ToggleZero));
            Command.Add("ToggleSplitflow", new RelayCommand(ToggleSplitflow));
            Command.Add("Tune", new RelayCommand(Tune));
            Command.Add("ResetFilament1", new RelayCommand(ResetFilament1));
            Command.Add("ResetFilament2", new RelayCommand(ResetFilament2));
            Command.Add("ToggleFilament", new RelayCommand(ToggleFilament));
            Command.Add("ToggleRejectHighLow", new RelayCommand(ToggleRejectHighLow));
            Command.Add("ToggleRejectOnOff", new RelayCommand(ToggleRejectOnOff));
            Command.Add("AutoRange", new RelayCommand(AutoRange));
            Command.Add("ToggleRangestop", new RelayCommand(ToggleRangestop));

            Command.Add("ChangeCalibrationType", new RelayCommand(ChangeCalibrationType));

            Command.Add("SetReject", new RelayCommand(SetReject));
        }

        private void SetReject(object obj)
        {
            TerminalViewModel.WriteToLog(ViewModel.Detector.Reject.Reject1.Value + " INIT-1REJECT");
        }


        /// <summary>
        /// Method for reseting turbo operating hours
        /// </summary>
        /// <param name="obj"></param>
        private void ResetTurbo(object obj)
        {
            ViewModel.Detector.SendToDetector("CALIBRATE", true);
        }

        /// <summary>
        /// Method for forcing detector to calibrate
        /// </summary>
        /// <param name="obj"></param>
        private void Calibrate(object obj)
        {
            ViewModel.Detector.SendToDetector("CALIBRATE", true);
        }

        /// <summary>
        /// Method for disabling/enabling softstart option for turbopump
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleSoftStart(object obj)
        {
            if (ViewModel.Detector.Turbo.SoftStart)
            {
                ViewModel.Detector.SendToDetector("SOFTSTARTOFF", true);
            }
            else
            {
                ViewModel.Detector.SendToDetector("SOFTSTARTON", true);
            }
        }

        /// <summary>
        /// Method for disabling/enabling autozero if leak value is less than 0
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleAutozero(object obj)
        {
            if (ViewModel.Detector.Settings.AutoZero)
            {
                ViewModel.Detector.SendToDetector("0 INIT-AZ<0", true);
                ViewModel.Detector.SendToDetector("?AZ<0", true);
            }
            else
            {
                ViewModel.Detector.SendToDetector("1 INIT-AZ<0", true);
                ViewModel.Detector.SendToDetector("?AZ<0", true);
            }
        }

        /// <summary>
        /// Method for disabling/enabling zero command
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleZero(object obj)
        {
            if (ViewModel.Detector.Settings.Zero)
            {
                ViewModel.Detector.SendToDetector("DISABLE-0ENABLE", true);
                ViewModel.Detector.SendToDetector("?0ENABLE", true);
            }
            else
            {
                ViewModel.Detector.SendToDetector("ENABLE-0ENABLE", true);
                ViewModel.Detector.SendToDetector("?0ENABLE", true);
            }

        }

        /// <summary>
        /// Method for disabling/enabling splitflow
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleSplitflow(object obj)
        {
            if (ViewModel.Detector.Calibration.Splitflow)
            {
                ViewModel.Detector.SendToDetector("DISABLE-SPLITFLOW", true);
                ViewModel.Detector.SendToDetector("?SPLITFLOW", true);
            }
            else
            {
                ViewModel.Detector.SendToDetector("ENABLE-SPLITFLOW", true);
                ViewModel.Detector.SendToDetector("?SPLITFLOW", true);
            }
        }

        /// <summary>
        /// Method for forcing detector to update ion voltage
        /// </summary>
        /// <param name="obj"></param>
        private void Tune(object obj)
        {
            ViewModel.Detector.SendToDetector("TUNE", true);
        }


        /// <summary>
        /// Method for reseting filament 1 operating hours
        /// </summary>
        /// <param name="obj"></param>
        private void ResetFilament1(object obj)
        {
            ViewModel.Detector.SendToDetector("RESET-FIL1-HRS", true);
        }

        /// <summary>
        /// Method for reseting filament 2 operating hours
        /// </summary>
        /// <param name="obj"></param>
        private void ResetFilament2(object obj)
        {
            ViewModel.Detector.SendToDetector("RESET-FIL2-HRS", true);
        }

        /// <summary>
        /// Method for switching active filament
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleFilament(object obj)
        {
            //TODO
        }

        /// <summary>
        /// Method for toggling reject from High to Low and vise versa
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleRejectHighLow(object obj)
        {
            try
            {
                if (obj != null)
                {
                    string RejectNum = obj.ToString();
                    bool IsHigh = false;
                    switch (RejectNum)
                    {
                        case "1":
                            if (ViewModel.Detector.Reject.Reject1.IsHigh)
                                IsHigh = true;
                            else
                                IsHigh = false;
                            break;
                        case "2":
                            if (ViewModel.Detector.Reject.Reject2.IsHigh)
                                IsHigh = true;
                            else
                                IsHigh = false;
                            break;
                        case "3":
                            if (ViewModel.Detector.Reject.Reject3.IsHigh)
                                IsHigh = true;
                            else
                                IsHigh = false;
                            break;
                        case "4":
                            if (ViewModel.Detector.Reject.Reject4.IsHigh)
                                IsHigh = true;
                            else
                                IsHigh = false;
                            break;
                        case "A":
                            if (ViewModel.Detector.Reject.RejectA.IsHigh)
                                IsHigh = true;
                            else
                                IsHigh = false;
                            break;
                        default: break;
                    }
                    if (IsHigh)
                        ViewModel.Detector.SendToDetector("LOW-" + RejectNum + "REJECT", true);
                    else
                        ViewModel.Detector.SendToDetector("HIGH-" + RejectNum + "REJECT", true);
                }
            }
            catch { }
        }

        /// <summary>
        /// Method for toggling reject from High to Low and vise versa
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleRejectOnOff(object obj)
        {
            try
            {
                if (obj != null)
                {
                    string RejectNum = obj.ToString();
                    bool IsOn = false;
                    switch (RejectNum)
                    {
                        case "1":
                            if (ViewModel.Detector.Reject.Reject1.State)
                                IsOn = true;
                            else
                                IsOn = false;
                            break;
                        case "2":
                            if (ViewModel.Detector.Reject.Reject2.State)
                                IsOn = true;
                            else
                                IsOn = false;
                            break;
                        case "3":
                            if (ViewModel.Detector.Reject.Reject3.State)
                                IsOn = true;
                            else
                                IsOn = false;
                            break;
                        case "4":
                            if (ViewModel.Detector.Reject.Reject4.State)
                                IsOn = true;
                            else
                                IsOn = false;
                            break;
                        case "A":
                            if (ViewModel.Detector.Reject.RejectA.State)
                                IsOn = true;
                            else
                                IsOn = false;
                            break;
                        default: break;
                    }
                    if (IsOn)
                        ViewModel.Detector.SendToDetector("DISABLE-" + RejectNum + "REJECT", true);
                    else
                        ViewModel.Detector.SendToDetector("ENABLE-" + RejectNum + "REJECT", true);
                }
            }
            catch { }
        }

        /// <summary>
        /// Method for setting auto range in detector
        /// </summary>
        /// <param name="obj"></param>
        private void AutoRange(object obj)
        {
            ViewModel.Detector.SendToDetector("AUTO", true);
        }

        /// <summary>
        /// Method for enabling/disabling rangestop
        /// </summary>
        /// <param name="obj"></param>
        private void ToggleRangestop(object obj)
        {
            if (ViewModel.Detector.Range.RangestopActive)
            {
                ViewModel.Detector.SendToDetector("DISABLE-RANGESTOP", true);
                ViewModel.Detector.SendToDetector("?RSONOFF", true);
            }
            else
            {
                ViewModel.Detector.SendToDetector("ENABLE-RANGESTOP", true);
                ViewModel.Detector.SendToDetector("?RSONOFF", true);
            }
        }

        /// <summary>
        /// Method for changing selected calibration type
        /// </summary>
        /// <param name="obj"></param>
        private void ChangeCalibrationType(object obj)
        {
            if(ViewModel.Detector.Calibration.CalibrationSelection == 0)            
                ViewModel.Detector.SendToDetector("INTERNAL", true);
            else
                ViewModel.Detector.SendToDetector("EXTERNAL", true);

        }
        #endregion

    }
}
