// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CmdPal.Ext.WindowWalker.Commands;
using Microsoft.CmdPal.Ext.WindowWalker.Pages;
using Windows.System;

namespace Microsoft.CmdPal.Ext.WindowWalker.Components;

internal static class ContextMenuHelper
{
    internal static List<CommandContextItem> GetContextMenuResults(in WindowWalkerListItem listItem)
    {
        ArgumentNullException.ThrowIfNull(listItem);

        if (listItem.Window is null)
        {
            return [];
        }

        var windowData = listItem.Window;
        var contextMenu = new List<CommandContextItem>
        {
            new(new CloseWindowCommand(windowData))
            {
                RequestedShortcut = KeyChordHelpers.FromModifiers(true, false, false, false, (int)VirtualKey.F4),
            },
        };

        // Hide menu if Explorer.exe is the shell process or the process name is ApplicationFrameHost.exe
        // In the first case we would crash the windows ui and in the second case we would kill the generic process for uwp apps.
        if (!windowData.Process.IsShellProcess && !(windowData.Process.IsUwpAppFrameHost && string.Equals(windowData.Process.Name, "ApplicationFrameHost.exe", StringComparison.OrdinalIgnoreCase))
            && !(windowData.Process.IsFullAccessDenied && SettingsManager.Instance.HideKillProcessOnElevatedProcesses))
        {
            contextMenu.Add(new CommandContextItem(new EndTaskCommand(windowData))
            {
                RequestedShortcut = KeyChordHelpers.FromModifiers(true, false, false, false, (int)VirtualKey.Delete),
                IsCritical = true,
            });
        }

        return contextMenu;
    }
}
