import '@things-factory/form-ui'
import '@things-factory/grist-ui'
import '@things-factory/import-ui'
import { client, CustomAlert, PageView, store } from '@things-factory/shell'
import { connect } from 'pwa-helpers/connect-mixin'
import { css, html } from 'lit-element'
import { getCodeByName } from '@things-factory/code-base'
import { ScrollbarStyles, CommonButtonStyles } from '@things-factory/styles'
import { isMobileDevice } from '@things-factory/utils'
import { i18next, localize } from '@things-factory/i18n-base'
import { openPopup } from '@things-factory/layout-base'
import { REUSABLE_PALLET_LABEL_SETTING_KEY } from '../constants'
import { DirectPrinter } from '@things-factory/barcode-base'
import gql from 'graphql-tag'
import { showToast } from '../../util/index'
import isEmpty from 'lodash-es/isEmpty'

class PalletList extends connect(store)(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.pallets'),
      actions: [
        {
          title: i18next.t('button.print_label'),
          action: this._printPalletLabel.bind(this),
          ...CommonButtonStyles.label
        },
        {
          title: i18next.t('button.save'),
          action: this._savePallet.bind(this),
          ...CommonButtonStyles.save
        },
        {
          title: i18next.t('button.delete'),
          action: this._deletePallet.bind(this),
          ...CommonButtonStyles.delete
        }
      ],
      exportable: {
        name: i18next.t('title.pallet'),
        data: this._exportableData.bind(this)
      },
      importable: {
        handler: this._importableData.bind(this)
      },
      help: 'operato-wms/master-data/pallet'
    }
  }

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

  async pageInitialized() {
    const _palletStatus = await getCodeByName('PALLET_STATUS')

    this._searchFields = [
      {
        label: i18next.t('field.ref_no'),
        name: 'name',
        type: 'text',
        props: { searchOper: 'i_like' }
      },
      {
        label: i18next.t('field.status'),
        name: 'status',
        type: 'select',
        options: [
          { value: '' },
          ..._palletStatus.map(status => {
            return {
              name: status.name,
              value: status.name
            }
          })
        ],
        props: { searchOper: 'eq' }
      }
    ]

    this.config = {
      list: {
        fields: ['name', 'owner', 'holder', 'status']
      },
      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.ref_no'),
          imex: { header: i18next.t('field.name'), key: 'name', width: 50, type: 'string' },
          record: {
            editable: true
          },
          sortable: true,
          width: 100
        },
        {
          type: 'object',
          name: 'owner',
          header: i18next.t('field.owner'),
          imex: { header: i18next.t('field.owner'), key: 'owner.name', width: 50, type: 'array', arrData: [] },
          record: {
            editable: true,
            options: {
              queryName: 'bizplaces'
            }
          },
          sortable: true,
          width: 250
        },
        {
          type: 'object',
          name: 'holder',
          header: i18next.t('field.holder'),
          imex: {
            header: i18next.t('field.holder'),
            key: 'holder.name',
            width: 50,
            type: 'array',
            arrData: []
          },
          record: {
            editable: true,
            options: {
              queryName: 'bizplaces'
            }
          },
          sortable: true,
          width: 250
        },
        {
          type: 'code',
          name: 'status',
          header: i18next.t('field.status'),
          imex: {
            header: i18next.t('field.status'),
            key: 'status',
            width: 50,
            type: 'array',
            arrData: _palletStatus.map(status => {
              return {
                name: status.name
              }
            })
          },
          record: {
            editable: true,
            codeName: 'PALLET_STATUS'
          },
          sortable: true,
          width: 130
        },
        {
          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 response = await client.query({
      query: gql`
        query pallets($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
          pallets(filters: $filters, pagination: $pagination, sortings: $sortings) {
            items {
              id
              name
              owner {
                id
                name
                description
              }
              holder {
                id
                name
                description
              }
              status
              updatedAt
              updater {
                id
                name
                description
              }
            }
            total
          }
        }
      `,
      variables: {
        filters: [...this.searchForm.queryFilters],
        pagination: { page, limit },
        sortings: sorters
      }
    })

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

  async importHandler(patches) {
    try {
      const _palletStatus = await getCodeByName('PALLET_STATUS')
      const getBizplace = await this.fetchBizplaces()
      let records = this.dataGrist.data.records
      patches = patches.map(itm => {
        //  Error if there is an empty value
        if (isEmpty(itm.name) || isEmpty(itm.status) || isEmpty(itm.holder.name) || isEmpty(itm.owner.name) || ''){
          throw new Error(i18next.t('text.empty_value_in_list'))
        }
        // Error if location name is already existed
        if (records.find(exist => exist.name == itm.name)) {
          throw new Error(i18next.t('text.pallet_name(s)_is_already_existed'))
        }
        // Error if pallet status is does not exist
        let foundPalletStatus = _palletStatus.find(exist => exist.name == itm.status)
        if (!foundPalletStatus) {
          throw new Error(i18next.t('text.pallet_status_does_not_exist'))
        }
        //  Error if the owner is not belong to the domain or doesn't exist
        let foundOwner = getBizplace.find(exist => exist.name == itm.owner.name)
        if (!foundOwner){
          throw new Error(i18next.t('text.invalid_owner'))
        }
        //  Error if the holder is not belong to the domain or doesn't exist
        let foundHolder = getBizplace.find(exist=>exist.name == itm.holder.name)
        if (!foundHolder) {
          throw new Error(i18next.t('text.invalid_holder'))
        }
        return itm
      })

      patches = patches.map(p => {
        return { ...p, cuFlag: '' }
      })
      this.dataGrist.showSpinner()
      const response = await client.query({
        query: gql`
          mutation updateMultiplePallet($patches: [PalletPatch!]!) {
            updateMultiplePallet(patches: $patches)
          }
        `,
        variables: {
          patches
        }
      })
      if (!response.errors) {
        history.back()
        this.dataGrist.fetch()
        document.dispatchEvent(
          new CustomEvent('notify', {
            detail: {
              message: i18next.t('text.data_imported_successfully')
            }
          })
        )
      }
      this.dataGrist.hideSpinner()
    } catch (e) {
      showToast(e)
    }
  }

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

      if (patches.some(p => (!p.name || !p.owner || !p.holder || !p.status) && p.cuFlag == '+'))
        throw new Error(i18next.t('text.empty_value_in_list'))

      if (patches && patches.length) {
        this.dataGrist.showSpinner()
        const response = await client.mutate({
          mutation: gql`
            mutation updateMultiplePallet($patches: [PalletPatch!]!) {
              updateMultiplePallet(patches: $patches)
            }
          `,
          variables: {
            patches
          }
        })

        if (!response.errors) {
          this.dataGrist.fetch()
          document.dispatchEvent(
            new CustomEvent('notify', {
              detail: {
                message: i18next.t('text.data_updated_successfully')
              }
            })
          )
        }
        this.dataGrist.hideSpinner()
      }
    } catch (e) {
      showToast(e)
    }
  }

  async fetchBizplaces(bizplace = []) {
    const response = await client.query({
      query: gql`
        query bizplaces($filters: [Filter!]) {
          bizplaces(filters: $filters) {
            items {
              id
              name
              description
            }
          }
        }
      `,
      variables: {
        filters: [...bizplace]
      }
    })
    return response.data.bizplaces.items
  }
  
  async _deletePallet() {
    CustomAlert({
      title: i18next.t('text.are_you_sure'),
      text: i18next.t('text.you_wont_be_able_to_revert_this'),
      type: 'warning',
      confirmButton: { text: i18next.t('button.delete'), color: '#22a6a7' },
      cancelButton: { text: 'cancel', color: '#cfcfcf' },
      callback: async result => {
        if (result.value) {
          this.dataGrist.showSpinner()
          const id = this.dataGrist.selected.map(record => record.id)
          if (id && id.length > 0) {
            const response = await client.query({
              query: gql`
                mutation deletePallets($id: [String!]!) {
                  deletePallets(ids: $id)
                }
              `,
              variables: { id }
            })

            if (!response.errors) {
              this.dataGrist.fetch()
              document.dispatchEvent(
                new CustomEvent('notify', {
                  detail: {
                    message: i18next.t('text.data_deleted_successfully')
                  }
                })
              )
            }
          } else {
            CustomAlert({
              title: i18next.t('text.nothing_selected'),
              text: i18next.t('text.there_is_nothing_to_delete')
            })
          }
          this.dataGrist.hideSpinner()
        }
      }
    })
  }

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

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

    const getBizplace = await this.fetchBizplaces()

    var headerSetting = this.dataGrist._config.columns
      .filter(column => column.type !== 'gutter' && column.record !== undefined && column.imex !== undefined)
      .map(column => {
        if (column.imex.key == 'holder.name') {
          column.imex.arrData = getBizplace.map(item => {
            return {
              id: item.id,
              name: item.name
            }
          })
        }
        if (column.imex.key == 'owner.name') {
          column.imex.arrData = getBizplace.map(item => {
            return {
              id: item.id,
              name: item.name
            }
          })
        }
        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 }
  }

  async _printPalletLabel() {
    var labelId = this._palletLabel && this._palletLabel.id
    let items = []

    if (this.dataGrist.selected && this.dataGrist.selected.length > 0) {
      items = this.dataGrist.selected
    }

    if (!labelId) {
      document.dispatchEvent(
        new CustomEvent('notify', {
          detail: {
            level: 'error',
            message: `${i18next.t('text.no_label_setting_was_found')}. ${i18next.t('text.please_check_your_setting')}`
          }
        })
      )
    } else {
      if (items.length <= 0) {
        document.dispatchEvent(
          new CustomEvent('notify', {
            detail: {
              level: 'error',
              message: `${i18next.t('text.theres_no_pallet_selected')}`
            }
          })
        )
      }

      for (var item of items) {
        var searchParams = new URLSearchParams()

        /* for pallet record mapping */
        searchParams.append('pallet', item.name)

        try {
          const response = await fetch(`label-command/${labelId}?${searchParams.toString()}`, {
            method: 'GET'
          })

          if (response.status !== 200) {
            throw `Error: Can't get label command from server (response: ${response.status})`
          }

          var command = await response.text()

          if (!this.printer) {
            this.printer = new DirectPrinter()
          }

          await this.printer.print(command)
        } catch (ex) {
          document.dispatchEvent(
            new CustomEvent('notify', {
              detail: {
                level: 'error',
                message: ex,
                ex
              }
            })
          )

          delete this.printer
          break
        }
      }
    }
  }

  stateChanged(state) {
    var palletLabelSetting = state.dashboard[REUSABLE_PALLET_LABEL_SETTING_KEY]
    this._palletLabel = (palletLabelSetting && palletLabelSetting.board) || {}
  }
}

window.customElements.define('pallet-list', PalletList)
