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

import makeFakeUser from '../../factories/user';

describe('JamTracks Page', () => {
  beforeEach(() => {
    const currentUser = makeFakeUser();
    cy.stubAuthenticate({ id: currentUser.id });
    cy.visit('/jamtracks');
  });

  it('should display the JamTracks page', () => {
    cy.get('.card-header h5').should('contain', 'Find JamTracks');
  });

  it('should display the download link', () => {
    cy.get('[data-testid=download-pdf]')
      .invoke('attr', 'href')
      .should('contain', 'https://s3.amazonaws.com/jamkazam-public/public/lists/all-jamkazam-jamtracks.pdf');
  });

  it('should display the JamTracks search bar', () => {
    cy.get('input[type="search"]').should('exist');
  });

  describe('search', () => {
    beforeEach(() => {
      //http://www.jamkazam.local:3000/api/jamtracks/autocomplete?match=ac+&limit=5
      // cy.intercept('GET', /S+\/jamtracks\/autocomplete\?match=ac\S+/, {
      //   body: [{ artists: [{ original_artist: 'AC DC' }], songs: [] }]
      // }).as('getJamTracksAutoComplete');
      cy.intercept('GET', /\S+\/jamtracks\?per_page=10\&page=1\&\S+/, { fixture: 'jamtracks_page1' }).as('getJamTracks_page1');
      cy.intercept('GET', /\S+\/jamtracks\?per_page=10\&page=2\&\S+/, { fixture: 'jamtracks_page2' }).as('getJamTracks_page2');
      cy.intercept('GET', /\S+\/jamtracks\?per_page=10\&page=3\&\S+/, { fixture: 'jamtracks_page3' }).as('getJamTracks_page3');
      cy.intercept('GET', /\S+\/shopping_carts/,  {
        statusCode: 200,
        body: []
      }).as('getShoppingCart');
      cy.intercept('POST', /\S+\/shopping_carts\/add_jamtrack/, {
        statusCode: 200,
        body: { id: 1,
          product_info: {
            total_price: 9.99
         }
        }
      }).as('addJamTrackToCart');
      cy.intercept('GET', /\S+\/jamtracks\/purchased/,  {
        statusCode: 200,
        body: []
      }).as('getPurchasedJamTracks');
    })

    it('should display the JamTracks', () => {
      cy.get('input[type="search"]').type('ba{enter}');
      cy.wait('@getJamTracks_page1');
      cy.contains('Search Results: JamTracks including "ba"');
      cy.get('[data-testid=jamtracks-table] tbody tr:first .track-name-col').should('contain', 'Back in Black by AC DC');
      cy.get('[data-testid=jamtracks-table] tbody tr:first .track-tracks-col .jamtrack-track:visible').should('have.length', 6);
      cy.get('[data-testid=jamtracks-table] tbody tr:first .track-tracks-col').contains('Show all tracks').click();
      cy.get('[data-testid=jamtracks-table] tbody tr:first .track-tracks-col .jamtrack-track:visible').should('have.length', 10);
      cy.get('[data-testid=jamtracks-table] tbody tr:first .track-tracks-col').contains('Show fewer tracks').click();
      cy.get('[data-testid=jamtracks-table] tbody tr:first .track-tracks-col .jamtrack-track:visible').should('have.length', 6);

      //load more
      cy.get('button').contains('Load More').click();
      cy.wait('@getJamTracks_page2');

      //load more
      cy.get('button').contains('Load More').click();
      cy.wait('@getJamTracks_page3');

      cy.get('[data-testid=jamtracks-table] tbody tr').should('have.length', 30);

      //no more pages
      cy.get('[data-testid=moreBtn]').should('not.exist');
    });

    it('let user to purchase a JamTrack', () => {
      cy.get('input[type="search"]').type('ba{enter}');
      cy.wait('@getJamTracks_page1');
      cy.wait('@getShoppingCart');
      cy.get('[data-testid=jamtracks-table] tbody tr').eq(2).find('.purchase-button-col button').should('contain', 'Add to Cart').click();
      cy.wait('@getShoppingCart');
      cy.wait('@addJamTrackToCart');
      cy.location('pathname').should('eq', '/shopping-cart')
    });



    describe.only('mobile', () => { 
      beforeEach(() => {
        cy.viewport('iphone-6');
      });

      it('should display the JamTracks', () => {
        cy.get('input[type="search"]').type('ba{enter}');
        cy.wait('@getJamTracks_page1');
        cy.contains('Search Results: JamTracks including "ba"');
      });
    });




    
  });




});
