using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Threading;

namespace AxCopilot.Views;

internal sealed class PlanViewerWindow : Window
{
	private const int WM_NCHITTEST = 132;

	private const int HTLEFT = 10;

	private const int HTRIGHT = 11;

	private const int HTTOP = 12;

	private const int HTTOPLEFT = 13;

	private const int HTTOPRIGHT = 14;

	private const int HTBOTTOM = 15;

	private const int HTBOTTOMLEFT = 16;

	private const int HTBOTTOMRIGHT = 17;

	private const int ResizeGrip = 12;

	private const string DragDataFormat = "PlanStepIndex";

	private readonly StackPanel _stepsPanel;

	private readonly ScrollViewer _scrollViewer;

	private readonly StackPanel _btnPanel;

	private readonly Border _statusBar;

	private readonly TextBlock _statusText;

	private readonly TextBlock _progressText;

	private readonly HashSet<int> _expandedSteps = new HashSet<int>();

	private int _dragSourceIndex = -1;

	private Point _dragStartPoint;

	private TaskCompletionSource<string?>? _tcs;

	private string _planText = "";

	private List<string> _steps = new List<string>();

	private int _currentStep = -1;

	private bool _isExecuting;

	public string PlanText => _planText;

	public List<string> Steps => _steps;

	[DllImport("user32.dll")]
	private static extern nint SendMessage(nint hWnd, int msg, nint wParam, nint lParam);

