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

const c = require('croppie');

describe('TicketSale', () => {
    beforeEach(() => {
        cy.login('developer');
        cy.viewport(1920, 1080);
        cy.visit('/#/ticket/31/sale');
    });

    const firstRow = 'tbody > :nth-child(1)';

    const selectFirstRow = () => {
        cy.waitForElement(firstRow);
        cy.get(firstRow).find('.q-checkbox__inner').click();
    };

    it('it should add item to basket', () => {
        cy.window().then((win) => {
            cy.stub(win, 'open').as('windowOpen');
        });
        cy.dataCy('ticketSaleAddToBasketBtn').should('exist');
        cy.dataCy('ticketSaleAddToBasketBtn').click();
        cy.get('@windowOpen').should('be.calledWithMatch', /\/order\/\d+\/catalog/);
    });

    it('should send SMS', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.waitForElement('[data-cy="sendShortageSMSItem"]');
        cy.dataCy('sendShortageSMSItem').should('exist');
        cy.dataCy('sendShortageSMSItem').click();
        cy.dataCy('vnSmsDialog').should('exist');
        cy.dataCy('sendSmsBtn').click();
        cy.checkNotification('SMS sent');
    });

    it('should recalculate price when "Recalculate price" is clicked', () => {
        cy.intercept('POST', '**/recalculatePrice').as('recalculatePrice');
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.waitForElement('[data-cy="recalculatePriceItem"]');
        cy.dataCy('recalculatePriceItem').should('exist');
        cy.dataCy('recalculatePriceItem').click();
        cy.wait('@recalculatePrice').its('response.statusCode').should('eq', 200);
        cy.checkNotification('Data saved');
    });

    it('should update discount when "Update discount" is clicked', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.waitForElement('[data-cy="updateDiscountItem"]');
        cy.dataCy('updateDiscountItem').should('exist');
        cy.dataCy('updateDiscountItem').click();
        cy.waitForElement('[data-cy="ticketSaleDiscountInput"]');
        cy.dataCy('ticketSaleDiscountInput').find('input').focus();
        cy.dataCy('ticketSaleDiscountInput').find('input').type('10');
        cy.dataCy('saveManaBtn').click();
        cy.waitForElement('.q-notification__message');
        cy.checkNotification('Data saved');
    });

    it('adds claim', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.dataCy('createClaimItem').click();
        cy.dataCy('VnConfirm_confirm').click();
        cy.url().should('match', /\/claim\/\d+\/basic-data/);
        // Delete created claim to avoid cluttering the database
        cy.dataCy('descriptor-more-opts').click();
        cy.dataCy('deleteClaim').click();
        cy.dataCy('VnConfirm_confirm').click();
        cy.checkNotification('Data deleted');
    });

    it('marks row as reserved', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.waitForElement('[data-cy="markAsReservedItem"]');
        cy.dataCy('markAsReservedItem').click();
        cy.dataCy('ticketSaleReservedIcon').should('exist');
    });

    it('unmarks row as reserved', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.waitForElement('[data-cy="unmarkAsReservedItem"]');
        cy.dataCy('unmarkAsReservedItem').click();
        cy.dataCy('ticketSaleReservedIcon').should('not.exist');
    });

    it('refunds row with warehouse', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.dataCy('ticketSaleRefundItem').click();
        cy.dataCy('ticketSaleRefundWithWarehouse').click();
        cy.checkNotification('The following refund ticket have been created');
    });

    it('refunds row without warehouse', () => {
        selectFirstRow();
        cy.dataCy('ticketSaleMoreActionsDropdown').click();
        cy.dataCy('ticketSaleRefundItem').click();
        cy.dataCy('ticketSaleRefundWithoutWarehouse').click();
        cy.checkNotification('The following refund ticket have been created');
    });

    it('transfers ticket', () => {
        cy.visit('/#/ticket/32/sale');
        selectFirstRow();
        cy.dataCy('ticketSaleTransferBtn').click();
        cy.dataCy('ticketTransferPopup').should('exist');
        cy.dataCy('ticketTransferNewTicketBtn').click();
        // existen 3 elementos "tbody" necesito checkear que el segundo elemento tbody tenga una row sola
        cy.get('tbody').eq(1).find('tr').should('have.length', 1);
        selectFirstRow();
        cy.dataCy('ticketSaleTransferBtn').click();
        cy.dataCy('ticketTransferPopup').should('exist');
        cy.dataCy('ticketTransferDestinationTicketInput').find('input').focus();
        cy.dataCy('ticketTransferDestinationTicketInput').find('input').type('32');
        cy.dataCy('ticketTransferTransferBtn').click();
        // checkear que la url contenga /ticket/1000002/sale
        cy.url().should('match', /\/ticket\/32\/sale/);
    });

    it('should redirect to ticket logs', () => {
        cy.get(firstRow).find('.q-btn:last').click();
        cy.url().should('match', /\/ticket\/31\/log/);
    });
});
