import { html, TemplateResult } from 'lit'
import { DemoPage } from '../../../src/demo/DemoPage.js'
import { ContextualMenu } from '../../../src/elements/contextual-menu/internals/ContextualMenu.js'
import { ContextMenuCommand } from '../../../src/elements/contextual-menu/internals/types.js'
import { formatSize, copy, paste, title } from './DemoIcons.js'

class ComponentPage extends DemoPage {
  override accessor componentName = 'Event based menu'

  override firstRender(): void {
    super.firstRender()
    this.initContextMenu()
  }

  initContextMenu(): void {
    const container = document.body.querySelector('#workspace') as HTMLElement
    const instance = new ContextualMenu(container)
    const commands: ContextMenuCommand[] = [
      {
        target: 'root',
        label: 'Font size',
        icon: formatSize,
        children: [
          {
            label: '0.75 rem',
            id: '0.75rem',
          },
          {
            label: '1 rem',
            id: '1rem',
          },
          {
            label: '1.25 rem',
            id: '1.25rem',
          },
          {
            label: '1.5 rem',
            id: '1.5rem',
          },
          {
            label: '2 rem',
            id: '2rem',
          },
        ],
      },
      {
        target: 'root',
        label: 'Font family',
        icon: title,
        children: [
          {
            label: 'Arial',
            id: 'Arial',
          },
          {
            label: 'Calibri',
            id: 'Calibri',
          },
          {
            label: 'Consolas',
            id: 'Consolas',
          },
          {
            label: 'Courier New',
            id: 'Courier New',
          },
          {
            label: 'Open Sans',
            id: 'Open Sans',
          },
          {
            label: 'Roboto',
            id: 'Roboto',
          },
          {
            label: 'Times New Roman',
            id: 'Times New Roman',
          },
        ],
      },
      {
        target: 'root',
        label: 'Paste',
        title: 'Paste the value',
        icon: paste,
      },
      {
        target: 'root',
        label: 'Copy',
        title: 'Copies the object',
        icon: copy,
      },
      {
        target: 'root',
        label: 'Add',
        children: [
          {
            label: 'Event',
            children: [
              {
                label: 'Publish event',
                id: 'event-publish',
              },
              {
                label: 'Subscribe event',
                id: 'event-subscribe',
              },
            ],
          },
        ],
      },
    ]
    instance.registerCommands(commands)
    instance.connect()
    instance.addEventListener('execute', (e: Event): void => {
      console.log((e as CustomEvent).detail)
    })
  }

  contentTemplate(): TemplateResult {
    return html`
      <a href="./">Back</a>
      <section class="demo-section">
        <h2 class="title-large">No "execute" function menu</h2>
        <div id="workspace">
          <div class="target" data-id="test-value"></div>
        </div>
      </section>
    `
  }
}

const instance = new ComponentPage()
instance.render()
