describe('WagonTypeCreate', () => {
    beforeEach(() => {
        cy.viewport(1920, 1080);
        cy.login('developer');
        cy.visit('/#/wagon/type/create');
    });

    function chooseColor(color) {
        cy.get('div.shelving-down').eq(1).click();
        cy.get('div.q-color-picker__cube').eq(color).click();
        cy.get('div.q-card__section').find('button').click();
    }

    function addTray(position) {
        cy.get('div.action-button').last().find('button').click();
        cy.focused().type(position);
        cy.focused().blur();
    }

    it('should create and delete a new wagon type', () => {
        cy.get('input').first().type('Example for testing');
        cy.get('div.q-checkbox__bg').click();
        chooseColor(1);

        // Insert invalid position (not minimal height)
        addTray(20);
        cy.get('div[role="alert"]').should('exist');
        chooseColor(2);
        addTray(150);
        chooseColor(3);
        addTray(100);

        // Insert invalid position (max height reached)
        addTray(210);
        cy.get('div[role="alert"]').should('exist');

        // Save
        cy.get('button[type="submit"]').click();

        // Check data has been saved successfully
        cy.get(':nth-child(1) > :nth-child(1) > .justify-between > .flex > .title')
            .contains('Example for testing')
            .click();
        cy.get('input').first().should('have.value', 'Example for testing');
        cy.get('div.wagon-tray').should('have.length', 4);
        cy.get('div.position').eq(0).find('input').should('have.value', '150');
        cy.get('div.position').eq(1).find('input').should('have.value', '100');
        cy.get('div.position').eq(2).find('input').should('have.value', '50');

        // Delete wagon type created
        cy.go('back');
        cy.get(
            ':nth-child(2) > :nth-child(1) > .card-list-body > .actions > .q-btn--standard'
        ).click();
    });
});
