using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using 电子展板.Base;
using 电子展板.Utility;
using 电子展板.Utility.Extension;

namespace 电子展板.ViewModels
{
    public class MainWindow2ViewModel : ViewModelBase
    {
        private Window window;
        public DelegateCommand<Window> LoadedCommand { get; set; }
        public DelegateCommand<CancelEventArgs> WindowClosingCommand { get; set; }
        public ImageSource Image1 { get; set; }
        public ImageSource Image2 { get; set; }
        public ImageSource Image3 { get; set; }
        public ImageSource Image4 { get; set; }
        public MainWindow2ViewModel()
        {
            LoadedCommand = new DelegateCommand<Window>(Loaded);
            WindowClosingCommand = new DelegateCommand<CancelEventArgs>(WindowClosing);
            EventBus.Instance.Subscribe(this, "save", ReloadImage);
            InitImage();
        }

        private void ReloadImage(string obj)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                InitImage();
            });
        }

        private void InitImage()
        {
            try
            {
                BitmapImage image1 = new BitmapImage();
                image1.BeginInit();
                image1.CacheOption = BitmapCacheOption.OnLoad;
                image1.StreamSource = new MemoryStream(File.ReadAllBytes(MyEnvironment.Root(Config.CPCMember1Path)));
                image1.EndInit();
                image1.Freeze();
                Image1 = image1;
            }
            catch (Exception ex)
            {
                Image1 = null;
            }
            try
            {
                BitmapImage image2 = new BitmapImage();
                image2.BeginInit();
                image2.CacheOption = BitmapCacheOption.OnLoad;
                image2.StreamSource = new MemoryStream(File.ReadAllBytes(MyEnvironment.Root(Config.CPCMember2Path)));
                image2.EndInit();
                image2.Freeze();
                Image2 = image2;
            }
            catch (Exception ex)
            {
                Image2 = null;

            }
            try
            {
                BitmapImage image3 = new BitmapImage();
                image3.BeginInit();
                image3.CacheOption = BitmapCacheOption.OnLoad;
                image3.StreamSource = new MemoryStream(File.ReadAllBytes(MyEnvironment.Root(Config.CPCMember3Path)));
                image3.EndInit();
                image3.Freeze();
                Image3 = image3;
            }
            catch (Exception ex)
            {
                Image3 = null;
            }
            try
            {
                BitmapImage image4 = new BitmapImage();
                image4.BeginInit();
                image4.CacheOption = BitmapCacheOption.OnLoad;
                image4.StreamSource = new MemoryStream(File.ReadAllBytes(MyEnvironment.Root(Config.CPCMember4Path)));
                image4.EndInit();
                image4.Freeze();
                Image4 = image4;
            }
            catch (Exception ex)
            {
                Image4 = null;
            }
        }


        private void Loaded(Window window)
        {
            this.window = window;
            Screen[] screens = Screen.AllScreens;
            if (screens.Length > 1)
            {
                int screenIndex = 1;
                this.window.Left = (int)(Screen.AllScreens[screenIndex].Bounds.Left / GetDpiScaleX());
                this.window.Top = (int)(Screen.AllScreens[screenIndex].Bounds.Top / GetDpiScaleX());
                this.window.Width = (int)(Screen.AllScreens[screenIndex].Bounds.Width / GetDpiScaleX());
                this.window.Height = (int)(Screen.AllScreens[screenIndex].Bounds.Height / GetDpiScaleX());
            }
            else
            {
                window.Close();
            }
        }


        /// <summary>
        /// 界面关闭时
        /// </summary>
        public void WindowClosing(CancelEventArgs args)
        {
            WebServer.Stop();
            Screen[] screens = Screen.AllScreens;
            if (screens.Length > 1)
            {
                Environment.Exit(0);
            }
        }


        public double GetDpiScaleX()
        {
            return VisualTreeHelper.GetDpi(this.window).DpiScaleX;
        }
    }
}
