import { browser, expect } from '@wdio/globals';

/** Reset UI to home state (close any open panels/overlays). */
async function resetToHomeState(): Promise<void> {
  const settingsPanel = await browser.$('.settings-panel');
  if (await settingsPanel.isExisting()) {
    const closeBtn = await browser.$('.settings-close');
    if (await closeBtn.isExisting()) await closeBtn.click();
  }
  const overlay = await browser.$('.search-overlay');
  if (await overlay.isExisting()) await browser.keys('Escape');
}

/** Close the settings panel if open. */
async function closeSettings(): Promise<void> {
  const panel = await browser.$('.sidebar-panel');
  if (await panel.isDisplayed().catch(() => false)) {
    await browser.execute(() => {
      const btn = document.querySelector('.panel-close');
      if (btn) (btn as HTMLElement).click();
    });
    await browser.pause(500);
  }
}

/** Open command palette — idempotent (won't toggle-close if already open). */
async function openCommandPalette(): Promise<void> {
  // Ensure sidebar is closed first (it can intercept keyboard events)
  await closeSettings();

  // Check if already open
  const alreadyOpen = await browser.execute(() => {
    const p = document.querySelector('.palette');
    return p !== null && getComputedStyle(p).display !== 'none';
  });
  if (alreadyOpen) return;

  // Dispatch Ctrl+K via JS for reliability with WebKit2GTK/tauri-driver
  await browser.execute(() => {
    document.dispatchEvent(new KeyboardEvent('keydown', {
      key: 'k', code: 'KeyK', ctrlKey: true, bubbles: true, cancelable: true,
    }));
  });
  await browser.pause(300);

  const palette = await browser.$('.palette');
  await palette.waitForDisplayed({ timeout: 5000 });
}

/** Close command palette if open — uses backdrop click (more reliable than Escape). */
async function closeCommandPalette(): Promise<void> {
  const isOpen = await browser.execute(() => {
    const p = document.querySelector('.palette');
    return p !== null && getComputedStyle(p).display !== 'none';
  });
  if (!isOpen) return;

  // Click backdrop to close (more reliable than dispatching Escape)
  await browser.execute(() => {
    const backdrop = document.querySelector('.palette-backdrop');
    if (backdrop) (backdrop as HTMLElement).click();
  });
  await browser.pause(500);
}

describe('BTerminal — Command Palette', () => {
  beforeEach(async () => {
    await closeCommandPalette();
  });

  it('should show palette input', async () => {
    await openCommandPalette();

    const input = await browser.$('.palette-input');
    await expect(input).toBeDisplayed();

    // Verify input accepts text (functional focus test, not activeElement check
    // which is unreliable in WebKit2GTK/tauri-driver)
    const canType = await browser.execute(() => {
      const el = document.querySelector('.palette-input') as HTMLInputElement | null;
      if (!el) return false;
      el.focus();
      return el === document.activeElement;
    });
    expect(canType).toBe(true);

    await closeCommandPalette();
  });

  it('should show palette items with command labels and categories', async () => {
    await openCommandPalette();

    const items = await browser.$$('.palette-item');
    expect(items.length).toBeGreaterThanOrEqual(1);

    // Each command item should have a label
    const cmdLabel = await browser.$('.palette-item .cmd-label');
    await expect(cmdLabel).toBeDisplayed();
    const labelText = await cmdLabel.getText();
    expect(labelText.length).toBeGreaterThan(0);

    // Commands should be grouped under category headers
    const categories = await browser.$$('.palette-category');
    expect(categories.length).toBeGreaterThanOrEqual(1);

    await closeCommandPalette();
  });

  it('should highlight selected item in palette', async () => {
    await openCommandPalette();

    // First item should be selected by default
    const selectedItem = await browser.$('.palette-item.selected');
    await expect(selectedItem).toBeExisting();

    await closeCommandPalette();
  });

  it('should filter palette items by typing', async () => {
    await openCommandPalette();

    const itemsBefore = await browser.$$('.palette-item');
    const countBefore = itemsBefore.length;

    // Type a nonsense string that won't match any group name
    const input = await browser.$('.palette-input');
    await input.setValue('zzz_nonexistent_group_xyz');
    await browser.pause(300);

    // Should show no results or fewer items
    const noResults = await browser.$('.no-results');
    const itemsAfter = await browser.$$('.palette-item');
    // Either no-results message appears OR item count decreased
    const filtered = (await noResults.isExisting()) || itemsAfter.length < countBefore;
    expect(filtered).toBe(true);

    await closeCommandPalette();
  });

  it('should close palette by clicking backdrop', async () => {
    await openCommandPalette();
    const palette = await browser.$('.palette');

    // Click the backdrop (outside the palette)
    await browser.execute(() => {
      const backdrop = document.querySelector('.palette-backdrop');
      if (backdrop) (backdrop as HTMLElement).click();
    });
    await browser.pause(500);

    await expect(palette).not.toBeDisplayed();
  });
});

describe('BTerminal — Keyboard Shortcuts', () => {
  before(async () => {
    await resetToHomeState();
    await closeSettings();
    await closeCommandPalette();
  });

  it('should open command palette with Ctrl+K', async () => {
    await openCommandPalette();

    const input = await browser.$('.palette-input');
    await expect(input).toBeDisplayed();

    // Close with Escape
    await closeCommandPalette();
    const palette = await browser.$('.palette');
    const isGone = !(await palette.isDisplayed().catch(() => false));
    expect(isGone).toBe(true);
  });

  it('should toggle settings with Ctrl+,', async () => {
    await browser.keys(['Control', ',']);

    const panel = await browser.$('.sidebar-panel');
    await panel.waitForDisplayed({ timeout: 3000 });

    // Close with Ctrl+,
    await browser.keys(['Control', ',']);
    await panel.waitForDisplayed({ timeout: 3000, reverse: true });
  });

  it('should toggle sidebar with Ctrl+B', async () => {
    // Open sidebar first
    await browser.keys(['Control', ',']);
    const panel = await browser.$('.sidebar-panel');
    await panel.waitForDisplayed({ timeout: 3000 });

    // Toggle off with Ctrl+B
    await browser.keys(['Control', 'b']);
    await panel.waitForDisplayed({ timeout: 3000, reverse: true });
  });

  it('should close sidebar with Escape', async () => {
    // Open sidebar
    await browser.keys(['Control', ',']);
    const panel = await browser.$('.sidebar-panel');
    await panel.waitForDisplayed({ timeout: 3000 });

    // Close with Escape
    await browser.keys('Escape');
    await panel.waitForDisplayed({ timeout: 3000, reverse: true });
  });

  it('should show command palette with categorized commands', async () => {
    await openCommandPalette();

    const items = await browser.$$('.palette-item');
    expect(items.length).toBeGreaterThanOrEqual(1);

    // Commands should have labels
    const cmdLabel = await browser.$('.palette-item .cmd-label');
    await expect(cmdLabel).toBeDisplayed();

    await closeCommandPalette();
  });
});
