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

const path = require('path')

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

  it('filters the grist by registration number with the same letter case', function () {
    const data = this.vehicle.regNumberLetterCase
    cy.get('input[name="regNumber"]').type(`${data.regNumber}{enter}`)
    cy.waitOneRecord()
    cy.vehicleExists(data)
  })

  it('filters the grist by registration number with a different letter case', function () {
    const data = this.vehicle.regNumberLetterCase
    cy.get('input[name="regNumber"]').type(`${data.regNumber.toLowerCase()}{enter}`)
    cy.waitOneRecord()
    cy.vehicleExists(data)
  })

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

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

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

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

  it('adds transport vehicle', function () {
    const data = this.vehicle.add1
    cy.addTransportVehicle(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.vehicleExists(data)
  })

  it('adds duplicate [registration number, description, size], different letter case', function () {
    const data = this.vehicle.add2
    cy.addTransportVehicle(data)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.vehicleExists(data)
  })

  it('adds duplicate [registration number, description, size], same letter case', function () {
    const data = this.vehicle.add3
    cy.addTransportVehicle(data)
    cy.clickSaveButton()
    cy.contains('span', 'duplicate key value violates unique constraint "ix_transport-vehicle_0"').should('be.visible')
  })

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

  it('deletes 3 transport drivers', function () {
    const names = this.vehicle.deleteMultiple
    cy.checkGristRecords(names)
    cy.clickDeleteButton()
    cy.clickPopupButton('delete')
    cy.contains('span', 'data deleted successfully').should('be.visible')
    for (const name of names) {
      cy.contains('span', name).should('not.exist')
    }
  })

  it('edits a name of transport vehicle', function () {
    const data = this.vehicle.edit1
    cy.editField(data.oldVal, data.newVal)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.contains('span', data.newVal).should('exist')
  })

  it('edits a description of transport vehicle', function () {
    const data = this.vehicle.edit2
    cy.editField(data.oldVal, data.newVal)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.contains('span', data.newVal).should('exist')
  })

  it('edits a size of transport vehicle', function () {
    const data = this.vehicle.edit3
    cy.editField(data.oldVal, data.newVal)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.contains('span', data.newVal).should('exist')
  })

  it('edits a status of transport vehicle', function () {
    const data = this.vehicle.edit4
    cy.editField(data.oldVal, data.newVal)
    cy.clickSaveButton()
    cy.contains('span', 'data updated successfully').should('be.visible')
    cy.contains('span', data.newVal).should('exist')
  })

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

  it('imports new vehicles from an xlsx file', function () {
    const data = this.vehicle.importNew
    cy.importGrist('master-data/transportVehicleImport.xlsx')
    for (const i of data) {
      cy.recordExists(i)
    }
  })

  it('imports deleted vehicles from an xlsx file', function () {
    const data = this.vehicle.importDeleted
    cy.importGrist('master-data/transportVehicleDeletedImport')
    cy.importAllPopup()
    for (const i of data) {
      cy.recordExists(i)
    }
  })

  it('imports vehicles from a partial [name, size] xlsx file', function () {
    cy.importGrist('master-data/transportVehicleNameSizeImport')
    cy.importAllPopup()
    // false positive
    // need to see actual error and check for it
  })

  it('imports vehicles from a partial [name, status] xlsx file', function () {
    cy.importGrist('master-data/transportVehicleNameStatusImport')
    cy.importAllPopup()
    // false positive
    // need to see actual error and check for it
  })
})
