using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Windows;
using System.Windows.Input;

using XHWK.Model;
using XHWK.WKTool.Helpers;

using static XHWK.WKTool.Helpers.ZJDownloadUtil;

namespace XHWK.WKTool
{
    /// <summary>
    /// AppUpdateWin.xaml 的交互逻辑
    /// </summary>
    public partial class AppUpdateWin : Window, ZJDownloadCallback
    {
        private readonly AppUpdatePageModel pageData = new AppUpdatePageModel();

        public AppUpdateWin(Model_App app)
        {
            InitializeComponent();
            pageData.appModel = app;

            DataContext = pageData;
        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        public class AppUpdatePageModel : NotifyModel
        {
            public Model_App appModel { get; set; }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void gengxinClick(object sender, RoutedEventArgs e)
        {
            string fileUrl = App.showImageUrl + pageData.appModel.versionpath;
            string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
            DirectoryInfo info = new DirectoryInfo(path);
            if (!info.Exists)
            {
                info.Create();
            }

            string filename = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);
            if (filename != null)
            {
                ZJDownloadUtil.downloadFileWithCallback(fileUrl, 999, Dispatcher, this);
            }
        }

        public void downloadBegin(int position)
        {
            ProgressSp.Visibility = Visibility.Visible;
            ContentSv.Visibility = Visibility.Collapsed;
            ButtonBottom.Visibility = Visibility.Collapsed;
        }

        public void downloadProgress(int position, int progress)
        {
            Mprogress.Value = progress;
            ParogressTb.Text = $"正在更新，已下载{progress}%";
        }

        public void downloadEnd(int position, string filepath)
        {
            ProgressSp.Visibility = Visibility.Collapsed;
            ContentSv.Visibility = Visibility.Visible;
            ButtonBottom.Visibility = Visibility.Visible;
            try
            {
                using (var managementClass = new ManagementClass("Win32_Process"))
                {
                    var processInfo = new ManagementClass("Win32_ProcessStartup");
                    processInfo.Properties["CreateFlags"].Value = 0x00000008;

                    var inParameters = managementClass.GetMethodParameters("Create");
                    inParameters["CommandLine"] = filepath;
                    inParameters["ProcessStartupInformation"] = processInfo;

                    var result = managementClass.InvokeMethod("Create", inParameters, null);
                    if ((result != null) && ((uint)result.Properties["ReturnValue"].Value != 0))
                    {
                        Console.WriteLine("Process ID: {0}", result.Properties["ProcessId"].Value);
                    }
                }
            }
            catch (Exception)
            {
            }
            Environment.Exit(0);
        }

        public void downloadError(int position, string msg)
        {
            MessageWindow.Show(msg);
        }
    }
}