	public PlanViewerWindow()
	{
		base.Width = 640.0;
		base.Height = 520.0;
		base.MinWidth = 480.0;
		base.MinHeight = 360.0;
		base.WindowStartupLocation = WindowStartupLocation.CenterScreen;
		base.WindowStyle = WindowStyle.None;
		base.AllowsTransparency = true;
		base.Background = Brushes.Transparent;
		base.ResizeMode = ResizeMode.CanResize;
		base.ShowInTaskbar = false;
		Brush background = (Application.Current.TryFindResource("LauncherBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(26, 27, 46));
		Brush foreground = (Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White;
		Brush brush = (Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray;
		Brush brush2 = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		Brush borderBrush = (Application.Current.TryFindResource("BorderColor") as Brush) ?? Brushes.Gray;
		Border border = new Border
		{
			Background = background,
			CornerRadius = new CornerRadius(14.0),
			BorderBrush = borderBrush,
			BorderThickness = new Thickness(1.0),
			Effect = new DropShadowEffect
			{
				BlurRadius = 20.0,
				ShadowDepth = 4.0,
				Opacity = 0.35,
				Color = Colors.Black
			}
		};
		Grid grid = new Grid
		{
			RowDefinitions = 
			{
				new RowDefinition
				{
					Height = GridLength.Auto
				},
				new RowDefinition
				{
					Height = GridLength.Auto
				},
				new RowDefinition
				{
					Height = GridLength.Auto
				},
				new RowDefinition
				{
					Height = new GridLength(1.0, GridUnitType.Star)
				},
				new RowDefinition
				{
					Height = GridLength.Auto
				}
			}
		};
		Grid grid2 = new Grid
		{
			Background = Brushes.Transparent,
			Margin = new Thickness(20.0, 14.0, 12.0, 0.0)
		};
		grid2.MouseLeftButtonDown += TitleBar_MouseLeftButtonDown;
		StackPanel stackPanel = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		stackPanel.Children.Add(new TextBlock
		{
			Text = "\ue9d2",
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 18.0,
			Foreground = brush2,
			VerticalAlignment = VerticalAlignment.Center,
			Margin = new Thickness(0.0, 0.0, 10.0, 0.0)
		});
		stackPanel.Children.Add(new TextBlock
		{
			Text = "실행 계획",
			FontSize = 16.0,
			FontWeight = FontWeights.Bold,
			Foreground = foreground,
			VerticalAlignment = VerticalAlignment.Center
		});
		grid2.Children.Add(stackPanel);
		Border border2 = new Border
		{
			Width = 32.0,
			Height = 32.0,
			CornerRadius = new CornerRadius(8.0),
			Background = Brushes.Transparent,
			Cursor = Cursors.Hand,
			HorizontalAlignment = HorizontalAlignment.Right,
			VerticalAlignment = VerticalAlignment.Center,
			Child = new TextBlock
			{
				Text = "\ue8bb",
				FontFamily = new FontFamily("Segoe MDL2 Assets"),
				FontSize = 12.0,
				Foreground = brush,
				HorizontalAlignment = HorizontalAlignment.Center,
				VerticalAlignment = VerticalAlignment.Center
			}
		};
		border2.MouseEnter += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Background = new SolidColorBrush(Color.FromArgb(24, byte.MaxValue, byte.MaxValue, byte.MaxValue));
		};
		border2.MouseLeave += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Background = Brushes.Transparent;
		};
		border2.MouseLeftButtonUp += delegate
		{
			Hide();
		};
		grid2.Children.Add(border2);
		Grid.SetRow(grid2, 0);
		grid.Children.Add(grid2);
		_statusBar = new Border
		{
			Visibility = Visibility.Collapsed,
			Margin = new Thickness(20.0, 8.0, 20.0, 0.0),
			Padding = new Thickness(12.0, 6.0, 12.0, 6.0),
			CornerRadius = new CornerRadius(8.0),
			Background = new SolidColorBrush(Color.FromArgb(21, ((SolidColorBrush)brush2).Color.R, ((SolidColorBrush)brush2).Color.G, ((SolidColorBrush)brush2).Color.B))
		};
		Grid grid3 = new Grid();
		_statusText = new TextBlock
		{
			Text = "실행 중...",
			FontSize = 12.0,
			Foreground = brush2,
			VerticalAlignment = VerticalAlignment.Center
		};
		grid3.Children.Add(_statusText);
		_progressText = new TextBlock
		{
			Text = "",
			FontSize = 12.0,
			Foreground = brush,
			HorizontalAlignment = HorizontalAlignment.Right,
			VerticalAlignment = VerticalAlignment.Center
		};
		grid3.Children.Add(_progressText);
		_statusBar.Child = grid3;
		Grid.SetRow(_statusBar, 1);
		grid.Children.Add(_statusBar);
		Brush hoverBg = (Application.Current.TryFindResource("ItemHoverBackground") as Brush) ?? new SolidColorBrush(Color.FromArgb(24, byte.MaxValue, byte.MaxValue, byte.MaxValue));
		StackPanel stackPanel2 = new StackPanel
		{
			Orientation = Orientation.Horizontal,
			HorizontalAlignment = HorizontalAlignment.Right,
			Margin = new Thickness(20.0, 6.0, 20.0, 0.0)
		};
		Border border3 = MakeToolbarButton("\ue70d", "모두 열기", brush, hoverBg);
		border3.MouseLeftButtonUp += delegate
		{
			for (int i = 0; i < _steps.Count; i++)
			{
				_expandedSteps.Add(i);
			}
			RenderSteps();
		};
		stackPanel2.Children.Add(border3);
		Border border4 = MakeToolbarButton("\ue70e", "모두 닫기", brush, hoverBg);
		border4.MouseLeftButtonUp += delegate
		{
			_expandedSteps.Clear();
			RenderSteps();
		};
		stackPanel2.Children.Add(border4);
		Grid.SetRow(stackPanel2, 2);
		grid.Children.Add(stackPanel2);
		_scrollViewer = new ScrollViewer
		{
			VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
			Margin = new Thickness(16.0, 6.0, 16.0, 0.0),
			Padding = new Thickness(4.0)
		};
		_stepsPanel = new StackPanel();
		_scrollViewer.Content = _stepsPanel;
		Grid.SetRow(_scrollViewer, 3);
		grid.Children.Add(_scrollViewer);
		_btnPanel = new StackPanel
		{
			Orientation = Orientation.Horizontal,
			HorizontalAlignment = HorizontalAlignment.Center,
			Margin = new Thickness(20.0, 12.0, 20.0, 16.0)
		};
		Grid.SetRow(_btnPanel, 4);
		grid.Children.Add(_btnPanel);
		border.Child = grid;
		base.Content = border;
		base.SourceInitialized += delegate
		{
			nint handle = new WindowInteropHelper(this).Handle;
			HwndSource.FromHwnd(handle)?.AddHook(WndProc);
		};
	}

	private static Border MakeToolbarButton(string icon, string label, Brush fg, Brush hoverBg)
	{
		Border border = new Border
		{
			CornerRadius = new CornerRadius(6.0),
			Padding = new Thickness(8.0, 3.0, 8.0, 3.0),
			Margin = new Thickness(0.0, 0.0, 4.0, 0.0),
			Background = Brushes.Transparent,
			Cursor = Cursors.Hand
		};
		StackPanel stackPanel = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		stackPanel.Children.Add(new TextBlock
		{
			Text = icon,
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 9.0,
			Foreground = fg,
			VerticalAlignment = VerticalAlignment.Center,
			Margin = new Thickness(0.0, 0.0, 4.0, 0.0)
		});
		stackPanel.Children.Add(new TextBlock
		{
			Text = label,
			FontSize = 11.5,
			Foreground = fg
		});
		border.Child = stackPanel;
		border.MouseEnter += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Background = hoverBg;
		};
		border.MouseLeave += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Background = Brushes.Transparent;
		};
		return border;
	}

