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 TransportVehicle extends localize(i18next)(PageView) {
  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;
        }
      `
    ]
  }

  static get properties() {
    return {
      _searchFields: Array,
      config: Object,
      data: Object,
      importHandler: Object
    }
  }

  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_vehicle'),
      actions: [
        {
          title: i18next.t('button.save'),
          action: this._saveTransportVehicle.bind(this),
          ...CommonButtonStyles.save
        },
        {
          title: i18next.t('button.delete'),
          action: this._deleteTransportVehicle.bind(this),
          ...CommonButtonStyles.delete
        }
      ],
      exportable: {
        name: i18next.t('title.transport_vehicle'),
        data: this._exportableData.bind(this)
      },
      importable: {
        handler: this._importableData.bind(this)
      },
      help: 'operato-wms/master-data/vehicle'
    }
  }

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

  pageInitialized() {
    this._searchFields = [
      {
        label: i18next.t('field.reg_number'),
        name: 'regNumber',
        type: 'text',
        props: { searchOper: 'i_like' }
      },
      {
        label: i18next.t('field.size'),
        name: 'size',
        type: 'text',
        props: { searchOper: 'i_like' }
      },
      {
        label: i18next.t('field.status'),
        name: 'status',
        type: 'text',
        props: { searchOper: 'i_like' }
      }
    ]

    this.config = {
      rows: { selectable: { multiple: true } },
      list: {
        fields: ['name', 'description', 'updatedAt']
      },
      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.reg_number'),
          record: { editable: true },
          imex: { header: i18next.t('field.name'), key: 'name', width: 50, type: 'string' },
          sortable: true,
          width: 150
        },
        {
          type: 'string',
          name: 'description',
          header: i18next.t('field.description'),
          record: { editable: true },
          imex: { header: i18next.t('field.description'), key: 'description', width: 50, type: 'string' },
          sortable: true,
          width: 200
        },
        {
          type: 'string',
          name: 'size',
          header: i18next.t('field.size'),
          record: { editable: true },
          imex: { header: i18next.t('field.size'), key: 'size', width: 50, type: 'string' },
          sortable: true,
          width: 80
        },
        {
          type: 'string',
          name: 'status',
          header: i18next.t('field.status'),
          record: { editable: true },
          imex: { header: i18next.t('field.status'), key: 'status', width: 50, type: 'string' },
          sortable: true,
          width: 100
        },
        {
          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 transportVehicles($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
          transportVehicles(filters: $filters, pagination: $pagination, sortings: $sortings) {
            items {
              id
              name
              regNumber
              description
              size
              status
              updatedAt
              updater {
                id
                name
                description
              }
            }
            total
          }
        }
      `,
      variables: { filters, pagination, sortings }
    })

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

  async importHandler(patches) {
    try {
      if (patches.some(p => (!p.name || !p.size || !p.status) && 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 updateMultipleTransportVehicle($patches: [TransportVehiclePatch!]!) {
            updateMultipleTransportVehicle(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 _saveTransportVehicle() {
    try {
      let patches = this.dataGrist.exportPatchList({ flagName: 'cuFlag' })
      if (!patches?.length) {
        CustomAlert({
          title: i18next.t('text.nothing_changed'),
          text: i18next.t('text.there_is_nothing_to_save')
        })
      } else {
        if (patches.some(p => (!p.name || !p.size || !p.status) && p.cuFlag == '+'))
          throw new Error(i18next.t('text.empty_value_in_list'))

        const response = await client.mutate({
          mutation: gql`
            mutation updateMultipleTransportVehicle($patches: [TransportVehiclePatch!]!) {
              updateMultipleTransportVehicle(patches: $patches)
            }
          `,
          variables: { patches }
        })

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

  async _deleteTransportVehicle() {
    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 deleteTransportVehicles($ids: [String!]!) {
            deleteTransportVehicles(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-vehicle', TransportVehicle)
