import '@material/mwc-icon-button'
import '@material/mwc-button'

import { css, html, LitElement } from 'lit'
import { customElement, property, query } from 'lit/decorators.js'

import { i18next, localize } from '@operato/i18n'

@customElement('home-page')
export class HomePage extends localize(i18next)(LitElement) {
  static styles = [
    css`
      :host {
        display: block;
        position: relative;
      }

      [home] {
        position: absolute;
        left: 20px;
        top: 10px;
        font-size: 2em;
        color: var(--primary-color);
      }

      [signin] {
        position: absolute;
        right: 20px;
        top: 20px;
        font-size: 2em;
      }

      [message] {
        background-color: var(--main-section-background-color);
        padding: 60px 50px 0 50px;
        color: var(--secondary-color);
        text-align: center;
        font-size: 20px;
      }

      [message] strong {
        display: block;
        font-size: 2.5rem;
      }

      [message] img {
        width: 800px;
        max-width: 90%;
        display: block;
        margin: auto;
        margin-top: -15px;
      }

      [ticket] {
        display: grid;
        grid-template-columns: 96px 1fr;
        width: 450px;
        margin: auto;
        margin-top: -20px;
        margin-bottom: 60px;
      }

      [brand] {
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 12px;
        overflow: hidden;
        background-color: var(--primary-color);
        box-shadow: var(--box-shadow);
        border-right: 3px dotted var(--main-section-background-color);
        text-align: center;
      }

      [ticket] [content] {
        border-radius: 12px;
        background-color: var(--theme-white-color);
        padding: var(--padding-default) var(--padding-wide);
        box-shadow: var(--box-shadow);
      }

      fieldset {
        border: none;
        margin: 0;
        padding: 0;
      }

      legend {
        margin: 0;
        padding: var(--padding-narrow) 0 var(--padding-default) 0;
        font-weight: bold;
        color: var(--legend-color);
        text-transform: uppercase;
      }

      input {
        border: var(--border-dark-color);
        border-radius: var(--border-radius);
        padding: var(--input-padding);
        font: var(--input-font);
      }

      input[type='text'] {
        min-width: 190px;
      }

      input:focus {
        outline: none;
      }

      @media screen and (max-width: 480px) {
        [message] {
          padding: 60px 10px 0 10px;
          text-align: center;
          font-size: 14px;
        }
        [message] strong {
          margin-bottom: 15px;
          font-size: 1.6rem;
          line-height: 1.2em;
        }
        [message] img {
          max-width: 100%;
        }

        legend {
          padding: 0;
        }

        [ticket] {
          display: grid;
          grid-template-columns: 0 1fr;
          width: 90%;
          margin: auto;
          margin-top: -15px;
          margin-bottom: 60px;
        }
        [brand] {
          border: 0;
        }
        [content] {
          border: 1px solid var(--primary-color);
        }

        fieldset {
          text-align: center;
        }
        input[type='text'] {
          width: calc(100% - 20px);
          margin: var(--margin-narrow) 0 var(--margin-default) 0;
          text-align: center;
        }
        fieldset mwc-button {
          width: 100%;
        }
      }
    `
  ]

  @property({ type: String, attribute: true }) tagline?: string
  @property({ type: String, attribute: true }) explanation1?: string
  @property({ type: String, attribute: true }) explanation2?: string

  private _applicationMeta?: { icon?: string; title?: string; description?: string }

  render() {
    var { icon, title } = this.applicationMeta

    const tagline = this.tagline || `Join the ${title} Business Program`
    const explanation1 = this.explanation1 || 'Create your free account today.'
    const explanation2 = this.explanation2 || 'Everything you need to go from where you are, to where you want to be.'

    return html`
      <mwc-icon-button home icon="home" @click=${e => (window.location.href = '/')}></mwc-icon-button>
      <mwc-button signin dense @click=${e => (window.location.href = '/auth/signin')}>sign in</mwc-button>

      <div message>
        <strong>${tagline}</strong> ${explanation1} <br />
        ${explanation2}
        <img src="/assets/images/invitation.png" />
      </div>

      <div ticket>
        <div brand><img src=${icon} /></div>
        <div content>
          <form action="/auth/join" accept-charset="utf-8" name="join" method="POST">
            <fieldset>
              <legend>${title} invitation ticket</legend>
              <input type="text" name="email" placeholder="Enter your email address" />
              <input type="submit" value="join now" />
              <!-- help this button, pls <mwc-button raised label="join now"></mwc-button> -->
            </fieldset>
          </form>
        </div>
      </div>
    `
  }

  @query('input[type=text]') firstInput!: HTMLInputElement

  firstUpdated() {
    setTimeout(() => {
      this.firstInput.focus()
    }, 100)
  }

  get applicationMeta() {
    if (!this._applicationMeta) {
      var iconLink: HTMLLinkElement | null = document.querySelector('link[rel="application-icon"]')
      var titleMeta: HTMLMetaElement | null = document.querySelector('meta[name="application-name"]')
      var descriptionMeta: HTMLMetaElement | null = document.querySelector('meta[name="application-description"]')

      this._applicationMeta = {
        icon: iconLink?.href,
        title: titleMeta ? titleMeta.content : 'Things Factory',
        description: descriptionMeta ? descriptionMeta.content : 'Reimagining Software'
      }
    }

    return this._applicationMeta
  }
}
