using Azure;
using BOB;
using Common.PubEvent;
using MahApps.Metro.Controls;
using Prism.Ioc;
using ProcessManager.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reflection.Metadata;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace ProcessManager.ViewModels
{
    public class PMainViewModel:BindableBase
    {
        private string _currentTag;
        public string CurrentTag
        {
            get => _currentTag;
            set => SetProperty(ref _currentTag, value);
        }

        private readonly IDialogService _dialogService;
        private  IRegionManager _regionManager;
        private  IEventAggregator _eventAggregator;
        public ICommand MinimizeCommand { get; set; }
        public ICommand MaximizeCommand { get; set; }
        public ICommand CloseCommand { get; set; }
        public ICommand NavigateToMenuCommand { get; set; }
        public ICommand NavigateToDeviceCommand { get; set; }
        public PMainViewModel(IContainerProvider containerProvider)
        {
            containerProvider = containerProvider;
            _eventAggregator=containerProvider.Resolve<IEventAggregator>();
            _dialogService = containerProvider.Resolve<IDialogService>();
            _regionManager = containerProvider.Resolve<IRegionManager>();
            NavigateToMenuCommand = new DelegateCommand(NavigateToMenu);
            NavigateToDeviceCommand = new DelegateCommand<string>(NavigateToDevice);
            MinimizeCommand = new DelegateCommand<Window>(MinimizeWindow);
            MaximizeCommand = new DelegateCommand<Window>(MaximizeWindow);
            CloseCommand = new DelegateCommand<Window>(CloseWindow);
            _eventAggregator.GetEvent<ChangeCurrentTagEvent>().Subscribe(ChangeCurrentTag);
        }

        private void ChangeCurrentTag(string obj)
        {
            CurrentTag = obj;
        }

        private void NavigateToDevice(string page)
        {
            var parameters = new NavigationParameters
                {
                    { "Title", page },
                };
            _regionManager.RequestNavigate("MainRegion", page,parameters);
        }

        private void NavigateToMenu()
        {
            _regionManager.RequestNavigate("MainRegion", "Menu");
        }
        private void MinimizeWindow(Window window)
        {
            if (window != null)
                window.WindowState = WindowState.Minimized;
        }

        private void MaximizeWindow(Window window)
        {
            if (window != null)
            {
                window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            }
        }

        private void CloseWindow(Window window)
        {
            window?.Close();
        }

    }
}
