using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using NetSparkleUpdater.Interfaces;
using NetSparkleUpdater.Events;
namespace NetSparkleUpdater.UI.WinForms
{
public partial class DownloadProgressWindow : Form, IDownloadProgress
{
public event DownloadInstallEventHandler? DownloadProcessCompleted;
private bool _shouldLaunchInstallFileOnClose = false;
private bool _didCallDownloadProcessCompletedHandler = false;
public DownloadProgressWindow(string downloadTitle, string actionButtonTitle, Icon? applicationIcon)
{
InitializeComponent();
if (applicationIcon != null)
{
imgAppIcon.Image = new Icon(applicationIcon, new Size(48, 48)).ToBitmap();
}
Icon = applicationIcon;
btnInstallAndReLaunch.Visible = false;
btnInstallAndReLaunch.Text = actionButtonTitle;
lblHeader.Text = downloadTitle;
downloadProgressLbl.Text = "";
progressDownload.Maximum = 100;
progressDownload.Minimum = 0;
progressDownload.Step = 1;
FormClosing += DownloadProgressWindow_FormClosing;
}
private void DownloadProgressWindow_FormClosing(object? sender, FormClosingEventArgs e)
{
FormClosing -= DownloadProgressWindow_FormClosing;
if (!_didCallDownloadProcessCompletedHandler)
{
_didCallDownloadProcessCompletedHandler = true;
DownloadProcessCompleted?.Invoke(this, new DownloadInstallEventArgs(_shouldLaunchInstallFileOnClose));
}
}
void IDownloadProgress.Show()
{
Show();
}
public void FinishedDownloadingFile(bool isDownloadedFileValid)
{
if (InvokeRequired)
{
Invoke((MethodInvoker)delegate () { FinishedDownloadingFile(isDownloadedFileValid); });
}
else
{
progressDownload.Visible = false;
buttonCancel.Visible = false;
downloadProgressLbl.Visible = false;
if (isDownloadedFileValid)
{
btnInstallAndReLaunch.Visible = true;
BackColor = Color.FromArgb(240, 240, 240);
lblHeader.Text = "Download Complete";
}
else
{
btnInstallAndReLaunch.Visible = false;
BackColor = Color.Tomato;
lblHeader.Text = "Download Error";
}
}
}
public bool DisplayErrorMessage(string errorMessage)
{
if (InvokeRequired)
{
this.Invoke((MethodInvoker)delegate () { DisplayErrorMessage(errorMessage); });
}
else
{
downloadProgressLbl.Visible = true;
progressDownload.Visible = false;
btnInstallAndReLaunch.Visible = false;
buttonCancel.Text = "Close";
downloadProgressLbl.Text = errorMessage;
}
return true;
}
void IDownloadProgress.Close()
{
DialogResult = DialogResult.Abort;
CloseForm();
}
private void CloseForm()
{
if (InvokeRequired)
{
this.Invoke((MethodInvoker)delegate () { Close(); });
}
else
{
Close();
}
}
private void OnDownloadProgressChanged(object sender, long bytesReceived, long totalBytesToReceive, int percentage)
{
if (InvokeRequired)
{
this.Invoke((MethodInvoker)delegate () { OnDownloadProgressChanged(sender, bytesReceived, totalBytesToReceive, percentage); });
}
else
{
progressDownload.Style = ProgressBarStyle.Continuous;
progressDownload.Value = percentage;
downloadProgressLbl.Text = "(" + Utilities.ConvertNumBytesToUserReadableString(bytesReceived) + " / " +
Utilities.ConvertNumBytesToUserReadableString(totalBytesToReceive) + ")";
}
}
public void OnDownloadProgressChanged(object sender, ItemDownloadProgressEventArgs e)
{
OnDownloadProgressChanged(sender, e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}
private void OnInstallAndReLaunchClick(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke((MethodInvoker)delegate () { OnInstallAndReLaunchClick(sender, e); });
}
else
{
DialogResult = DialogResult.OK;
_shouldLaunchInstallFileOnClose = true;
_didCallDownloadProcessCompletedHandler = true;
DownloadProcessCompleted?.Invoke(this, new DownloadInstallEventArgs(true));
}
}
public void SetDownloadAndInstallButtonEnabled(bool shouldBeEnabled)
{
if (InvokeRequired)
{
Invoke((MethodInvoker)delegate () { SetDownloadAndInstallButtonEnabled(shouldBeEnabled); });
}
else
{
btnInstallAndReLaunch.Enabled = shouldBeEnabled;
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
_didCallDownloadProcessCompletedHandler = true;
DownloadProcessCompleted?.Invoke(this, new DownloadInstallEventArgs(false));
}
}
}