/// <reference types="cypress" />
describe('OrderCatalog', () => {
    beforeEach(() => {
        cy.login('developer');
        cy.viewport(1920, 720);
        cy.visit('/#/order/8/catalog');
    });

    const checkCustomFilterTag = (filterName = 'Plant') => {
        cy.dataCy('catalogFilterCustomTag').should('exist');
        cy.dataCy('catalogFilterCustomTag').contains(filterName);
    };

    const checkFilterTag = (filterName = 'Plant') => {
        cy.dataCy('vnFilterPanelChip').should('exist');
        cy.dataCy('vnFilterPanelChip').contains(filterName);
    };

    const selectCategory = (categoryIndex = 1, categoryName = 'Plant') => {
        cy.get(
            `div.q-page-container div:nth-of-type(${categoryIndex}) > [data-cy='catalogFilterCategory']`
        ).should('exist');
        cy.get(
            `div.q-page-container div:nth-of-type(${categoryIndex}) > [data-cy='catalogFilterCategory']`
        ).click();
        checkCustomFilterTag(categoryName);
    };

    const searchByCustomTagInput = (option) => {
        cy.dataCy('catalogFilterValueInput').find('input').last().focus();
        cy.dataCy('catalogFilterValueInput').find('input').last().type(option);
        cy.dataCy('catalogFilterValueInput').find('input').last().type('{enter}');
        checkCustomFilterTag(option);
    };

    const selectTypeFilter = (option) => {
        cy.selectOption(
            'div.q-page-container div.list > div:nth-of-type(2) div:nth-of-type(3)',
            option
        );
        checkFilterTag(option);
    };

    it('Shows empty state', () => {
        cy.dataCy('orderCatalogPage').should('exist');
        cy.dataCy('orderCatalogPage').contains('No data to display');
    });

    it('filter by category', () => {
        selectCategory();
        cy.dataCy('orderCatalogItem').should('exist');
    });

    it('filters by type', () => {
        selectCategory();
        selectTypeFilter('Anthurium');
    });

    it('filters by custom value select', () => {
        selectCategory();
        searchByCustomTagInput('Silver');
    });

    it('filters by custom value dialog', () => {
        Cypress.on('uncaught:exception', (err) => {
            if (err.message.includes('canceled')) {
                return false;
            }
        });
        selectCategory();
        cy.dataCy('catalogFilterValueDialogBtn').should('exist');
        cy.dataCy('catalogFilterValueDialogBtn').last().click();
        cy.dataCy('catalogFilterValueDialogTagSelect').should('exist');
        cy.selectOption("[data-cy='catalogFilterValueDialogTagSelect']", 'Tallos');
        cy.dataCy('catalogFilterValueDialogValueInput').find('input').focus();
        cy.dataCy('catalogFilterValueDialogValueInput').find('input').type('2');
        cy.dataCy('catalogFilterValueDialogValueInput').find('input').type('{enter}');
        checkCustomFilterTag('2');
    });

    it('removes a secondary tag', () => {
        selectCategory();
        selectTypeFilter('Anthurium');
        cy.dataCy('vnFilterPanelChip').should('exist');
        cy.get(
            "div.q-page-container [data-cy='vnFilterPanelChip'] > i.q-chip__icon--remove"
        )
            .contains('cancel')
            .should('exist');
        cy.get(
            "div.q-page-container [data-cy='vnFilterPanelChip'] > i.q-chip__icon--remove"
        )
            .contains('cancel')
            .click();
        cy.dataCy('vnFilterPanelChip').should('not.exist');
    });

    it('Removes category tag', () => {
        selectCategory();
        cy.get(
            "div.q-page-container [data-cy='catalogFilterCustomTag'] > i.q-chip__icon--remove"
        )
            .contains('cancel')
            .should('exist');
        cy.get(
            "div.q-page-container [data-cy='catalogFilterCustomTag'] > i.q-chip__icon--remove"
        )
            .contains('cancel')
            .click();
        cy.dataCy('catalogFilterCustomTag').should('not.exist');
    });
});