using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using ClaudeDo.Ui.ViewModels.Islands;
using ClaudeDo.Ui.Views.Modals;

namespace ClaudeDo.Ui.Views.Islands;

public partial class DetailsIslandView : UserControl
{
    public DetailsIslandView()
    {
        InitializeComponent();
        DataContextChanged += OnDataContextChanged;
    }

    private void OnDataContextChanged(object? sender, EventArgs e)
    {
        if (DataContext is DetailsIslandViewModel vm)
        {
            vm.ShowDiffModal = async (diffVm) =>
            {
                var owner = TopLevel.GetTopLevel(this) as Window;
                if (owner == null) return;
                var modal = new DiffModalView { DataContext = diffVm };
                await modal.ShowDialog(owner);
            };

            vm.ShowWorktreeModal = async (worktreeVm) =>
            {
                var owner = TopLevel.GetTopLevel(this) as Window;
                if (owner == null) return;
                var modal = new WorktreeModalView { DataContext = worktreeVm };
                await modal.ShowDialog(owner);
            };

            vm.ShowMergeModal = async (mergeVm) =>
            {
                var owner = TopLevel.GetTopLevel(this) as Window;
                if (owner == null) return;
                var modal = new MergeModalView { DataContext = mergeVm };
                await modal.ShowDialog(owner);
            };

            vm.ConfirmAsync = ShowConfirmAsync;
        }
    }

    private async System.Threading.Tasks.Task<bool> ShowConfirmAsync(string message)
    {
        var owner = TopLevel.GetTopLevel(this) as Window;
        if (owner == null) return false;

        var tcs = new TaskCompletionSource<bool>();

        var cancel = new Button { Content = "Cancel", MinWidth = 90 };
        var confirm = new Button { Content = "Delete", MinWidth = 90, Classes = { "danger" } };

        var dialog = new Window
        {
            Title = "Confirm",
            Width = 360,
            SizeToContent = SizeToContent.Height,
            CanResize = false,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            ShowInTaskbar = false,
            Background = this.FindResource("SurfaceBrush") as IBrush,
            Content = new StackPanel
            {
                Spacing = 16,
                Margin = new Thickness(20),
                Children =
                {
                    new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap },
                    new StackPanel
                    {
                        Orientation = Orientation.Horizontal,
                        Spacing = 8,
                        HorizontalAlignment = HorizontalAlignment.Right,
                        Children = { cancel, confirm }
                    }
                }
            }
        };

        cancel.Click += (_, _) => { tcs.TrySetResult(false); dialog.Close(); };
        confirm.Click += (_, _) => { tcs.TrySetResult(true); dialog.Close(); };
        dialog.Closed += (_, _) => tcs.TrySetResult(false);

        _ = dialog.ShowDialog(owner);
        return await tcs.Task;
    }

    private void NotesLostFocus(object? sender, RoutedEventArgs e)
    {
        if (DataContext is DetailsIslandViewModel vm)
            vm.SaveNotesCommand.Execute(null);
    }
}
