import '@things-factory/grist-ui'
import { i18next } from '@things-factory/i18n-base'
import { isMobileDevice } from '@things-factory/utils'
import { css, html, LitElement } from 'lit-element'

class SelectProductBatch extends LitElement {
  static get properties() {
    return {
      config: Object,
      data: Object,
      orderProducts: Array
    }
  }

  static get styles() {
    return [
      css`
        :host {
          padding: 10px;
          display: flex;
          flex-direction: column;
          overflow-x: overlay;
          background-color: var(--main-section-background-color);
        }
        .grist {
          background-color: var(--main-section-background-color);
          display: flex;
          flex-direction: column;
          flex: 1;
          overflow-y: auto;
        }
        data-grist {
          overflow-y: hidden;
          flex: 1;
        }
        .button-container {
          padding: var(--button-container-padding);
          margin: var(--button-container-margin);
          text-align: var(--button-container-align);
          background-color: var(--button-container-background);
          height: var(--button-container-height);
        }
      `
    ]
  }

  render() {
    return html`
      <div class="grist">
        <data-grist
          id="grist"
          .mode=${isMobileDevice() ? 'LIST' : 'GRID'}
          .config=${this.config}
          .data="${this.data}"
        ></data-grist>
      </div>

      <div class="button-container">
        <mwc-button
          raised
          @click="${this._selectProductBatch.bind(this)}"
          label="${i18next.t('button.select')}"
        ></mwc-button>
      </div>
    `
  }

  firstUpdated() {
    this.config = {
      pagination: { infinite: true },
      rows: { selectable: { multiple: false }, appendable: false },
      list: { fields: ['sku', 'batchId', 'product', 'packingType', 'packingSize'] },
      columns: [
        { type: 'gutter', gutterName: 'sequence' },
        { type: 'gutter', gutterName: 'row-selector', multiple: false },
        {
          type: 'string',
          name: 'sku',
          header: i18next.t('field.sku'),
          width: 120
        },
        {
          type: 'string',
          name: 'batchId',
          header: i18next.t('field.batch_id'),
          width: 150
        },
        {
          type: 'object',
          name: 'product',
          header: i18next.t('field.product'),
          width: 250
        },
        {
          type: 'string',
          name: 'packingType',
          header: i18next.t('field.packing_type'),
          width: 120
        },
        {
          type: 'number',
          name: 'packingSize',
          header: i18next.t('field.packing_size'),
          width: 100
        }
      ]
    }
  }

  get dataGrist() {
    return this.shadowRoot.querySelector('data-grist')
  }

  updated(changeProps) {
    if (changeProps.has('orderProducts')) {
      if (this.orderProducts && this.orderProducts.length) {
        this.data = { records: this.orderProducts }
      }
    }
  }

  _selectProductBatch() {
    const selectedBatch = this.dataGrist.selected[0]
    if (selectedBatch) {
      this.dispatchEvent(new CustomEvent('selected-batch', { detail: selectedBatch }))
      history.back()
    } else {
      document.dispatchEvent(
        new CustomEvent('notify', { detail: { message: i18next.t('text.batch_is_not_selected') } })
      )
    }
  }
}
window.customElements.define('select-product-batch', SelectProductBatch)
