import '@things-factory/grist-ui'

import { css, html, LitElement } from 'lit-element'
import { connect } from 'pwa-helpers/connect-mixin'

import { SingleColumnFormStyles } from '@things-factory/form-ui'
import { i18next, localize } from '@things-factory/i18n-base'
import { store, CustomAlert } from '@things-factory/shell'
import { isMobileDevice } from '@things-factory/utils'

class PutawaySameCartonPopup extends connect(store)(localize(i18next)(LitElement)) {
  static get properties() {
    return {
      config: Object,
      data: Object,
      putawayInventories: Array
    }
  }

  constructor() {
    super()
    this._selectedInventoryIds = []
  }

  static get styles() {
    return [
      SingleColumnFormStyles,
      css`
        .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);
        }
        .button-container button {
          background-color: var(--button-container-button-background-color);
          border-radius: var(--button-container-button-border-radius);
          height: var(--button-container-button-height);
          border: var(--button-container-button-border);
          margin: var(--button-container-button-margin);

          padding: var(--button-padding);
          color: var(--theme-white-color);
          font: var(--button-font);
          text-transform: var(--button-text-transform);
        }
        .button-container button:hover,
        .button-container button:active {
          background-color: var(--button-background-focus-color);
        }
        data-grist {
          overflow-y: hidden;
          flex: 1;
        }
        .grist {
          display: flex;
          flex-direction: column;
          flex: 1;
          overflow-y: auto;
        }
        :host {
          padding: 10px;
          display: flex;
          flex-direction: column;
          overflow: hidden;
          background-color: white;
        }
      `
    ]
  }

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

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

  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._selectPutawayInventories.bind(this)}"
          label="${i18next.t('button.select')}"
        ></mwc-button>
      </div>
    `
  }

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

  async _selectPutawayInventories() {
    try {
      let selectedInventories = []
      selectedInventories = this.dataGrist.selected.map(
        ({ name, expirationDate, minInboundShelfLife, palletId, cartonId }) => ({
          name,
          expirationDate,
          minInboundShelfLife,
          palletId,
          cartonId
        })
      )
      if (!selectedInventories?.length) throw new Error(i18next.t('text.select_at_least_one_inventory_to_putaway'))

      this.dispatchEvent(new CustomEvent('selections', { detail: selectedInventories }))
      history.back()
    } catch (e) {
      this._showToast(e)
    }
  }

  _showToast({ type, message }) {
    document.dispatchEvent(
      new CustomEvent('notify', {
        detail: {
          type,
          message
        }
      })
    )
  }
}

window.customElements.define('putaway-same-carton-popup', PutawaySameCartonPopup)
