using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;

namespace QC.QMine.QMineInterface;

public partial class ProgressWindow : Window, IComponentConnector
{
	private bool hideRunDetails;

	public Action CancelAction { get; set; }

	public string ProgressText
	{
		get
		{
			return ProgressTitle.Text;
		}
		set
		{
			ProgressTitle.Text = value;
		}
	}

	public ProgressBar ReportProgressBar => ProgressBar1;

	public bool IsCancelEnabled
	{
		get
		{
			return CancelButton.IsEnabled;
		}
		set
		{
			CancelButton.IsEnabled = value;
		}
	}

	public ProgressWindow()
	{
		InitializeComponent();
		base.Owner = Constants.MainWindow;
	}

	private void CancelButton_Click(object sender, RoutedEventArgs e)
	{
		if (CancelAction != null)
		{
			CancelTextBlock.Text = "Cancelling Please wait ...";
			CancelButton.IsEnabled = false;
			CancelAction();
		}
	}

	public void HideWindow()
	{
		Hide();
	}

	public void HideRunDetails()
	{
		hideRunDetails = true;
	}

	private void RunDetailsExpander_Collapsed(object sender, RoutedEventArgs e)
	{
		base.Height = 100.0;
		base.Width = 300.0;
		if (e != null)
		{
			e.Handled = true;
		}
	}

	private void RunDetailsExpander_Expanded(object sender, RoutedEventArgs e)
	{
		base.Height = 400.0;
		base.Width = 600.0;
		e.Handled = true;
	}

	private void UpdateDetailsBinding()
	{
		RunDetailsExpander.DataContext = null;
		RunDetailsExpander.DataContext = OutputLogger.OutputCollection.Last();
	}

	private void ProgressWindow_Loaded(object sender, RoutedEventArgs e)
	{
		UpdateDetailsBinding();
	}

	private void ProgressWindowVisibility_Changed(object sender, DependencyPropertyChangedEventArgs e)
	{
		if (base.Visibility == Visibility.Visible)
		{
			UpdateDetailsBinding();
			if (hideRunDetails)
			{
				RunDetailsExpander_Collapsed(null, null);
				RunDetailsExpander.Visibility = Visibility.Collapsed;
				hideRunDetails = false;
			}
			else
			{
				RunDetailsExpander.Visibility = Visibility.Visible;
			}
		}
	}

	private void ProgressWindow_MouseDown(object sender, MouseButtonEventArgs e)
	{
		if (e.ChangedButton == MouseButton.Left)
		{
			DragMove();
		}
	}
}
