/// <reference types="cypress" />

const path = require('path')

describe('pallet', () => {
  beforeEach(() => {
    cy.fixture('master-data/pallet').as('pallet')
    cy.visit('/domain/el-cold-chain-warehouse/pallets')
    cy.waitGristLoad()
  })

  it('adds a pallet [ref no., owner, holder, status]', function () {
    const data = this.pallet.add1
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('data updated successfully')
    cy.palletExists(data)
  })

  it('adds a pallet [ref no., owner, holder]', function () {
    const data = this.pallet.add2
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('null value in column "status"')
    cy.searchGrist()
    cy.palletNotExists(data)
  })

  it('adds a pallet [ref no., owner, status]', function () {
    const data = this.pallet.add3
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('holder field is empty')
    cy.searchGrist()
    cy.palletNotExists(data)
  })

  it('adds a pallet [ref no., holder, status]', function () {
    const data = this.pallet.add4
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('owner field is empty')
    cy.searchGrist()
    cy.palletNotExists(data)
  })

  it('adds a pallet [ref no., status]', function () {
    const data = this.pallet.add5
    cy.addPallet(this.pallet['5'])
    cy.clickSaveButton()
    cy.toastContains('owner field is empty')
    cy.searchGrist()
    cy.palletNotExists(this.pallet['5']['ref no.'])
  })

  it('adds a pallet [ref no., owner]', function () {
    const data = this.pallet.add6
    cy.addPallet(this.pallet['6'])
    cy.clickSaveButton()
    cy.toastContains('holder field is empty')
    cy.searchGrist()
    cy.palletNotExists(this.pallet['6'])
  })

  it('adds a pallet [owner, holder, status]', function () {
    const data = this.pallet.add7
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('null value in column "name"')
    cy.searchGrist()
    cy.palletNotExists(data)
  })

  it('adds a pallet [holder, status]', function () {
    const data = this.pallet.add8
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('null value in column "name"')
    cy.searchGrist()
    cy.palletNotExists(data)
  })

  it('adds a duplicate pallet ref no. with a different letter case', function () {
    const data = this.pallet.add9
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('data updated successfully')
    cy.palletExists(data)
  })

  it('adds a duplicate pallet ref no. with the same letter case', function () {
    const data = this.pallet.add10
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('duplicate key value')
  })

  it('deletes a pallet', function () {
    const data = this.pallet.delete1
    cy.checkGristRecords([data])
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.toastContains('data deleted successfully')
    cy.palletNotExists(data)
  })

  it('deletes 3 pallets', function () {
    const pallets = this.pallet.deleteMultiple
    cy.checkGristRecords(pallets)
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.toastContains('data deleted successfully')
    for (const pallet of pallets) {
      cy.palletNotExists(pallet)
    }
  })

  it('adds a deleted pallet', function () {
    const data = this.pallet.addDeleted
    cy.addPallet(data)
    cy.clickSaveButton()
    cy.toastContains('duplicate key value')
    cy.searchGrist()
    cy.palletNotExists(data)
  })

  it('filters the grist by ref no with the same letter case', function () {
    const data = this.pallet.filter1
    cy.get('input[name="name"]').type(`${data['ref no.']}{enter}`)
    cy.palletExists(data)
    cy.get('data-grid-footer').within(() => {
      cy.contains('span', 'total 1 records.', { timeout: 10000 }).should('exist')
    })
  })

  it('filters the grist by ref no with a different letter case', function () {
    const data = this.pallet.filter1
    cy.get('input[name="name"]').type(`${data['ref no.'].toLowerCase()}{enter}`)
    cy.palletExists(data)
    cy.get('data-grid-footer').within(() => {
      cy.contains('span', 'total 1 records.', { timeout: 10000 }).should('exist')
    })
  })

  it('filters the grist by status', function () {
    const data = this.pallet.filter2
    cy.get('select[name="status"]').select(data['status'], { force: true }).type('{enter}')
    cy.palletExists(data)
    cy.get('data-grid-footer').within(() => {
      cy.contains('span', 'total 1 records.', { timeout: 10000 }).should('exist')
    })
  })

  it('downloads the json, csv, xlsx, xls files', () => {
    const fileTypes = this.globals.fileTypes
    cy.exportGrist(path, fileTypes, 'pallet')
  })

  it('imports pallets from an xlsx file', function () {
    const pallets = this.pallet.importNew
    cy.importGrist('master-data/palletImport.xlsx')
    cy.importAllPopup()
    for (const pallet of pallets) {
      cy.palletExists(pallet)
    }
  })

  it('imports already deleted pallets from an xlsx file', function () {
    cy.importGrist('master-data/palletDeletedImport.xlsx')
    cy.importAllPopup()
    cy.log('false positive')
    // pending fix for import function
    // code for next assertion steps goes here
    // will return false positive!
  })
})
