using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using AxCopilot.Models;
using AxCopilot.Services;

namespace AxCopilot.Views;

public class ReminderPopupWindow : Window, IComponentConnector
{
	private const int GWL_EXSTYLE = -20;

	private const int WS_EX_NOACTIVATE = 134217728;

	private const int WS_EX_TOOLWINDOW = 128;

	private readonly DispatcherTimer _timer;

	private readonly EventHandler _tickHandler;

	private int _remaining;

	internal Border PopupBorder;

	internal TextBlock UsageText;

	internal Button CloseBtn;

	internal TextBlock QuoteText;

	internal TextBlock AuthorText;

	internal ProgressBar CountdownBar;

	private bool _contentLoaded;

	[DllImport("user32.dll")]
	private static extern nint GetWindowLongPtr(nint hwnd, int nIndex);

	[DllImport("user32.dll")]
	private static extern nint SetWindowLongPtr(nint hwnd, int nIndex, nint dwNewLong);

	public ReminderPopupWindow(string quoteText, string? author, TimeSpan todayUsage, SettingsService settings)
	{
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Expected O, but got Unknown
		InitializeComponent();
		ReminderSettings cfg = settings.Settings.Reminder;
		QuoteText.Text = quoteText;
		if (!string.IsNullOrWhiteSpace(author))
		{
			AuthorText.Text = "— " + author;
			AuthorText.Visibility = Visibility.Visible;
		}
		int num = (int)todayUsage.TotalHours;
		int minutes = todayUsage.Minutes;
		UsageText.Text = ((num > 0) ? $"오늘 총 {num}시간 {minutes}분 근무 중" : ((minutes >= 1) ? $"오늘 총 {minutes}분 근무 중" : "오늘 방금 시작했습니다"));
		_remaining = Math.Max(3, cfg.DisplaySeconds);
		CountdownBar.Maximum = _remaining;
		CountdownBar.Value = _remaining;
		base.Loaded += delegate
		{
			SetNoActivate();
			PositionWindow(cfg.Corner);
			AnimateIn();
		};
		_tickHandler = delegate
		{
			_remaining--;
			CountdownBar.Value = _remaining;
			if (_remaining <= 0)
			{
				Close();
			}
		};
		_timer = new DispatcherTimer
		{
			Interval = TimeSpan.FromSeconds(1.0)
		};
		_timer.Tick += _tickHandler;
		_timer.Start();
		base.KeyDown += delegate(object _, KeyEventArgs e)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Invalid comparison between Unknown and I4
			if ((int)e.Key == 13)
			{
				Close();
			}
		};
	}

	protected override void OnSourceInitialized(EventArgs e)
	{
		base.OnSourceInitialized(e);
		SetNoActivate();
	}

	private void SetNoActivate()
	{
		nint handle = new WindowInteropHelper(this).Handle;
		if (handle != IntPtr.Zero)
		{
			long num = GetWindowLongPtr(handle, -20);
			SetWindowLongPtr(handle, -20, (nint)(num | 0x8000000 | 0x80));
		}
	}

	private void PositionWindow(string corner)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		Rect workArea = SystemParameters.WorkArea;
		base.Left = (corner.Contains("left") ? (((Rect)(ref workArea)).Left + 20.0) : (((Rect)(ref workArea)).Right - base.ActualWidth - 20.0));
		base.Top = (corner.Contains("top") ? (((Rect)(ref workArea)).Top + 20.0) : (((Rect)(ref workArea)).Bottom - base.ActualHeight - 20.0));
	}

	private void AnimateIn()
	{
		base.Opacity = 0.0;
		DoubleAnimation animation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(280.0));
		BeginAnimation(UIElement.OpacityProperty, animation);
	}

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

	protected override void OnClosed(EventArgs e)
	{
		_timer.Stop();
		_timer.Tick -= _tickHandler;
		base.OnClosed(e);
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri resourceLocator = new Uri("/AxCopilot;component/views/reminderpopupwindow.xaml", UriKind.Relative);
			Application.LoadComponent(this, resourceLocator);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "10.0.5.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		switch (connectionId)
		{
		case 1:
			PopupBorder = (Border)target;
			break;
		case 2:
			UsageText = (TextBlock)target;
			break;
		case 3:
			CloseBtn = (Button)target;
			CloseBtn.Click += CloseBtn_Click;
			break;
		case 4:
			QuoteText = (TextBlock)target;
			break;
		case 5:
			AuthorText = (TextBlock)target;
			break;
		case 6:
			CountdownBar = (ProgressBar)target;
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}
