import { css, html, LitElement } from 'lit'
import { customElement, property } from 'lit/decorators.js'
import { connect } from 'pwa-helpers/connect-mixin'

import { store } from '@operato/shell'
import { ScrollbarStyles } from '@operato/styles'

@customElement('domain-switch')
export class DomainSwitch extends connect(store)(LitElement) {
  static styles = [
    ScrollbarStyles,
    css`
      :host {
        display: flex;
        max-width: 100%;
        background-color: rgba(var(--primary-color-rgb), 0.1);
        border-bottom: var(--border-dark-color);
      }

      :host * {
        vertical-align: middle;
      }

      div {
        flex: 1;
        display: flex;
        flex-direction: row;
        padding: var(--padding-default);
      }

      mwc-icon {
        background-color: rgba(var(--primary-color-rgb), 0.8);
        margin-right: var(--margin-narrow);
        padding: 2px;
        border-radius: 50%;
        font-size: var(--fontsize-large);
        color: var(--theme-white-color);
      }
      span,
      select {
        flex: 1;
        color: var(--secondary-color);
        font: bold 14px/20px var(--theme-font);
      }

      select {
        border: none;
        background-color: transparent;
      }

      select:focus {
        outline: 0;
      }
      :host([dark]) {
        background-color: rgba(0, 0, 0, 0.2);
        padding: 0 !important;
        border-bottom: none;
      }
      :host([dark]) mwc-icon {
        background-color: var(--secondary-text-color);
        margin: 1px 4px 0px 0px;
        padding: 1px 2px;
        border-radius: 50%;
        line-height: 19px;
      }
      :host([dark]) span,
      :host([dark]) select {
        color: var(--theme-white-color);
        font: bold 13px/13px var(--theme-font);
      }
      :host([dark]) option {
        background-color: var(--primary-color, #585858);
        color: var(--theme-white-color, #fff);
      }
      :host([dark]) span {
        line-height: 23px;
      }

      :host([rounded-corner]) {
        height: 30px;
        border-radius: 20px;
        border: var(--border-dark-color);
      }
      :host([rounded-corner]) div {
        padding: var(--padding-narrow) var(--padding-default);
      }
    `
  ]

  @property({ type: Array }) domains: any[] = []
  @property({ type: Object }) domain: any
  @property({ type: String, attribute: true }) attrname: string = 'name'

  render() {
    const domains = this.domains || []
    const domain = this.domain || {}
    const attrname = this.attrname || 'name'

    return html`
      <div>
        <mwc-icon>outlined_flag</mwc-icon>
        ${domains.length <= 1
          ? html` <span>${domains[0]?.[attrname] || domain.name}</span> `
          : html`
              <select
                .value=${domain.subdomain}
                @change=${e => (window.location.pathname = `/auth/checkin/${e.target.value}`)}
              >
                ${domains.map(
                  d => html`
                    <option .value=${d.subdomain} ?selected=${d.subdomain == domain.subdomain}>${d[attrname]}</option>
                  `
                )}
              </select>
            `}
      </div>
    `
  }

  stateChanged(state) {
    this.domains = state.app.domains
    this.domain = state.app.domain
  }
}