	private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		if (e.ClickCount == 2)
		{
			return;
		}
		try
		{
			DragMove();
		}
		catch
		{
		}
	}

	private nint WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled)
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		if (msg == 132)
		{
			Point val = PointFromScreen(new Point((double)(short)(((IntPtr)lParam).ToInt32() & 0xFFFF), (double)(short)(((IntPtr)lParam).ToInt32() >> 16)));
			double actualWidth = base.ActualWidth;
			double actualHeight = base.ActualHeight;
			int num = 0;
			if (((Point)(ref val)).X < 12.0 && ((Point)(ref val)).Y < 12.0)
			{
				num = 13;
			}
			else if (((Point)(ref val)).X > actualWidth - 12.0 && ((Point)(ref val)).Y < 12.0)
			{
				num = 14;
			}
			else if (((Point)(ref val)).X < 12.0 && ((Point)(ref val)).Y > actualHeight - 12.0)
			{
				num = 16;
			}
			else if (((Point)(ref val)).X > actualWidth - 12.0 && ((Point)(ref val)).Y > actualHeight - 12.0)
			{
				num = 17;
			}
			else if (((Point)(ref val)).X < 12.0)
			{
				num = 10;
			}
			else if (((Point)(ref val)).X > actualWidth - 12.0)
			{
				num = 11;
			}
			else if (((Point)(ref val)).Y < 12.0)
			{
				num = 12;
			}
			else if (((Point)(ref val)).Y > actualHeight - 12.0)
			{
				num = 15;
			}
			if (num != 0)
			{
				handled = true;
				return num;
			}
		}
		return IntPtr.Zero;
	}

	public Task<string?> ShowPlanAsync(string planText, List<string> steps, TaskCompletionSource<string?> tcs)
	{
		_planText = planText;
		_steps = steps;
		_tcs = tcs;
		_currentStep = -1;
		_isExecuting = false;
		_expandedSteps.Clear();
		RenderSteps();
		BuildApprovalButtons();
		_statusBar.Visibility = Visibility.Collapsed;
		Show();
		Activate();
		return tcs.Task;
	}

	public void SwitchToExecutionMode()
	{
		_isExecuting = true;
		_statusBar.Visibility = Visibility.Visible;
		_statusText.Text = "▶ 계획 실행 중...";
		_progressText.Text = $"0 / {_steps.Count}";
		BuildExecutionButtons();
	}

	public void UpdateCurrentStep(int stepIndex)
	{
		if (stepIndex >= 0 && stepIndex < _steps.Count)
		{
			_currentStep = stepIndex;
			_progressText.Text = $"{stepIndex + 1} / {_steps.Count}";
			_expandedSteps.Add(stepIndex);
			RenderSteps();
		}
	}

	public void MarkComplete()
	{
		_isExecuting = false;
		_statusText.Text = "✓ 계획 실행 완료";
		_statusText.Foreground = new SolidColorBrush(Color.FromRgb(16, 185, 129));
		_progressText.Text = $"{_steps.Count} / {_steps.Count}";
		_currentStep = _steps.Count;
		RenderSteps();
		BuildCloseButton();
	}

	private void RenderSteps()
	{
		_stepsPanel.Children.Clear();
		Brush brush = (Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White;
		Brush secondaryText = (Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray;
		Brush accentBrush = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		Brush brush2 = (Application.Current.TryFindResource("ItemBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(42, 43, 64));
		Brush hoverBg = (Application.Current.TryFindResource("ItemHoverBackground") as Brush) ?? new SolidColorBrush(Color.FromArgb(24, byte.MaxValue, byte.MaxValue, byte.MaxValue));
		bool flag = !_isExecuting && _currentStep < 0;
		for (int i = 0; i < _steps.Count; i++)
		{
			string text = _steps[i];
			int capturedIdx = i;
			bool flag2 = i < _currentStep;
			bool flag3 = i == _currentStep;
			bool flag4 = i > _currentStep;
			bool flag5 = _expandedSteps.Contains(i);
			Border border = new Border
			{
				CornerRadius = new CornerRadius(10.0),
				Padding = new Thickness(10.0, 7.0, 10.0, 7.0),
				Margin = new Thickness(0.0, 0.0, 0.0, 5.0),
				Background = (flag3 ? new SolidColorBrush(Color.FromArgb(24, ((SolidColorBrush)accentBrush).Color.R, ((SolidColorBrush)accentBrush).Color.G, ((SolidColorBrush)accentBrush).Color.B)) : brush2),
				BorderBrush = (flag3 ? accentBrush : Brushes.Transparent),
				BorderThickness = new Thickness(flag3 ? 1.5 : 0.0),
				AllowDrop = flag
			};
			border.Cursor = Cursors.Hand;
			border.MouseLeftButtonUp += delegate(object s, MouseButtonEventArgs e)
			{
				if (!(e.OriginalSource is Border { Tag: var tag }) || !(tag?.ToString() == "DragHandle"))
				{
					if (_expandedSteps.Contains(capturedIdx))
					{
						_expandedSteps.Remove(capturedIdx);
					}
					else
					{
						_expandedSteps.Add(capturedIdx);
					}
					RenderSteps();
				}
			};
			Grid grid = new Grid();
			int value = (flag ? 1 : 0);
			int value2 = ((!flag) ? 1 : 2);
			int value3 = (flag ? 3 : 2);
			int value4 = (flag ? 4 : (-1));
			if (flag)
			{
				grid.ColumnDefinitions.Add(new ColumnDefinition
				{
					Width = GridLength.Auto
				});
			}
			grid.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = GridLength.Auto
			});
			grid.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = new GridLength(1.0, GridUnitType.Star)
			});
			grid.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = GridLength.Auto
			});
			if (flag)
			{
				grid.ColumnDefinitions.Add(new ColumnDefinition
				{
					Width = GridLength.Auto
				});
			}
			if (flag)
			{
				Color color = Color.FromArgb(85, 128, 128, 128);
				SolidColorBrush dimBrush = new SolidColorBrush(color);
				Border border2 = new Border
				{
					Tag = "DragHandle",
					Width = 20.0,
					Cursor = Cursors.SizeAll,
					Background = Brushes.Transparent,
					Margin = new Thickness(0.0, 0.0, 6.0, 0.0),
					VerticalAlignment = VerticalAlignment.Center,
					Child = new TextBlock
					{
						Text = "\ue8fd",
						FontFamily = new FontFamily("Segoe MDL2 Assets"),
						FontSize = 11.0,
						Foreground = dimBrush,
						HorizontalAlignment = HorizontalAlignment.Center,
						VerticalAlignment = VerticalAlignment.Center
					}
				};
				border2.MouseEnter += delegate(object s, MouseEventArgs _)
				{
					((TextBlock)((Border)s).Child).Foreground = secondaryText;
				};
				border2.MouseLeave += delegate(object s, MouseEventArgs _)
				{
					((TextBlock)((Border)s).Child).Foreground = dimBrush;
				};
				border2.PreviewMouseLeftButtonDown += delegate(object s, MouseButtonEventArgs e)
				{
					//IL_0033: Unknown result type (might be due to invalid IL or missing references)
					//IL_0038: Unknown result type (might be due to invalid IL or missing references)
					_dragSourceIndex = capturedIdx;
					_dragStartPoint = e.GetPosition(_stepsPanel);
					e.Handled = true;
				};
				border2.PreviewMouseMove += delegate(object s, MouseEventArgs e)
				{
					//IL_0033: Unknown result type (might be due to invalid IL or missing references)
					//IL_0038: Unknown result type (might be due to invalid IL or missing references)
					//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
					//IL_00bd: Expected O, but got Unknown
					if (_dragSourceIndex >= 0 && e.LeftButton == MouseButtonState.Pressed)
					{
						Point position = e.GetPosition(_stepsPanel);
						if (Math.Abs(((Point)(ref position)).X - ((Point)(ref _dragStartPoint)).X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(((Point)(ref position)).Y - ((Point)(ref _dragStartPoint)).Y) > SystemParameters.MinimumVerticalDragDistance)
						{
							int dragSourceIndex = _dragSourceIndex;
							_dragSourceIndex = -1;
							DragDrop.DoDragDrop((DependencyObject)s, new DataObject("PlanStepIndex", dragSourceIndex), DragDropEffects.Move);
							((DispatcherObject)this).Dispatcher.InvokeAsync((Action)RenderSteps);
						}
					}
				};
				border2.PreviewMouseLeftButtonUp += delegate
				{
					_dragSourceIndex = -1;
				};
				Grid.SetColumn(border2, 0);
				grid.Children.Add(border2);
				border.DragOver += delegate(object s, DragEventArgs e)
				{
					if (e.Data.GetDataPresent("PlanStepIndex"))
					{
						int num = (int)e.Data.GetData("PlanStepIndex");
						if (num != capturedIdx)
						{
							((Border)s).BorderBrush = accentBrush;
							((Border)s).BorderThickness = new Thickness(1.5);
							e.Effects = DragDropEffects.Move;
						}
						else
						{
							e.Effects = DragDropEffects.None;
						}
						e.Handled = true;
					}
				};
				border.DragLeave += delegate(object s, DragEventArgs _)
				{
					bool flag6 = _currentStep == capturedIdx;
					((Border)s).BorderBrush = (flag6 ? accentBrush : Brushes.Transparent);
					((Border)s).BorderThickness = new Thickness(flag6 ? 1.5 : 0.0);
				};
				border.Drop += delegate(object s, DragEventArgs e)
				{
					if (!e.Data.GetDataPresent("PlanStepIndex"))
					{
						e.Handled = true;
					}
					else
					{
						int num = (int)e.Data.GetData("PlanStepIndex");
						int num2 = capturedIdx;
						if (num != num2 && num >= 0 && num < _steps.Count)
						{
							string item = _steps[num];
							_steps.RemoveAt(num);
							int index = ((num < num2) ? (num2 - 1) : num2);
							_steps.Insert(index, item);
							_expandedSteps.Clear();
							RenderSteps();
						}
						e.Handled = true;
					}
				};
			}
			UIElement element = (flag2 ? new TextBlock
			{
				Text = "\ue73e",
				FontFamily = new FontFamily("Segoe MDL2 Assets"),
				FontSize = 13.0,
				Foreground = new SolidColorBrush(Color.FromRgb(16, 185, 129)),
				VerticalAlignment = VerticalAlignment.Center,
				Margin = new Thickness(0.0, 0.0, 10.0, 0.0),
				Width = 20.0,
				TextAlignment = TextAlignment.Center
			} : ((!flag3) ? ((UIElement)new Border
			{
				Width = 22.0,
				Height = 22.0,
				CornerRadius = new CornerRadius(11.0),
				Background = new SolidColorBrush(Color.FromArgb(37, 128, 128, 128)),
				Margin = new Thickness(0.0, 0.0, 10.0, 0.0),
				VerticalAlignment = VerticalAlignment.Center,
				Child = new TextBlock
				{
					Text = $"{i + 1}",
					FontSize = 11.0,
					Foreground = secondaryText,
					FontWeight = FontWeights.SemiBold,
					HorizontalAlignment = HorizontalAlignment.Center,
					VerticalAlignment = VerticalAlignment.Center
				}
			}) : ((UIElement)new TextBlock
			{
				Text = "\ue768",
				FontFamily = new FontFamily("Segoe MDL2 Assets"),
				FontSize = 13.0,
				Foreground = accentBrush,
				VerticalAlignment = VerticalAlignment.Center,
				Margin = new Thickness(0.0, 0.0, 10.0, 0.0),
				Width = 20.0,
				TextAlignment = TextAlignment.Center
			})));
			Grid.SetColumn(element, value);
			grid.Children.Add(element);
			TextBlock textBlock = new TextBlock
			{
				Text = text,
				FontSize = 13.0,
				Foreground = (flag2 ? secondaryText : brush),
				VerticalAlignment = VerticalAlignment.Center,
				Opacity = ((flag4 && _isExecuting) ? 0.6 : 1.0),
				TextDecorations = (flag2 ? TextDecorations.Strikethrough : null),
				Margin = new Thickness(0.0, 0.0, 4.0, 0.0)
			};
			if (flag5)
			{
				textBlock.TextWrapping = TextWrapping.Wrap;
				textBlock.TextTrimming = TextTrimming.None;
			}
			else
			{
				textBlock.TextWrapping = TextWrapping.NoWrap;
				textBlock.TextTrimming = TextTrimming.CharacterEllipsis;
				textBlock.ToolTip = text;
			}
			Grid.SetColumn(textBlock, value2);
			grid.Children.Add(textBlock);
			Border border3 = new Border
			{
				Width = 22.0,
				Height = 22.0,
				CornerRadius = new CornerRadius(4.0),
				Background = Brushes.Transparent,
				Cursor = Cursors.Hand,
				VerticalAlignment = VerticalAlignment.Center,
				Margin = new Thickness(0.0, 0.0, flag ? 4 : 0, 0.0),
				Child = new TextBlock
				{
					Text = (flag5 ? "\ue70e" : "\ue70d"),
					FontFamily = new FontFamily("Segoe MDL2 Assets"),
					FontSize = 9.0,
					Foreground = new SolidColorBrush(Color.FromArgb(112, 128, 128, 128)),
					HorizontalAlignment = HorizontalAlignment.Center,
					VerticalAlignment = VerticalAlignment.Center
				}
			};
			border3.MouseEnter += delegate(object s, MouseEventArgs _)
			{
				((Border)s).Background = hoverBg;
			};
			border3.MouseLeave += delegate(object s, MouseEventArgs _)
			{
				((Border)s).Background = Brushes.Transparent;
			};
			border3.MouseLeftButtonUp += delegate(object _, MouseButtonEventArgs e)
			{
				if (_expandedSteps.Contains(capturedIdx))
				{
					_expandedSteps.Remove(capturedIdx);
				}
				else
				{
					_expandedSteps.Add(capturedIdx);
				}
				RenderSteps();
				e.Handled = true;
			};
			Grid.SetColumn(border3, value3);
			grid.Children.Add(border3);
			if (flag)
			{
				StackPanel stackPanel = new StackPanel
				{
					Orientation = Orientation.Horizontal,
					VerticalAlignment = VerticalAlignment.Center,
					Margin = new Thickness(2.0, 0.0, 0.0, 0.0)
				};
				if (i > 0)
				{
					Border border4 = CreateMiniButton("\ue70e", secondaryText, hoverBg);
					border4.ToolTip = "위로 이동";
					border4.MouseLeftButtonUp += delegate(object _, MouseButtonEventArgs e)
					{
						SwapSteps(capturedIdx, capturedIdx - 1);
						e.Handled = true;
					};
					stackPanel.Children.Add(border4);
				}
				if (i < _steps.Count - 1)
				{
					Border border5 = CreateMiniButton("\ue70d", secondaryText, hoverBg);
					border5.ToolTip = "아래로 이동";
					border5.MouseLeftButtonUp += delegate(object _, MouseButtonEventArgs e)
					{
						SwapSteps(capturedIdx, capturedIdx + 1);
						e.Handled = true;
					};
					stackPanel.Children.Add(border5);
				}
				Border border6 = CreateMiniButton("\ue70f", accentBrush, hoverBg);
				border6.ToolTip = "편집";
				border6.MouseLeftButtonUp += delegate(object _, MouseButtonEventArgs e)
				{
					EditStep(capturedIdx);
					e.Handled = true;
				};
				stackPanel.Children.Add(border6);
				Border border7 = CreateMiniButton("\ue74d", new SolidColorBrush(Color.FromRgb(220, 38, 38)), hoverBg);
				border7.ToolTip = "삭제";
				border7.MouseLeftButtonUp += delegate(object _, MouseButtonEventArgs e)
				{
					if (_steps.Count > 1)
					{
						_steps.RemoveAt(capturedIdx);
						_expandedSteps.Remove(capturedIdx);
						RenderSteps();
					}
					e.Handled = true;
				};
				stackPanel.Children.Add(border7);
				Grid.SetColumn(stackPanel, value4);
				grid.Children.Add(stackPanel);
			}
			border.Child = grid;
			_stepsPanel.Children.Add(border);
		}
		if (flag)
		{
			Brush foreground = (Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray;
			Brush hb2 = (Application.Current.TryFindResource("ItemHoverBackground") as Brush) ?? new SolidColorBrush(Color.FromArgb(24, byte.MaxValue, byte.MaxValue, byte.MaxValue));
			Border border8 = new Border
			{
				CornerRadius = new CornerRadius(10.0),
				Padding = new Thickness(14.0, 8.0, 14.0, 8.0),
				Margin = new Thickness(0.0, 4.0, 0.0, 0.0),
				Background = Brushes.Transparent,
				BorderBrush = new SolidColorBrush(Color.FromArgb(48, 128, 128, 128)),
				BorderThickness = new Thickness(1.0),
				Cursor = Cursors.Hand
			};
			StackPanel stackPanel2 = new StackPanel
			{
				Orientation = Orientation.Horizontal,
				HorizontalAlignment = HorizontalAlignment.Center
			};
			stackPanel2.Children.Add(new TextBlock
			{
				Text = "\ue710",
				FontFamily = new FontFamily("Segoe MDL2 Assets"),
				FontSize = 12.0,
				Foreground = foreground,
				VerticalAlignment = VerticalAlignment.Center,
				Margin = new Thickness(0.0, 0.0, 6.0, 0.0)
			});
			stackPanel2.Children.Add(new TextBlock
			{
				Text = "단계 추가",
				FontSize = 12.0,
				Foreground = foreground
			});
			border8.Child = stackPanel2;
			border8.MouseEnter += delegate(object s, MouseEventArgs _)
			{
				((Border)s).Background = hb2;
			};
			border8.MouseLeave += delegate(object s, MouseEventArgs _)
			{
				((Border)s).Background = Brushes.Transparent;
			};
			border8.MouseLeftButtonUp += delegate
			{
				_steps.Add("새 단계");
				RenderSteps();
				EditStep(_steps.Count - 1);
			};
			_stepsPanel.Children.Add(border8);
		}
		if (_currentStep >= 0 && _stepsPanel.Children.Count > _currentStep)
		{
			_stepsPanel.UpdateLayout();
			FrameworkElement frameworkElement = (FrameworkElement)_stepsPanel.Children[Math.Min(_currentStep, _stepsPanel.Children.Count - 1)];
			frameworkElement.BringIntoView();
		}
	}

	private void SwapSteps(int a, int b)
	{
		if (a >= 0 && b >= 0 && a < _steps.Count && b < _steps.Count)
		{
			List<string> steps = _steps;
			List<string> steps2 = _steps;
			string value = _steps[b];
			string value2 = _steps[a];
			steps[a] = value;
			steps2[b] = value2;
			RenderSteps();
		}
	}

	private void EditStep(int index)
	{
		if (index < 0 || index >= _steps.Count)
		{
			return;
		}
		Brush brush = (Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White;
		Brush background = (Application.Current.TryFindResource("ItemBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(42, 43, 64));
		Brush borderBrush = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		if (index >= _stepsPanel.Children.Count)
		{
			return;
		}
		Border border = new Border
		{
			CornerRadius = new CornerRadius(10.0),
			Padding = new Thickness(10.0, 8.0, 10.0, 8.0),
			Margin = new Thickness(0.0, 0.0, 0.0, 5.0),
			Background = background,
			BorderBrush = borderBrush,
			BorderThickness = new Thickness(1.5)
		};
		TextBox textBox = new TextBox
		{
			Text = _steps[index],
			FontSize = 13.0,
			Background = Brushes.Transparent,
			Foreground = brush,
			CaretBrush = brush,
			BorderThickness = new Thickness(0.0),
			AcceptsReturn = false,
			TextWrapping = TextWrapping.Wrap,
			Padding = new Thickness(4.0)
		};
		int capturedIdx = index;
		textBox.KeyDown += delegate(object _, KeyEventArgs e)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Invalid comparison between Unknown and I4
			if ((int)e.Key == 6)
			{
				_steps[capturedIdx] = textBox.Text.Trim();
				RenderSteps();
				e.Handled = true;
			}
			if ((int)e.Key == 13)
			{
				RenderSteps();
				e.Handled = true;
			}
		};
		textBox.LostFocus += delegate
		{
			_steps[capturedIdx] = textBox.Text.Trim();
			RenderSteps();
		};
		border.Child = textBox;
		_stepsPanel.Children[index] = border;
		textBox.Focus();
		textBox.SelectAll();
	}

	private void BuildApprovalButtons()
	{
		_btnPanel.Children.Clear();
		Brush brush = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		Border border = CreateActionButton("\ue73e", "승인", brush, Brushes.White, filled: true);
		border.MouseLeftButtonUp += delegate
		{
			_tcs?.TrySetResult(null);
			SwitchToExecutionMode();
		};
		_btnPanel.Children.Add(border);
		Border border2 = CreateActionButton("\ue70f", "수정 요청", brush, brush, filled: false);
		border2.MouseLeftButtonUp += delegate
		{
			ShowEditInput();
		};
		_btnPanel.Children.Add(border2);
		Border border3 = CreateActionButton("\ue72c", "재확인", (Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray, (Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White, filled: false);
		border3.MouseLeftButtonUp += delegate
		{
			_tcs?.TrySetResult("계획을 다시 검토하고 더 구체적으로 수정해주세요.");
		};
		_btnPanel.Children.Add(border3);
		SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromRgb(220, 38, 38));
		Border border4 = CreateActionButton("\ue711", "취소", solidColorBrush, solidColorBrush, filled: false);
		border4.MouseLeftButtonUp += delegate
		{
			_tcs?.TrySetResult("취소");
			Hide();
		};
		_btnPanel.Children.Add(border4);
	}

	private void BuildExecutionButtons()
	{
		_btnPanel.Children.Clear();
		Brush borderColor = (Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray;
		Border border = CreateActionButton("\ue921", "숨기기", borderColor, (Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White, filled: false);
		border.MouseLeftButtonUp += delegate
		{
			Hide();
		};
		_btnPanel.Children.Add(border);
	}

	private void BuildCloseButton()
	{
		_btnPanel.Children.Clear();
		Brush borderColor = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		Border border = CreateActionButton("\ue73e", "닫기", borderColor, Brushes.White, filled: true);
		border.MouseLeftButtonUp += delegate
		{
			Hide();
		};
		_btnPanel.Children.Add(border);
	}

	private void ShowEditInput()
	{
		Border border = new Border();
		border.Margin = new Thickness(20.0, 0.0, 20.0, 12.0);
		border.Padding = new Thickness(12.0, 8.0, 12.0, 8.0);
		border.CornerRadius = new CornerRadius(10.0);
		border.Background = (Application.Current.TryFindResource("ItemBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(42, 43, 64));
		Border border2 = border;
		StackPanel stackPanel = new StackPanel();
		stackPanel.Children.Add(new TextBlock
		{
			Text = "수정 사항을 입력하세요:",
			FontSize = 11.5,
			Foreground = ((Application.Current.TryFindResource("SecondaryText") as Brush) ?? Brushes.Gray),
			Margin = new Thickness(0.0, 0.0, 0.0, 6.0)
		});
		TextBox textBox = new TextBox
		{
			MinHeight = 44.0,
			MaxHeight = 120.0,
			AcceptsReturn = true,
			TextWrapping = TextWrapping.Wrap,
			FontSize = 13.0,
			Background = ((Application.Current.TryFindResource("LauncherBackground") as Brush) ?? new SolidColorBrush(Color.FromRgb(26, 27, 46))),
			Foreground = ((Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White),
			CaretBrush = ((Application.Current.TryFindResource("PrimaryText") as Brush) ?? Brushes.White),
			BorderBrush = ((Application.Current.TryFindResource("BorderColor") as Brush) ?? Brushes.Gray),
			BorderThickness = new Thickness(1.0),
			Padding = new Thickness(10.0, 8.0, 10.0, 8.0)
		};
		stackPanel.Children.Add(textBox);
		Brush background = (Application.Current.TryFindResource("AccentColor") as Brush) ?? new SolidColorBrush(Color.FromRgb(75, 94, 252));
		Border border3 = new Border
		{
			Background = background,
			CornerRadius = new CornerRadius(8.0),
			Padding = new Thickness(14.0, 6.0, 14.0, 6.0),
			Margin = new Thickness(0.0, 8.0, 0.0, 0.0),
			Cursor = Cursors.Hand,
			HorizontalAlignment = HorizontalAlignment.Right,
			Child = new TextBlock
			{
				Text = "전송",
				FontSize = 12.5,
				FontWeight = FontWeights.SemiBold,
				Foreground = Brushes.White
			}
		};
		border3.MouseEnter += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Opacity = 0.85;
		};
		border3.MouseLeave += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Opacity = 1.0;
		};
		border3.MouseLeftButtonUp += delegate
		{
			string text = textBox.Text.Trim();
			if (!string.IsNullOrEmpty(text))
			{
				_tcs?.TrySetResult(text);
			}
		};
		stackPanel.Children.Add(border3);
		border2.Child = stackPanel;
		if (!(_btnPanel.Parent is Grid grid))
		{
			return;
		}
		for (int num = grid.Children.Count - 1; num >= 0; num--)
		{
			if (grid.Children[num] is Border { Tag: var tag } && tag?.ToString() == "EditPanel")
			{
				grid.Children.RemoveAt(num);
			}
		}
		border2.Tag = "EditPanel";
		Grid.SetRow(border2, 4);
		grid.Children.Add(border2);
		_btnPanel.Margin = new Thickness(20.0, 0.0, 20.0, 16.0);
		textBox.Focus();
	}

	private static Border CreateMiniButton(string icon, Brush fg, Brush hoverBg)
	{
		Border border = new Border
		{
			Width = 24.0,
			Height = 24.0,
			CornerRadius = new CornerRadius(6.0),
			Background = Brushes.Transparent,
			Cursor = Cursors.Hand,
			Margin = new Thickness(1.0, 0.0, 1.0, 0.0),
			Child = new TextBlock
			{
				Text = icon,
				FontFamily = new FontFamily("Segoe MDL2 Assets"),
				FontSize = 10.0,
				Foreground = fg,
				HorizontalAlignment = HorizontalAlignment.Center,
				VerticalAlignment = VerticalAlignment.Center
			}
		};
		border.MouseEnter += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Background = hoverBg;
		};
		border.MouseLeave += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Background = Brushes.Transparent;
		};
		return border;
	}

	private static Border CreateActionButton(string icon, string text, Brush borderColor, Brush textColor, bool filled)
	{
		Color color = ((SolidColorBrush)borderColor).Color;
		Border border = new Border
		{
			CornerRadius = new CornerRadius(12.0),
			Padding = new Thickness(16.0, 8.0, 16.0, 8.0),
			Margin = new Thickness(4.0, 0.0, 4.0, 0.0),
			Cursor = Cursors.Hand,
			Background = (filled ? borderColor : new SolidColorBrush(Color.FromArgb(24, color.R, color.G, color.B))),
			BorderBrush = (filled ? Brushes.Transparent : new SolidColorBrush(Color.FromArgb(128, color.R, color.G, color.B))),
			BorderThickness = new Thickness(filled ? 0.0 : 1.2)
		};
		StackPanel stackPanel = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		stackPanel.Children.Add(new TextBlock
		{
			Text = icon,
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 12.0,
			Foreground = (filled ? Brushes.White : textColor),
			VerticalAlignment = VerticalAlignment.Center,
			Margin = new Thickness(0.0, 0.0, 6.0, 0.0)
		});
		stackPanel.Children.Add(new TextBlock
		{
			Text = text,
			FontSize = 12.5,
			FontWeight = FontWeights.SemiBold,
			Foreground = (filled ? Brushes.White : textColor)
		});
		border.Child = stackPanel;
		border.MouseEnter += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Opacity = 0.85;
		};
		border.MouseLeave += delegate(object s, MouseEventArgs _)
		{
			((Border)s).Opacity = 1.0;
		};
		return border;
	}
}
