import { demoFunc } from './dees-dataview-codebox.demo.js';
import {
  DeesElement,
  html,
  customElement,
  type TemplateResult,
  property,
  state,
  cssManager,
} from '@design.estate/dees-element';
import { cssGeistFontFamily, cssMonoFontFamily } from '../../00fonts.js';

import type { HLJSApi } from 'highlight.js';

import * as smartstring from '@push.rocks/smartstring';

import * as domtools from '@design.estate/dees-domtools';
import { DeesContextmenu } from '../../dees-contextmenu/dees-contextmenu.js';
import { DeesServiceLibLoader } from '../../../services/index.js';

declare global {
  interface HTMLElementTagNameMap {
    'dees-dataview-codebox': DeesDataviewCodebox;
  }
}

@customElement('dees-dataview-codebox')
export class DeesDataviewCodebox extends DeesElement {
  public static demo = demoFunc;
  public static demoGroup = 'Data View';

  @property()
  accessor progLang: string = 'typescript';

  @property({
    type: String,
    reflect: true,
  })
  accessor codeToDisplay: string = '';

  constructor() {
    super();
  }

  render(): TemplateResult {
    return html`
      ${domtools.elementBasic.styles}
      <style>
        :host {
          position: relative;
          display: block;
          text-align: left;
          font-size: 16px;
          font-family: ${cssGeistFontFamily};
        }
        .mainbox {
          position: relative;
          color: ${cssManager.bdTheme('#09090b', '#fafafa')};
          border: 1px solid ${cssManager.bdTheme('#e5e7eb', '#27272a')};
          box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
          background: ${cssManager.bdTheme('#ffffff', '#09090b')};
          border-radius: 6px;
          overflow: hidden;
        }

        .appbar {
          position: relative;
          color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
          background: ${cssManager.bdTheme('#f9fafb', '#18181b')};
          border-bottom: 1px solid ${cssManager.bdTheme('#e5e7eb', '#27272a')};
          height: 32px;
          display: flex;
          font-size: 13px;
          line-height: 32px;
          justify-content: center;
          align-items: center;
        }

        .appbar .fileName {
          line-height: inherit;
          position: relative;
          flex: 1;
          text-align: center;
        }

        .bottomBar {
          position: relative;
          color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
          background: ${cssManager.bdTheme('#f9fafb', '#18181b')};
          border-top: 1px solid ${cssManager.bdTheme('#e5e7eb', '#27272a')};
          height: 28px;
          font-size: 12px;
          line-height: 28px;
          display: flex;
          justify-content: flex-end;
          align-items: stretch;
          overflow: hidden;
        }

        .spacesLabel {
          padding: 0 16px;
          display: flex;
          align-items: center;
        }

        .languageLabel {
          color: ${cssManager.bdTheme('#3b82f6', '#3b82f6')};
          font-size: 12px;
          line-height: 28px;
          background: ${cssManager.bdTheme('rgba(59, 130, 246, 0.1)', 'rgba(59, 130, 246, 0.1)')};
          padding: 0px 16px;
          font-weight: 500;
          display: flex;
          align-items: center;
        }

        .hljs-keyword {
          color: ${cssManager.bdTheme('#dc2626', '#f87171')};
        }

        .codegrid {
          display: grid;
          grid-template-columns: 50px auto;
          overflow: hidden;
        }

        .lineNumbers {
          color: ${cssManager.bdTheme('#71717a', '#52525b')};
          padding: 24px 16px 0px 0px;
          text-align: right;
          border-right: 1px solid ${cssManager.bdTheme('#e5e7eb', '#27272a')};
        }

        .lineCounter:last-child {
          opacity: 50%;
        }

        pre {
          overflow-x: auto;
          margin: 0px;
          padding: 24px 24px;
        }

        code {
          font-weight: 400;
          padding: 0px;
          margin: 0px;
        }

        code,
        code *,
        .lineNumbers {
          line-height: 1.4em;
          font-weight: 200;
          font-family: ${cssMonoFontFamily};
        }

        .hljs-string {
          color: ${cssManager.bdTheme('#059669', '#10b981')};
        }

        .hljs-built_in {
          color: ${cssManager.bdTheme('#8b5cf6', '#a78bfa')};
        }

        .hljs-function {
          color: ${cssManager.bdTheme('#3b82f6', '#60a5fa')};
        }

        .hljs-params {
          color: ${cssManager.bdTheme('#0891b2', '#06b6d4')};
        }

        .hljs-comment {
          color: ${cssManager.bdTheme('#71717a', '#71717a')};
        }

        .hljs-number {
          color: ${cssManager.bdTheme('#ea580c', '#fb923c')};
        }

        .hljs-literal {
          color: ${cssManager.bdTheme('#dc2626', '#f87171')};
        }

        .hljs-attr {
          color: ${cssManager.bdTheme('#8b5cf6', '#a78bfa')};
        }

        .hljs-variable {
          color: ${cssManager.bdTheme('#09090b', '#fafafa')};
        }
      </style>
      <div
        class="mainbox"
        @contextmenu="${(eventArg) => {
          DeesContextmenu.openContextMenuWithOptions(eventArg, [
            {
              name: 'About',
              iconName: 'circleInfo',
              action: async () => {
                return null;
              },
            },
          ]);
        }}"
      >
        <div class="appbar">
          <dees-windowcontrols type="mac" position="left"></dees-windowcontrols>
          <div class="fileName">index.ts</div>
          <dees-windowcontrols type="mac" position="right"></dees-windowcontrols>
        </div>
        <div class="codegrid">
          <div class="lineNumbers">
            ${(() => {
              let lineCounter = 0;
              return this.codeToDisplay.split('\n').map((lineArg) => {
                lineCounter++;
                return html`<div class="lineCounter">${lineCounter}</div>`;
              });
            })()}
          </div>
          <pre><code></code></pre>
        </div>
        <div class="bottomBar">
          <div class="spacesLabel">Spaces: 2</div>
          <div class="languageLabel">${this.progLang}</div>
        </div>
      </div>
    `;
  }

  private codeToDisplayStore = '';
  private highlightJs: HLJSApi | null = null;

  public async updated(_changedProperties) {
    super.updated(_changedProperties);
    console.log('highlighting now');
    console.log(this.childNodes);
    const slottedCodeNodes: Text[] = [];
    this.childNodes.forEach((childNode) => {
      if (childNode.nodeName === '#text') {
        slottedCodeNodes.push(childNode as Text);
      }
    });
    if (this.codeToDisplay && this.codeToDisplay !== this.codeToDisplayStore) {
      this.codeToDisplayStore = smartstring.indent.normalize(this.codeToDisplay).trimStart();
    }
    if (slottedCodeNodes[0] && slottedCodeNodes[0].wholeText && !this.codeToDisplay) {
      this.codeToDisplayStore = smartstring.indent
        .normalize(slottedCodeNodes[0].wholeText)
        .trimStart();
      this.codeToDisplay = this.codeToDisplayStore;
    }
    await domtools.plugins.smartdelay.delayFor(0);

    // Load highlight.js from CDN if not already loaded
    if (!this.highlightJs) {
      this.highlightJs = await DeesServiceLibLoader.getInstance().loadHighlightJs();
    }

    const localCodeNode = this.shadowRoot.querySelector('code');
    const highlightedHtml = this.highlightJs.highlight(this.codeToDisplayStore, {
      language: this.progLang,
      ignoreIllegals: true,
    });
    localCodeNode.innerHTML = highlightedHtml.value;
  }
}
