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

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

import {
  i18next,
  localize
} from '@things-factory/i18n-base'
import {
  client,
  PageView,
  store
} from '@things-factory/shell'
import {
  flattenObject,
  isMobileDevice
} from '@things-factory/utils'

class ElcclInventorySummaryReport extends connect(store)(localize(i18next)(PageView)) {
  static get styles() {
    return css`
      :host {
        display: flex;
        flex-direction: column;
        width: 100%;
      }

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

  static get properties() {
    return {
      _searchFields: Object,
      _config: Object,
      _partners: Object,
      data: Object
    }
  }

  get context() {
    return {
      title: i18next.t('title.inventory_summary_report'),
      exportable: {
        name: i18next.t('title.inventory_summary_report'),
        data: this._exportableData.bind(this)
      }
    }
  }

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

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

  get _bizplaceSelector() {
    return this.searchForm.shadowRoot.querySelector('select[name=bizplace]')
  }

  get _fromDateInput() {
    return this.searchForm.shadowRoot.querySelector('input[name=fromDate]')
  }

  get _toDateInput() {
    return this.searchForm.shadowRoot.querySelector('input[name=toDate]')
  }

  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 searchFields() {
    return [
      {
        label: i18next.t('field.company'),
        name: 'bizplace',
        type: 'select',
        options: [
          { value: '' },
          ...this._partners
            .map(partner => {
              return {
                name: partner.name,
                value: partner.id
              }
            })
            .sort(this._compareValues('name', 'asc'))
        ],
        props: { searchOper: 'eq' }
      },
      {
        label: i18next.t('field.from_date'),
        name: 'fromDate',
        type: 'date',
        props: {
          searchOper: 'eq',
          max: new Date().toISOString().split('T')[0]
        },
        value: (() => {
          let date = new Date()
          date.setMonth(date.getMonth() - 6)
          return date.toISOString().split('T')[0]
        })(),
        handlers: { change: this._modifyDateRange.bind(this) }
      },
      {
        label: i18next.t('field.to_date'),
        name: 'toDate',
        type: 'date',
        props: {
          searchOper: 'eq',
          min: (() => {
            let date = new Date()
            date.setMonth(date.getMonth() - 1)
            return date.toISOString().split('T')[0]
          })(),
          max: new Date().toISOString().split('T')[0]
        },
        value: new Date().toISOString().split('T')[0]
      },
      {
        label: i18next.t('field.show_balance_only'),
        name: 'balanceOnly',
        type: 'checkbox',
        props: { searchOper: 'eq' }
      }
    ]
  }

  get reportConfig() {
    return {
      list: {
        fields: [
          'batchId',
          'product|name',
          'packingType',
          'initialDate',
          'initialQty',
          'adjustmentQty',
          'totalReturnQty',
          'totalOutQty',
          'closingQty'
        ]
      },
      rows: {
        selectable: false,
        insertable: false,
        appendable: false
      },
      columns: [
        { type: 'gutter', gutterName: 'sequence' },
        {
          type: 'string',
          name: 'batchId',
          imex: { header: i18next.t('field.batch_id'), key: 'batchId', width: 15, type: 'string' },
          header: i18next.t('field.batch_id'),
          width: 180
        },
        {
          type: 'string',
          name: 'product|sku',
          header: i18next.t('field.sku'),
          imex: { header: i18next.t('field.sku'), key: 'product|sku', width: 75, type: 'string' },
          width: 150
        },
        {
          type: 'string',
          name: 'product|name',
          header: i18next.t('field.product'),
          imex: { header: i18next.t('field.product'), key: 'product|name', width: 75, type: 'string' },
          width: 320
        },
        {
          type: 'string',
          name: 'packingType',
          header: i18next.t('field.packing_type'),
          imex: { header: i18next.t('field.packing_type'), key: 'packingType', width: 25, type: 'string' },
          width: 160
        },
        {
          type: 'date',
          name: 'initialDate',
          header: i18next.t('field.initial_inbound_date'),
          imex: { header: i18next.t('field.initial_inbound_date'), key: 'initialDate', width: 25, type: 'date' },
          width: 180
        },
        {
          type: 'float',
          name: 'initialQty',
          header: i18next.t('field.initial_inbound_qty'),
          record: { align: 'center' },
          imex: { header: i18next.t('field.initial_inbound_qty'), key: 'initialQty', width: 25, type: 'string' },
          width: 140
        },
        {
          type: 'float',
          name: 'adjustmentQty',
          header: i18next.t('field.adjusted_qty'),
          record: { align: 'center' },
          imex: { header: i18next.t('field.adjusted_qty'), key: 'adjustmentQty', width: 25, type: 'string' },
          width: 140
        },
        {
          type: 'float',
          name: 'totalReturnQty',
          header: i18next.t('field.return_qty'),
          record: { align: 'center' },
          imex: { header: i18next.t('field.return_qty'), key: 'totalReturnQty', width: 25, type: 'string' },
          width: 140
        },
        {
          type: 'float',
          name: 'totalOutQty',
          record: { align: 'center' },
          header: i18next.t('field.outbound_qty'),
          imex: { header: i18next.t('field.outbound_qty'), key: 'totalOutQty', width: 25, type: 'string' },
          width: 140
        },
        {
          type: 'float',
          name: 'closingQty',
          record: { align: 'center' },
          header: i18next.t('field.closing_balance'),
          imex: { header: i18next.t('field.closing_balance'), key: 'closingQty', width: 25, type: 'string' },
          width: 140
        }
      ]
    }
  }

  async pageInitialized() {
    this._products = []
    this._partners = [...(await this._fetchPartners())]

    this._searchFields = this.searchFields
    this._config = this.reportConfig
  }

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

  async fetchHandler({ page, limit, sorters = [] }) {
    try {
      if(!this._validateDateRange()) return
      this._validate()
      let filter = this.searchForm.queryFilters.map(filter => {
        switch (filter.name) {
          case 'fromDate':
            let fromDt = new Date(filter.value)
            fromDt.setHours(0)
            filter.value = fromDt.toISOString()
            break
          case 'toDate':
            let toDt = new Date(filter.value)
            toDt.setHours(23, 59, 59)
            filter.value = toDt.toISOString()
            break
          default:
            break
        }
        return filter
      })
      const response = await client.query({
        query: gql`
          query elcclInventoryHistorySummaryReport($filters: [Filter], $pagination: Pagination, $sortings: [Sorting]) {
            elcclInventoryHistorySummaryReport(filters: $filters, pagination: $pagination, sortings: $sortings) {
              items {
                batchId
                packingType
                product {
                  id
                  sku
                  name
                  description
                }
                adjustmentQty
                closingQty
                totalInQty
                totalOutQty
                totalReturnQty
                initialQty
                initialDate
              }
              total
            }
          }
        `,
        variables: { filters: filter, pagination: { page, limit }, sortings: sorters }
      })
      return {
        total: response.data.elcclInventoryHistorySummaryReport.total,
        records: response.data.elcclInventoryHistorySummaryReport.items.map(item => flattenObject(item)) || []
      }
    } catch (e) {
      console.log(e)
    }
  }

  async _fetchPartners() {
    const filters = []
    const response = await client.query({
      query: gql`
        query partnersBizplaces($filters: [Filter!], $sortings: [Sorting!]) {
          partnersBizplaces(filters: $filters, sortings: $sortings) {
            items {
              id
              name
              description
            }
          }
        }
      `,
      variables: { filters, sortings: [{ name: 'name' }] }
    })

    if (!response.errors) {
      return response.data.partnersBizplaces.items
    }
  }

  _validate() {
    if (!this.searchForm.shadowRoot.querySelector('form').checkValidity())
      throw new Error(i18next.t('text.invalid_form_value'))
    if (!this._bizplaceSelector.value) throw new Error(i18next.t('text.company_does_not_selected'))
    if (!this._fromDateInput.value) throw new Error(i18next.t('text.from_date_is_empty'))
    if (!this._toDateInput.value) throw new Error(i18next.t('text.to_date_is_empty'))
  }

  _compareValues(key, order = 'asc') {
    return function innerSort(a, b) {
      if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
        return 0
      }

      const varA = typeof a[key] === 'string' ? a[key].toUpperCase() : a[key]
      const varB = typeof b[key] === 'string' ? b[key].toUpperCase() : b[key]

      let comparison = 0
      if (varA > varB) {
        comparison = 1
      } else if (varA < varB) {
        comparison = -1
      }
      return order === 'desc' ? comparison * -1 : comparison
    }
  }

  _modifyDateRange(e) {
    const fromDate = e.currentTarget.value

    if (this._toDateInput.value < fromDate) this._toDateInput.value = fromDate

    let min = new Date(fromDate)
    let today = new Date()
    today.setHours(0, 0, 0, 0)

    min = min.toISOString().split('T')[0]

    this._toDateInput.min = min
  }

  async _exportableData() {
    try {
      let header = [
        ...this.dataGrist._config.columns
          .filter(column => column.type !== 'gutter' && column.record !== undefined && column.imex !== undefined)
          .map(column => {
            return column.imex
          })
      ]

      header.filter(hdr => hdr.type == 'date').map(hdr => (hdr.type = 'string'))

      let data = (await this.fetchHandler({ page: 1, limit: 9999999 })).records.map(itm =>
        flattenObject({ ...itm, initialDate: new Date(parseInt(itm.initialDate)).toLocaleDateString() })
      )

      return { header, data }
    } catch (e) {
      this._showToast(e)
    }
  }

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

  _validateDateRange(){
    if(this._fromDateInput.value ==='' && this._toDateInput.value===''){
      this._showToast({ message: i18next.t('text.both_date_cannot_be_empty')})
      return false
    }else if(this._fromDateInput.value ===''){
      this._showToast({ message: i18next.t('text.from_date_cannot_be_empty')})
      return false
    }else if(this._toDateInput.value ===''){
      this._showToast({message :i18next.t('text.to_date_cannot_be_empty')})
      return false
    }else if(this._fromDateInput.value > this._toDateInput.value){
      this._showToast({ message: i18next.t('text.to_date_cannot_be_before_from_date') })
      return false
    }
    return true
  }

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

window.customElements.define('elccl-inventory-summary-report', ElcclInventorySummaryReport)
