import '@things-factory/form-ui'
import '@things-factory/grist-ui'
import '@things-factory/import-ui'

import gql from 'graphql-tag'
import { css, html } from 'lit-element'

import { i18next, localize } from '@things-factory/i18n-base'
import { openPopup } from '@things-factory/layout-base'
import { client, CustomAlert, PageView } from '@things-factory/shell'
import { CommonButtonStyles, ScrollbarStyles } from '@things-factory/styles'
import { isMobileDevice } from '@things-factory/utils'
import { showToast } from '../../util/index'

class TransportDriver extends localize(i18next)(PageView) {
  static get properties() {
    return {
      config: Object,
      data: Object,
      importHandler: Object
    }
  }

  static get styles() {
    return [
      ScrollbarStyles,
      css`
        :host {
          display: flex;
          flex-direction: column;
          overflow: hidden;
        }

        search-form {
          overflow: visible;
        }

        data-grist {
          overflow-y: auto;
          flex: 1;
        }
      `
    ]
  }

  render() {
    return html`
      <search-form id="search-form" .fields=${this._searchFields} @submit=${e => this.dataGrist.fetch()}></search-form>

      <data-grist
        .mode=${isMobileDevice() ? 'LIST' : 'GRID'}
        .config=${this.config}
        .fetchHandler="${this.fetchHandler.bind(this)}"
      ></data-grist>
    `
  }

  get context() {
    return {
      title: i18next.t('title.transport_driver'),
      actions: [
        {
          title: i18next.t('button.save'),
          action: this._saveTransportDriver.bind(this),
          ...CommonButtonStyles.save
        },
        {
          title: i18next.t('button.delete'),
          action: this._deleteTransportDriver.bind(this),
          ...CommonButtonStyles.delete
        }
      ],
      exportable: {
        name: i18next.t('title.transport_driver'),
        data: this._exportableData.bind(this)
      },
      importable: {
        handler: this._importableData.bind(this)
      },
      help: 'operato-wms/master-data/driver'
    }
  }

  pageUpdated(changes, lifecycle) {
    if (this.active) {
      this.dataGrist.fetch()
    }
  }

  async pageInitialized() {
    this._searchFields = [
      {
        label: i18next.t('field.name'),
        name: 'name',
        type: 'text',
        props: { searchOper: 'i_like' }
      },
      {
        label: i18next.t('field.driver_code'),
        name: 'driverCode',
        type: 'text',
        props: { searchOper: 'i_like' }
      },
      {
        label: i18next.t('field.description'),
        name: 'description',
        type: 'text',
        props: { searchOper: 'i_like' }
      }
    ]

    this.config = {
      rows: { selectable: { multiple: true } },
      pagination: { limit: 50, pages: [50, 100, 200, 500] },
      columns: [
        { type: 'gutter', gutterName: 'dirty' },
        { type: 'gutter', gutterName: 'sequence' },
        { type: 'gutter', gutterName: 'row-selector', multiple: true },
        {
          type: 'string',
          name: 'name',
          header: i18next.t('field.name'),
          record: {
            editable: true
          },
          imex: { header: i18next.t('field.name'), key: 'name', width: 50, type: 'string' },
          sortable: true,
          width: 250
        },
        {
          type: 'string',
          name: 'description',
          header: i18next.t('field.description'),
          record: {
            editable: true
          },
          imex: { header: i18next.t('field.description'), key: 'description', width: 100, type: 'string' },
          sortable: true,
          width: 200
        },
        {
          type: 'string',
          name: 'driverCode',
          header: i18next.t('field.driver_code'),
          record: { editable: true },
          imex: { header: i18next.t('field.driver_code'), key: 'driverCode', width: 100, type: 'string' },
          sortable: true,
          width: 80
        },
        {
          type: 'datetime',
          name: 'updatedAt',
          header: i18next.t('field.updated_at'),
          sortable: true,
          width: 150
        },
        {
          type: 'object',
          name: 'updater',
          header: i18next.t('field.updater'),
          sortable: true,
          width: 150
        }
      ]
    }
  }

  get searchForm() {
    return this.shadowRoot.querySelector('search-form')
  }

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

  _importableData(records) {
    setTimeout(() => {
      openPopup(
        html`
          <import-pop-up
            .records=${records}
            .config=${{
              rows: this.config.rows,
              columns: [...this.config.columns.filter(column => column.imex !== undefined)]
            }}
            .importHandler="${this.importHandler.bind(this)}"
          ></import-pop-up>
        `,
        {
          backdrop: true,
          size: 'large',
          title: i18next.t('title.import')
        }
      )
    }, 500)
  }

