using Client.Helpers;
using Client.ISServiceRef;
using Client.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Client.ViewModel
{
    public class MainWindowViewModel : ViewModelBase, INotifyPropertyChanged
    {
        public object FrameContent { get; set; }
        public string ServerConnectionStatusColor { get; set; } = "#FFFF00";
        public string ServerConnectionStatus { get; set; } = Properties.Resources.ServerConnectionStatusConnecting;
        public string LoggedUser { get; set; }


        public ICommand ShowEmployeesViewCommand { get; set; }
        public ICommand ShowDepartmentsViewCommand { get; set; }
        public ICommand ShowProjectsViewCommand { get; set; }
        public ICommand ShowEditAttendanceViewCommand { get; set; }
        public ICommand ShowEditRequestsViewCommand { get; set; }
        public ICommand ShowEmployeesAttendanceViewCommand { get; set; }
        public ICommand ShowPersonalEventsCommand { get; set; }
        public ICommand ShowInfoPanelCommand { get; set; }
        public ICommand ShowEditTimeSheetsViewCommand { get; set; }
        public ICommand ShowEmployeesTimeSheetsViewCommand { get; set; }
        public ICommand ShowEmployeePositionsViewCommand { get; set; }
        public ICommand ShowUserRolesCommand { get; set; }
        public ICommand LogoutCommand { get; set; }
        public ICommand OpenUserSettingsCommand { get; set; }

        public MainWindowViewModel()
        {
            ShowEmployeesViewCommand = new RelayCommand(ShowEmployeesView);
            ShowDepartmentsViewCommand = new RelayCommand(ShowDepartmentsView);
            ShowProjectsViewCommand = new RelayCommand(ShowProjectsView);
            ShowEditAttendanceViewCommand = new RelayCommand(ShowEditAttendanceView);
            ShowEditRequestsViewCommand = new RelayCommand(ShowRequestsView);
            ShowEmployeesAttendanceViewCommand = new RelayCommand(ShowEmployeesAttendanceView);
            ShowPersonalEventsCommand = new RelayCommand(ShowPersonalEventsView);
            ShowInfoPanelCommand = new RelayCommand(ShowInfoPanelView);
            ShowEditTimeSheetsViewCommand = new RelayCommand(ShowEditTimeSheetsView);
            ShowEmployeesTimeSheetsViewCommand = new RelayCommand(ShowEmployeesTimeSheetsView);
            ShowEmployeePositionsViewCommand = new RelayCommand(ShowEmployeePositionsView);
            ShowUserRolesCommand = new RelayCommand(ShowUserRolesView);
            LogoutCommand = new RelayCommand<IClosable>(Logout);
            OpenUserSettingsCommand = new RelayCommand(OpenUserSettings);


            LoggedUser = $"{UserManager.Instance.CurrentUser.Employee.FirstName} {UserManager.Instance.CurrentUser.Employee.LastName}";

            _connectionServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            _connectionServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            _connectionServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            Task.Run(async () => await RunServerConnectionCheckAsync());

            ShowInfoPanelView();

        }


        private void ShowEmployeesView()
        {
            var employeeServiceClient = new EmployeeServiceClient();

            employeeServiceClient.ClientCredentials.UserName.UserName = UserManager.Instance.CurrentUser.Login;
            employeeServiceClient.ClientCredentials.UserName.Password = UserManager.Instance.CurrentUser.Password;
            employeeServiceClient.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            try
            {
                employeeServiceClient.CheckAccessViewEmployees();
                FrameContent = new EmployeesView();
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show(Properties.Resources.Unauthorized, string.Empty, MessageBoxButton.OK, MessageBoxImage.Warning);
            }

        }

        private void ShowDepartmentsView()
        {
            FrameContent = new DepartmentsView();
        }

        private void ShowProjectsView()
        {
            FrameContent = new ProjectsView();
        }

        private void ShowEditAttendanceView()
        {
            FrameContent = new EditAttendanceView();
        }

        private void ShowRequestsView()
        {
            FrameContent = new EditRequestsView();
        }

        private void ShowEmployeesAttendanceView()
        {
            FrameContent = new EmployeesAttendanceView();
        }

        private void ShowPersonalEventsView()
        {
            FrameContent = new PersonalEventsView();
        }

        private void ShowInfoPanelView()
        {
            FrameContent = new InfoPanelView();
        }

        private void ShowEditTimeSheetsView()
        {
            FrameContent = new EditTimeSheetsView();
        }

        private void ShowEmployeesTimeSheetsView()
        {
            FrameContent = new EmployeesTimeSheetsView();
        }

        private void ShowEmployeePositionsView()
        {
            FrameContent = new PositionsView();
        }

        private void ShowUserRolesView()
        {
            FrameContent = new UserRolesView();
        }

        private void Logout(IClosable window)
        {
            var loginWindow = new LoginView();
            loginWindow.Show();
            window.Close();
        }

        private void OpenUserSettings()
        {
            var changePasswordWindow = new ChangePasswordView();
            changePasswordWindow.ShowDialog();
        }
        private async Task RunServerConnectionCheckAsync()
        {
            while (true)
            {
                try
                {
                    _connectionServiceClient.CheckConnection();

                    ServerConnectionStatusColor = "#00FF00";
                    ServerConnectionStatus = Properties.Resources.ServerConnectionStatusOk;
                }
                catch (Exception e)
                {
                    ServerConnectionStatusColor = "#FF0000";
                    ServerConnectionStatus = Properties.Resources.ServerConnectionStatusNotOk;
                }
                finally
                {
                    await Task.Delay(10000);
                }
            }
        }

        private ConnectionServiceClient _connectionServiceClient = new ISServiceRef.ConnectionServiceClient();

    }
}
