using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 电子展板.Base;
using 电子展板.Models;
using 电子展板.Utility;
using 电子展板.Utility.Core;
using 电子展板.Utility.Extension;

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

        private void Loaded(Window window)
        {
            this.window = window;
            int screenIndex = 0;
            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());

            WebServer.Start();
            //GlobalVariable.WebServer = new NancyHost(new Uri("http://localhost:80"));
            //GlobalVariable.WebServer.Start();
        }

        private void WindowClosing(CancelEventArgs args)
        {
            WebServer.Stop();
            //GlobalVariable.WebServer?.Stop();
            //GlobalVariable.WebServer?.Dispose();
            //GlobalVariable.WebServer = null;
            Environment.Exit(0);
        }
        public double GetDpiScaleX()
        {
            return VisualTreeHelper.GetDpi(this.window).DpiScaleX;
        }


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

        private void InitImage()
        {
            Image1 = GetImage(Config.CPCMember1Path);
            Image2 = GetImage(Config.CPCMember2Path);
            Image3 = GetImage(Config.CPCMember3Path);
            Image4 = GetImage(Config.CPCMember4Path);
        }

        private ImageSource GetImage(string path)
        {
            try
            {
                if (path.IsNullOrEmpty())
                    return null;
                UploadFiles uploadFiles = GlobalVariable.Config.FileUploadList.FirstOrDefault(x => path.EndsWith(x.Name));
                if (uploadFiles == null)
                    return null;
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = new ByteArrayStream(uploadFiles.Bin);
                image.EndInit();
                image.Freeze();
                return image;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
