using System;
using System.Diagnostics;
using System.IO;
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 temp = System.Environment.GetEnvironmentVariable("TEMP");
            DirectoryInfo info = new DirectoryInfo(temp);
            if (!info.Exists)
            {
                info.Create();
            }

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

        public void downloadBegin(int position)
        {
            progress_sp.Visibility = Visibility.Visible;
            content_sv.Visibility = Visibility.Collapsed;
            button_bottom.Visibility = Visibility.Collapsed;
        }

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

        public void downloadEnd(int position, string filepath)
        {
            //System.Console.WriteLine("filepath:" + filepath);
            progress_sp.Visibility = Visibility.Collapsed;
            content_sv.Visibility = Visibility.Visible;
            button_bottom.Visibility = Visibility.Visible;
            ProcessStartInfo psi = new ProcessStartInfo(filepath);
            Process pro = new Process
            {
                StartInfo = psi
            };
            pro.Start();
            Dispatcher.Invoke(new Action(() =>
            {
                System.Environment.Exit(0);
            }));
            Close();
        }

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