using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Resources;
using VSview.Properties;
using System.Linq;

namespace VSview
{
    public class MainViewModel : BaseViewModel
    {
        #region Public properties

        /// <summary>
        /// Control module class - contains setting and communicaiton
        /// </summary>
        public Turck ControlModule { get; set; }

        /// <summary>
        /// Detector class - contains setting and communication
        /// </summary>
        public Detector Detector { get; set; }

        /// <summary>
        /// Graph class - contains interaction and settings
        /// </summary>
        public Graph Graph { get; set; }

        #endregion

        #region Child view models
        /// <summary>
        /// Measurement tab view model
        /// </summary>
        public MeasurementViewModel MeasurementViewModel { get; set; }

        /// <summary>
        /// Service tab view model
        /// </summary>
        public ServiceViewModel ServiceViewModel { get; set; }

        /// <summary>
        /// Settings tab view model
        /// </summary>
        public SettingViewModel SettingViewModel { get; set; }

        /// <summary>
        /// Parameter tab view model
        /// </summary>
        public ParameterViewModel ParameterViewModel { get; set; }

        /// <summary>
        /// Terminal tab view model
        /// </summary>
        public TerminalViewModel TerminalViewModel { get; set; }

        /// <summary>
        /// View model for status window
        /// </summary>
        public StatusViewModel StatusViewModel { get; set; }
        #endregion

        #region Constructors

        /// <summary>
        /// Main view model constructor
        /// </summary>
        public MainViewModel()
        {

            //Order is important

            Detector = new Detector(this);

            ControlModule = new Turck(this);

            Graph = new Graph(this);

            TerminalViewModel = new TerminalViewModel(this);

            ParameterViewModel = new ParameterViewModel(this);

            SettingViewModel = new SettingViewModel(this);

            MeasurementViewModel = new MeasurementViewModel(this);

            ServiceViewModel = new ServiceViewModel(this);

            StatusViewModel = new StatusViewModel();

            SetupCommands();

            LoadSettings();

        }

        #endregion

        #region Commands

        /// <summary>
        /// Commands associated with Main window - close, minimize, maximize...
        /// </summary>
        public Dictionary<string, RelayCommand> WindowCommands { get; set; }

        /// <summary>
        /// Method for creating commands assosiated with window
        /// </summary>
        private void SetupCommands()
        {
            //Create new instance of dictionary for window commands
            WindowCommands = new Dictionary<string, RelayCommand>();

            //Insert window button commands
            WindowCommands.Add("Exit", new RelayCommand(Exit));
            WindowCommands.Add("Resize", new RelayCommand(Resize));
            WindowCommands.Add("Minimize", new RelayCommand(Minimize));
            WindowCommands.Add("MoveWindow", new RelayCommand(MoveWindow));
        }

        /// <summary>
        /// Closes apllication
        /// </summary>
        /// <param name="obj"></param>
        private void Exit(object obj)
        {
            SaveSettings();
            Detector.Communication.CloseSerial();
            ControlModule.DisconnectTurck();
            Application.Current.Shutdown();
        }

        /// <summary>
        /// Resizes window - fullscreen / normal
        /// </summary>
        /// <param name="obj"></param>
        private void Resize(object obj)
        {
            if(Application.Current.MainWindow.WindowState != WindowState.Normal)
            {
                Application.Current.MainWindow.WindowState = WindowState.Normal;
            }
            else
            {
                Application.Current.MainWindow.WindowState = WindowState.Maximized;
            }
        
        }

        /// <summary>
        /// Minimazes window
        /// </summary>
        /// <param name="obj"></param>
        private void Minimize(object obj)
        {
            Application.Current.Windows[0].WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// Command for moving main window
        /// </summary>
        /// <param name="obj"></param>
        private void MoveWindow(object obj)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed)
                Application.Current.Windows[0].DragMove();
        }

        #endregion

        #region Settings
        /// <summary>
        /// Method for saving user settings when closing application
        /// </summary>
        private void SaveSettings()
        {
            try
            {
                Settings.Default.Analog1 = MeasurementViewModel.DisplayList[3];
                Settings.Default.Analog2 = MeasurementViewModel.DisplayList[4];
                Settings.Default.Analog3 = MeasurementViewModel.DisplayList[5];
                Settings.Default.Analog4 = MeasurementViewModel.DisplayList[6];


                Settings.Default.IP = SettingViewModel.IPAddress;
                Settings.Default.MAC = SettingViewModel.MACAddress;

                Settings.Default.Language = SettingViewModel.Languages[SettingViewModel.LanguageIndex];
                Settings.Default.ControlModule = SettingViewModel.ControlModules[SettingViewModel.ControlIndex];

                Settings.Default.IPchecked = SettingViewModel.UsingIP;
                Settings.Default.MACchecked = SettingViewModel.UsingMAC;

                Settings.Default.Save();
            }
            catch (Exception ex) { MessageBox.Show("Error occured while saving settings", "Settings error", MessageBoxButton.OK, MessageBoxImage.Warning); }
        }

        /// <summary>
        /// Method for loading user settings when openning application
        /// </summary>
        private void LoadSettings()
        {
            try
            {
                MeasurementViewModel.DisplayList[3] = Settings.Default.Analog1;
                MeasurementViewModel.DisplayList[4] = Settings.Default.Analog2;
                MeasurementViewModel.DisplayList[5] = Settings.Default.Analog3;
                MeasurementViewModel.DisplayList[6] = Settings.Default.Analog4;

                SettingViewModel.IPAddress = Settings.Default.IP;
                SettingViewModel.MACAddress = Settings.Default.MAC;

                SettingViewModel.LanguageIndex = SettingViewModel.Languages.IndexOf(Settings.Default.Language);

                SettingViewModel.ControlIndex = SettingViewModel.ControlModules.IndexOf(Settings.Default.ControlModule);


                SettingViewModel.UsingIP = Settings.Default.IPchecked;
                SettingViewModel.UsingMAC = Settings.Default.MACchecked;
            }
            catch (Exception ex) { MessageBox.Show("Error occured while loading settings", "Settings error", MessageBoxButton.OK, MessageBoxImage.Warning); }
        }
        #endregion
    }
}
