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

const path = require('path')

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

  it('filters the grist by name with a space in front', function () {
    const data = this.warehouse.nameLetterCase
    cy.get('input[name="name"]').type(` ${data['name']}{enter}`, { force: true })
    cy.contains('span', 'total 0 records.', { timeout: 10000 }).should('exist')
    cy.dataNotExists(data)
  })

  it('filters the grist by name with a space at the back', function () {
    const data = this.warehouse.nameLetterCase
    cy.get('input[name="name"]').type(`${data['name']} {enter}`, { force: true })
    cy.contains('span', 'total 0 records.', { timeout: 10000 }).should('exist')
    cy.dataNotExists(data)
  })

  it('filters the grist by name with the same letter case', function () {
    const data = this.warehouse.nameLetterCase
    cy.get('input[name="name"]').type(`${data['name']}{enter}`)
    cy.waitOneRecord()
    cy.recordExists(data)
  })

  it('filters the grist by name with a different letter case', function () {
    const data = this.warehouse.nameLetterCase
    cy.get('input[name="name"]').type(`${data['name'].toLowerCase()}{enter}`)
    cy.waitOneRecord()
    cy.recordExists(data)
  })

  it('adds a warehouse', function () {
    const data = this.warehouse.add1
    cy.addWarehouse(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('adds duplicate [name, type], same letter case', function () {
    const data = this.warehouse.add2
    cy.addWarehouse(data)
    cy.clickSaveButton()
    cy.contains('span', /.*duplicate key value.*/, { timeout: 15000 }).should('be.visible')
    cy.searchGrist()
    cy.warehouseNotAdded(data)
  })

  it('adds duplicate [name, type], different letter case', function () {
    const data = this.warehouse.add3
    cy.addWarehouse(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('adds duplicate name, same letter case', function () {
    const data = this.warehouse.add4
    cy.addWarehouse(data)
    cy.clickSaveButton()
    cy.contains('span', /.*duplicate key value.*/, { timeout: 15000 }).should('be.visible')
    cy.searchGrist()
    cy.warehouseNotAdded(data)
  })

  it('adds duplicate name, different letter case', function () {
    const data = this.warehouse.add5
    cy.addWarehouse(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.recordExists(data)
  })

  it('adds multiple warehouses', function () {
    const warehouses = this.warehouse.addMultiple
    for (const warehouse of warehouses) {
      cy.addWarehouse(warehouse)
    }
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    for (const warehouse of warehouses) {
      cy.recordExists(warehouse)
    }
  })

  it('deletes a warehouse', function () {
    const data = this.warehouse.delete1
    cy.checkWarehouseRecords([data.name])
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.contains('span', 'data deleted successfully').should('be.visible')
    cy.contains('span', data.name).should('not.exist')
  })

  it('deletes multiple warehouses', function () {
    const data = this.warehouse.deleteMultiple
    cy.checkWarehouseRecords(data)
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.contains('span', 'data deleted successfully').should('be.visible')
    cy.contains('span', data.name).should('not.exist')
  })

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

  it('imports warehouses from an xlsx file', function () {
    const warehouses = this.warehouse.importNew
    cy.importGrist('master-data/warehouseImport.xlsx')
    cy.importAllPopup()
    for (const warehouse of warehouses) {
      cy.recordExists(warehouse)
    }
  })

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