import * as plugins from './00plugins.js';
import * as interfaces from './interfaces/index.js';

import {
  DeesElement,
  type TemplateResult,
  property,
  customElement,
  html,
  css,
  cssManager,
} from '@design.estate/dees-element';
import { DeesContextmenu } from './dees-contextmenu.js';

/**
 * the most left menu
 * usually used as organization selector
 */
@customElement('dees-appui-mainmenu')
export class DeesAppuiMainmenu extends DeesElement {
  public static demo = () => html`<dees-appui-mainmenu></dees-appui-mainmenu>`;

  // INSTANCE

  // INSTANCE
  @property()
  public tabs: interfaces.ITab[] = [
    { key: 'option 1', iconName: 'building', action: () => {} },
    { key: 'option 2', iconName: 'building', action: () => {} },
    { key: 'option 3', iconName: 'building', action: () => {} },
    { key: 'option 4', iconName: 'building', action: () => {} },
  ];

  @property()
  public selectedTab: interfaces.ITab;

  public static styles = [
    cssManager.defaultStyles,
    css`
      .mainContainer {
        --menuSize: 60px;
        color: #ccc;
        z-index: 10;
        display: block;
        position: relative;
        width: var(--menuSize);
        height: 100%;
        background: #000000;
        box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.5);
        user-select: none;
        border-right: 1px solid #202020;
      }

      .tabsContainer {
      }

      .tab {
        padding-top: 18px;
        font-size: 24px;
        width: var(--menuSize);
        height: var(--menuSize);
        text-align: center;
        transition: color 0.1s, background 0.2s;
      }

      .tab:hover {
        background: rgba(255, 255, 255, 0.15);
      }

      .tab.selectedTab {
        color: #fff;
        background: rgba(255, 255, 255, 0.1);
      }

      .tabIndicator {
        opacity: 0;
        background: #4e729a;
        position: absolute;
        width: 5px;
        height: calc((var(--menuSize) / 3) * 2);
        left: 0px;
        top: calc(var(--menuSize) - (((var(--menuSize) / 3) * 2) / 2));
        border-top-right-radius: 7px;
        border-bottom-right-radius: 7px;
        transition: all 0.1s;
      }
    `,
  ];

  public render(): TemplateResult {
    return html`
      <div class="mainContainer" @contextmenu=${(eventArg) => {
        DeesContextmenu.openContextMenuWithOptions(eventArg, [{
          name: 'app settings',
          action: async () => {},
          iconName: 'gear',
        }])
      }}>
        <div class="tabsContainer">
          ${this.tabs.map((tabArg) => {
            return html`
              <div
                class="tab ${tabArg === this.selectedTab ? 'selectedTab' : null}"
                @click="${() => {
                  this.updateTab(tabArg);
                }}"
              >
                <dees-icon iconFA="${tabArg.iconName as any}"></dees-icon>
              </div>
            `;
          })}
        </div>
        <div class="tabIndicator"></div>
      </div>
    `;
  }

  private async updateTabIndicator() {
    let selectedTab = this.selectedTab;
    if (!selectedTab) {
      selectedTab = this.tabs[0];
    }
    const tabIndex = this.tabs.indexOf(selectedTab);
    const selectedTabElement: HTMLElement = this.shadowRoot.querySelector(
      `.tabsContainer .tab:nth-child(${tabIndex + 1})`
    );
    const tabIndicator: HTMLElement = this.shadowRoot.querySelector('.tabIndicator');
    const offsetTop = selectedTabElement.offsetTop;
    tabIndicator.style.opacity = `1`;
    tabIndicator.style.top = `calc(${offsetTop}px + (var(--menuSize) / 6))`;
  }

  updateTab(tabArg: interfaces.ITab) {
    this.selectedTab = tabArg;
    this.updateTabIndicator();
    this.selectedTab.action();
  }

  firstUpdated() {
    this.updateTab(this.tabs[0]);
  }
}