import * as moment from 'moment';

describe('Онлайн табло: Прилет', () => {
    const arrivalCity = {
        name: 'Анапа',
        code: 'AAQ',
        latitude: 55.7558,
        longitude: 37.62,
    };
    
    const today = moment().format('DD.MM.YYYY');

    const expectedUrlDateTime = `${moment().format('DDMMYYYY')}-0000-2400`;

    beforeEach(() => {
        cy.intercept('GET', '**api/flights/v1.1/ru/board**').as('getFlights');
        cy.forbidGeolocation();
        cy.visit('/');
    });

    const assertSearchResults = () => {
        cy.getByTestId('loader').should('be.visible');
        cy.wait('@getFlights').then(() => {
            cy.getByTestId('board-search-result').should('be.visible');
            cy.getByTestId('flight-result').should('have.length.at.least', 1);
        });
    };

    it(`Должен искать рейсы (${arrivalCity.name} - ручной ввод), открывать корректный URL и детали рейса`, () => {
        cy.getByTestId('city-autocomplete-input').type(`${arrivalCity.name}`).type('{enter}');
        cy.getByTestId('calendar-input').type(today).type('{enter}');
        
        cy.getByTestId('city-code').contains(`${arrivalCity.code}`).should('be.visible');

        cy.getByTestId('arrival-search-button').click();
        cy.url().should('include', `ru-ru/onlineboard/arrival/${arrivalCity.code}/${expectedUrlDateTime}`);

        assertSearchResults();

        cy.getByTestId('flight-result')
            .first()
            .getByTestId('flight-carrier-number')
            .should('be.visible')
            .getByTestId('flight-company-logo')
            .should('be.visible');

        cy.getByTestId('flight-details-button').click();
        cy.getByTestId('flight-details-number').should('be.visible');
        cy.getByTestId('flight-company-logo').should('be.visible');
    });

    it(`Должен искать рейсы (${arrivalCity.name} - выбор из списка)`, () => {
        cy.getByTestId('calendar-input').type(today).type('{enter}');
        cy.getByTestId('autocomplete-popup-button').click();
        cy.getByTestId(`city-name-cell-${arrivalCity.name}`).click();

        cy.getByTestId('city-code').contains(`${arrivalCity.code}`).should('be.visible');

        cy.getByTestId('arrival-search-button').click();

        assertSearchResults();
    });
});
