using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using AxCopilot.Services;

namespace AxCopilot.Views;

public class DiffViewerPanel : Border
{
	private readonly string _filePath;

	private readonly string _oldContent;

	private readonly string _newContent;

	private readonly List<DiffService.DiffLine> _diffLines;

	public event EventHandler? Accepted;

	public event EventHandler? Rejected;

	public DiffViewerPanel(string filePath, string oldContent, string newContent)
	{
		_filePath = filePath;
		_oldContent = oldContent;
		_newContent = newContent;
		_diffLines = DiffService.ComputeDiff(oldContent, newContent);
		base.CornerRadius = new CornerRadius(10.0);
		base.Background = new SolidColorBrush(Color.FromRgb(250, 250, byte.MaxValue));
		base.BorderBrush = new SolidColorBrush(Color.FromRgb(224, 224, 236));
		base.BorderThickness = new Thickness(1.0);
		base.Padding = new Thickness(0.0);
		base.Margin = new Thickness(0.0, 8.0, 0.0, 8.0);
		Child = BuildContent();
	}

	private UIElement BuildContent()
	{
		StackPanel stackPanel = new StackPanel();
		Border border = new Border
		{
			Background = new SolidColorBrush(Color.FromRgb(240, 242, byte.MaxValue)),
			CornerRadius = new CornerRadius(10.0, 10.0, 0.0, 0.0),
			Padding = new Thickness(14.0, 10.0, 14.0, 10.0)
		};
		Grid grid = new Grid();
		grid.ColumnDefinitions.Add(new ColumnDefinition
		{
			Width = new GridLength(1.0, GridUnitType.Star)
		});
		grid.ColumnDefinitions.Add(new ColumnDefinition
		{
			Width = GridLength.Auto
		});
		string summary = DiffService.GetSummary(_diffLines);
		StackPanel stackPanel2 = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		stackPanel2.Children.Add(new TextBlock
		{
			Text = "\ue89a",
			FontFamily = new FontFamily("Segoe MDL2 Assets"),
			FontSize = 13.0,
			Foreground = new SolidColorBrush(Color.FromRgb(75, 94, 252)),
			VerticalAlignment = VerticalAlignment.Center,
			Margin = new Thickness(0.0, 0.0, 8.0, 0.0)
		});
		stackPanel2.Children.Add(new TextBlock
		{
			Text = Path.GetFileName(_filePath),
			FontSize = 13.0,
			FontWeight = FontWeights.SemiBold,
			Foreground = new SolidColorBrush(Color.FromRgb(26, 27, 46)),
			VerticalAlignment = VerticalAlignment.Center
		});
		stackPanel2.Children.Add(new TextBlock
		{
			Text = "  ·  " + summary,
			FontSize = 11.0,
			Foreground = new SolidColorBrush(Color.FromRgb(136, 136, 170)),
			VerticalAlignment = VerticalAlignment.Center
		});
		Grid.SetColumn(stackPanel2, 0);
		grid.Children.Add(stackPanel2);
		StackPanel stackPanel3 = new StackPanel
		{
			Orientation = Orientation.Horizontal
		};
		Button button = CreateButton("적용", Color.FromRgb(16, 124, 16), Colors.White);
		button.Click += delegate
		{
			this.Accepted?.Invoke(this, EventArgs.Empty);
		};
		stackPanel3.Children.Add(button);
		Button button2 = CreateButton("취소", Colors.Transparent, Color.FromRgb(197, 15, 31));
		button2.Click += delegate
		{
			this.Rejected?.Invoke(this, EventArgs.Empty);
		};
		stackPanel3.Children.Add(button2);
		Grid.SetColumn(stackPanel3, 1);
		grid.Children.Add(stackPanel3);
		border.Child = grid;
		stackPanel.Children.Add(border);
		ScrollViewer scrollViewer = new ScrollViewer
		{
			MaxHeight = 300.0,
			VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
			HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
		};
		StackPanel stackPanel4 = new StackPanel
		{
			Margin = new Thickness(0.0)
		};
		foreach (DiffService.DiffLine diffLine in _diffLines)
		{
			DiffService.DiffType type = diffLine.Type;
			if (1 == 0)
			{
			}
			(Color, Color, string) tuple = type switch
			{
				DiffService.DiffType.Added => (Color.FromRgb(230, byte.MaxValue, 237), Color.FromRgb(22, 108, 52), "+"), 
				DiffService.DiffType.Removed => (Color.FromRgb(byte.MaxValue, 235, 233), Color.FromRgb(207, 34, 46), "-"), 
				_ => (Color.FromRgb(byte.MaxValue, byte.MaxValue, byte.MaxValue), Color.FromRgb(102, 102, 136), " "), 
			};
			if (1 == 0)
			{
			}
			(Color, Color, string) tuple2 = tuple;
			Color item = tuple2.Item1;
			Color item2 = tuple2.Item2;
			string item3 = tuple2.Item3;
			string text = diffLine.OldLineNo?.ToString() ?? "";
			string text2 = diffLine.NewLineNo?.ToString() ?? "";
			Border border2 = new Border
			{
				Background = new SolidColorBrush(item),
				Padding = new Thickness(8.0, 1.0, 8.0, 1.0)
			};
			Grid grid2 = new Grid();
			grid2.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = new GridLength(36.0)
			});
			grid2.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = new GridLength(36.0)
			});
			grid2.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = new GridLength(16.0)
			});
			grid2.ColumnDefinitions.Add(new ColumnDefinition
			{
				Width = new GridLength(1.0, GridUnitType.Star)
			});
			TextBlock element = new TextBlock
			{
				Text = text,
				FontSize = 10.0,
				FontFamily = new FontFamily("Consolas"),
				Foreground = new SolidColorBrush(Color.FromRgb(170, 170, 204)),
				HorizontalAlignment = HorizontalAlignment.Right,
				Margin = new Thickness(0.0, 0.0, 4.0, 0.0)
			};
			Grid.SetColumn(element, 0);
			grid2.Children.Add(element);
			TextBlock element2 = new TextBlock
			{
				Text = text2,
				FontSize = 10.0,
				FontFamily = new FontFamily("Consolas"),
				Foreground = new SolidColorBrush(Color.FromRgb(170, 170, 204)),
				HorizontalAlignment = HorizontalAlignment.Right,
				Margin = new Thickness(0.0, 0.0, 4.0, 0.0)
			};
			Grid.SetColumn(element2, 1);
			grid2.Children.Add(element2);
			TextBlock element3 = new TextBlock
			{
				Text = item3,
				FontSize = 11.0,
				FontFamily = new FontFamily("Consolas"),
				Foreground = new SolidColorBrush(item2),
				FontWeight = FontWeights.Bold
			};
			Grid.SetColumn(element3, 2);
			grid2.Children.Add(element3);
			TextBlock element4 = new TextBlock
			{
				Text = diffLine.Content,
				FontSize = 11.0,
				FontFamily = new FontFamily("Consolas"),
				Foreground = new SolidColorBrush(item2),
				TextWrapping = TextWrapping.NoWrap
			};
			Grid.SetColumn(element4, 3);
			grid2.Children.Add(element4);
			border2.Child = grid2;
			stackPanel4.Children.Add(border2);
		}
		scrollViewer.Content = stackPanel4;
		stackPanel.Children.Add(scrollViewer);
		return stackPanel;
	}

	private static Button CreateButton(string text, Color bg, Color fg)
	{
		Button button = new Button
		{
			Cursor = Cursors.Hand,
			Margin = new Thickness(4.0, 0.0, 0.0, 0.0)
		};
		ControlTemplate controlTemplate = new ControlTemplate(typeof(Button));
		FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(Border));
		frameworkElementFactory.SetValue(Border.BackgroundProperty, new SolidColorBrush(bg));
		frameworkElementFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(6.0));
		frameworkElementFactory.SetValue(Border.PaddingProperty, new Thickness(12.0, 5.0, 12.0, 5.0));
		if (bg == Colors.Transparent)
		{
			frameworkElementFactory.SetValue(Border.BorderBrushProperty, new SolidColorBrush(fg));
			frameworkElementFactory.SetValue(Border.BorderThicknessProperty, new Thickness(1.0));
		}
		FrameworkElementFactory frameworkElementFactory2 = new FrameworkElementFactory(typeof(ContentPresenter));
		frameworkElementFactory2.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center);
		frameworkElementFactory.AppendChild(frameworkElementFactory2);
		controlTemplate.VisualTree = frameworkElementFactory;
		button.Template = controlTemplate;
		button.Content = new TextBlock
		{
			Text = text,
			FontSize = 11.0,
			Foreground = new SolidColorBrush(fg),
			FontWeight = FontWeights.SemiBold
		};
		return button;
	}
}
