using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using McLaren_WPF.Interfaces;
using Microsoft.Practices.Unity;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Threading;
using DPackLib;

namespace McLaren_WPF.ViewModels
{
    public class ShellViewModel : BindableBase, IShellViewModel, IDisposable
    {
        [Dependency]public IRegionManager RegionManager { get; set; }
        [Dependency]public IUnityContainer Container { get; set; }
        [Dependency]public IEventAggregator EventAggregator { get; set; }

        public DelegateCommand<string> NavigateCommand { get; set; }
        private Process genProcess { get; set; }
        private DispatcherTimer startTimer;

        [InjectionMethod]
        public void InitializeShellView()
        {
            NavigateCommand = new DelegateCommand<string>(Navigate);
            LDataBackground = new SolidColorBrush(Color.FromRgb(170, 77, 114));
            CreateStartTimer();
        }

        private void CreateStartTimer()
        {
            startTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(5) };
            startTimer.Tick += GenTimer_Tick;
            startTimer.Start();
        }

        private void GenTimer_Tick(object sender, EventArgs e)
        {
            startTimer.Stop();
            StartButtonVisibility=Visibility.Collapsed;
            Navigate("StartGenerators");            
        }

        private void Navigate(string uri)
        {
            foreach (var view in RegionManager.Regions["MainRegion"].Views)
            { RegionManager.Regions["MainRegion"].Remove(view); }

            switch (uri)
            {
                case "StartGenerators":
                    RegionManager.AddToRegion("MainRegion", Container.Resolve<MonitorsSetViewModel>());
                    if (File.Exists(CommonConstants.GeneratorsPath))
                    {
                        try
                        {
                            genProcess = StartProc(CommonConstants.GeneratorsPath);
                        } 
                        catch
                        {
                            MessageBox.Show(CommonConstants.GeneratorsPath + "\r\nNot Started!");
                        }
                    }
                    else
                    {
                        try
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(CommonConstants.GeneratorsPath));
                            File.Copy(Path.GetFileName(CommonConstants.GeneratorsPath), CommonConstants.GeneratorsPath);
                            File.Copy(Path.GetFileName(CommonConstants.LibPath), CommonConstants.LibPath);
                            genProcess = StartProc(CommonConstants.GeneratorsPath);
                        }
                        catch { MessageBox.Show(Path.GetDirectoryName(CommonConstants.GeneratorsPath) + "\r\nNot Copied!"); }
                    }
                    StartButtonVisibility = Visibility.Collapsed;
                    startTimer.Stop();
                    break;

                case "Shutdown":
                    StopProc(genProcess);
                    Application.Current.Shutdown();
                    break;
            }
        }

        public Process StartProc(string procname)
        {
            var proc = new Process {StartInfo = {FileName = procname}};
            proc.Start();
            return proc;
        }

        public void StopProc(Process proc)
        {
            proc.CloseMainWindow();

            Thread.Sleep(1000);

            if (Directory.Exists(CommonConstants.RootDir))
            {
                try {Directory.Delete(CommonConstants.RootDir,true); }catch {};                
            }

            if (Directory.Exists(CommonConstants.RootDir))
                try
                {
                    var datFiles = Directory.GetFiles(CommonConstants.RootDir).Where(x => x.EndsWith(".dat"));
                    foreach (var datFile in datFiles)
                    {
                        File.Delete(datFile);
                    }
                } catch { };

        }

        #region Binding

        private SolidColorBrush _lDataBackground;

        public SolidColorBrush LDataBackground
        {
            get { return _lDataBackground; }
            set { SetProperty(ref _lDataBackground, value); }
        }

        private Visibility _startButtonVisibility;

        public Visibility StartButtonVisibility
        {
            get { return _startButtonVisibility; }
            set { SetProperty(ref _startButtonVisibility, value); }
        }

        #endregion

        public void Dispose()
        {
            if (Container != null)
            {
                Container.Dispose();
                Container = null;
            }
        }
    }
}
