using Downloader;
using System.ComponentModel;
using System.Windows.Controls;

namespace Zerolauncher.dialog
{
    /// <summary>
    /// DownloadControl.xaml 的交互逻辑
    /// </summary>
    public partial class DownloadControl1 : UserControl
    {
        const string cache_dir = "./.cache/auto.cache";
        static bool is_strat = false;

        public DownloadControl1()
        {
            InitializeComponent();
            if (!is_strat) text2.Text = "正在连接服务器";
        }

        

        public static void DoDownload(string fileUrl) 
        {
            var downloadOpt = new DownloadConfiguration()
            {
                ChunkCount = 8, // file parts to download, the default value is 1
                ParallelDownload = true // download parts of the file as parallel or not. The default value is false
            };
            var downloader = new DownloadService(downloadOpt);
            // Provide `FileName` and `TotalBytesToReceive` at the start of each download
            downloader.DownloadStarted += OnDownloadStarted;

            // Provide any information about download progress, 
            // like progress percentage of sum of chunks, total speed, 
            // average speed, total received bytes and received bytes array 
            // to live streaming.
            downloader.DownloadProgressChanged += OnDownloadProgressChanged;

            // Download completed event that can include errors or 
            // canceled or download completed successfully.
            downloader.DownloadFileCompleted += OnDownloadFileCompleted;
        }

        private static void OnDownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
        {
            if (UpdateDialog.ui_enale)
            {
                UpdateDialog.Close();
            }
        }

        private static void OnDownloadProgressChanged(object? sender, DownloadProgressChangedEventArgs e)
        {
            if (UpdateDialog.ui_enale)
            {
                UpdateDialog.editControl.pbDown.Value = e.ProgressPercentage;
                UpdateDialog.editControl.text2.Text = $"{e.AverageBytesPerSecondSpeed}";
            }
        }

        private static void OnDownloadStarted(object? sender, DownloadStartedEventArgs e)
        {
            is_strat = true;
            if (UpdateDialog.ui_enale) UpdateDialog.editControl.text1.Text = "零蛋正在更新装备...";
        }

    }
}