  async fetchHandler({ page, limit, sorters = [] }) {
    const filters = this.searchForm.queryFilters
    const pagination = { page, limit }
    const sortings = sorters

    const response = await client.query({
      query: gql`
        query transportDrivers($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
          transportDrivers(filters: $filters, pagination: $pagination, sortings: $sortings) {
            items {
              id
              name
              driverCode
              description
              updatedAt
              updater {
                id
                name
                description
              }
            }
            total
          }
        }
      `,
      variables: { filters, pagination, sortings }
    })

    return {
      total: response.data.transportDrivers.total || 0,
      records: response.data.transportDrivers.items || []
    }
  }

  async importHandler(patches) {
    try {
      if (patches.some(p => (!p.name || !p.driverCode) && p.cuFlag == '+')) {
        throw new Error(i18next.t('text.empty_value_in_list'))
      }

      patches = patches.map(p => {
        return { ...p, cuFlag: '' }
      })
      const response = await client.mutate({
        mutation: gql`
          mutation updateMultipleTransportDriver($patches: [TransportDriverPatch!]!) {
            updateMultipleTransportDriver(patches: $patches)
          }
        `,
        variables: { patches }
      })

      if (!response.errors) {
        history.back()
        this.dataGrist.fetch()
        showToast({ message: i18next.t('text.data_imported_successfully') })
      }
    } catch (e) {
      showToast(e)
    }
  }

  async _saveTransportDriver() {
    try {
      let patches = this.dataGrist.exportPatchList({ flagName: 'cuFlag' })

      if (patches.some(p => (!p.name || !p.driverCode) && p.cuFlag == '+')) {
        throw new Error(i18next.t('text.empty_value_in_list'))
      }

      if (!patches?.length) {
        CustomAlert({
          title: i18next.t('text.nothing_changed'),
          text: i18next.t('text.there_is_nothing_to_save')
        })
      } else {
        const response = await client.mutate({
          mutation: gql`
            mutation updateMultipleTransportDriver($patches: [TransportDriverPatch!]!) {
              updateMultipleTransportDriver(patches: $patches)
            }
          `,
          variables: { patches }
        })

        if (!response.errors) {
          this.dataGrist.fetch()
          showToast({ message: i18next.t('text.data_updated_successfully') })
        }
      }
    } catch (e) {
      showToast(e)
    }
  }

  async _deleteTransportDriver() {
    const ids = this.dataGrist.selected.map(record => record.id)
    if (!ids?.length) {
      CustomAlert({
        title: i18next.t('text.nothing_selected'),
        text: i18next.t('text.there_is_nothing_to_delete')
      })
    } else {
      const answer = await CustomAlert({
        type: 'warning',
        title: i18next.t('button.delete'),
        text: i18next.t('text.are_you_sure'),
        confirmButton: { text: i18next.t('button.delete') },
        cancelButton: { text: i18next.t('button.cancel') }
      })

      if (!answer.value) return

      const response = await client.mutate({
        mutation: gql`
          mutation deleteTransportDrivers($ids: [String!]!) {
            deleteTransportDrivers(ids: $ids)
          }
        `,
        variables: { ids }
      })

      if (!response.errors) {
        this.dataGrist.fetch()
        showToast({ message: i18next.t('text.data_deleted_successfully') })
      }
    }
  }

  get _columns() {
    return this.config.columns
  }

  _exportableData() {
    let records = []
    if (this.dataGrist.selected && this.dataGrist.selected.length > 0) {
      records = this.dataGrist.selected
    } else {
      records = this.dataGrist.data.records
    }

    var headerSetting = this.dataGrist._config.columns
      .filter(column => column.type !== 'gutter' && column.record !== undefined && column.imex !== undefined)
      .map(column => {
        return column.imex
      })

    var data = records.map(item => {
      return {
        id: item.id,
        ...this._columns
          .filter(column => column.type !== 'gutter' && column.record !== undefined && column.imex !== undefined)
          .reduce((record, column) => {
            record[column.imex.key] = column.imex.key
              .split('.')
              .reduce((obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined), item)
            return record
          }, {})
      }
    })

    return { header: headerSetting, data: data }
  }
}

window.customElements.define('transport-driver', TransportDriver)
