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

describe('Top Navigation', () => {
  const showSubscribeToUpdates = () => {
    cy.contains('Keep JamKazam Improving').should('exist');
    cy.contains('Subscribe').should('exist');
  };

  const showProfileDropdown = () => {
    cy.get('[data-testid=navbarTopProfileDropdown]').should('exist');
    cy.contains('Sign Out').should('exist');
  };

  describe('when user has not logged in', () => {
    beforeEach(() => {
      cy.stubUnauthenticate();
    });

    it('shows homepage', () => {
      cy.visit('/');
      cy.wait('@getAppFeatures');
      cy.contains('Sign in').should('exist');
    });

    it('not allowed to protected page', () => {
      cy.visit('/friends');
      cy.wait('@getAppFeatures');
      cy.url().should('include', '/authentication/basic/login');
      cy.contains('Sign in');
      cy.get('button').should('have.text', 'Sign in');
      cy.get('[data-testid=navbarTopProfileDropdown]').should('not.exist');
    });
  });

  describe('when user has logged in', () => {
    beforeEach(() => {
      cy.stubAuthenticate();
      cy.visit('/');
      cy.wait('@getAppFeatures');
    });

    it('shows user dropdown', () => {
      showSubscribeToUpdates();
      showProfileDropdown();
    });

    it('sign out', () => {
      cy.get('[data-testid=navbarTopProfileDropdown]')
        .click();
      cy.stubUnauthenticate();
      cy.get('[data-testid=navbarTopProfileDropdown]')
        .contains('Sign Out')
        .click();
      cy.get('[data-testid=navbarTopProfileDropdown]').should('not.exist');
      cy.contains('Sign in');
    });
  });

  describe('header notifications', () => {
    beforeEach(() => {
      cy.stubAuthenticate();
      cy.intercept('GET', /\S+\/my_notifications/, { fixture: 'notifications' });
      cy.intercept('GET', /\S+\/profile\S+/, { fixture: 'person' });
      cy.visit('/');
      cy.wait('@getAppFeatures');
    });

    it('shows notifications', () => {
      cy.get('[data-testid=notificationDropdown]').should('not.be.visible');
      cy.get('.bell-icon').click();

      cy.get('[data-testid=notificationDropdown]').should('be.visible');
      cy.get('[data-testid=notificationDropdown] .list-group-item').should('have.length', 3);
      cy.get('[data-testid=notificationDropdown]')
        .contains('View all')
        .click(); //view all notifications
      cy.url().should('include', '/notifications');
    });
  });

  describe.skip('locale switch', () => {
    beforeEach(() => {
      cy.stubAuthenticate();
      cy.visit('/');
      cy.wait('@getAppFeatures');
    });

    it('translate', () => {
      cy.get('.card-header').contains('Home');
      cy.get('[data-testid=langSwitch]')
        .contains('ES')
        .click();
      cy.get('.card-header').contains('Página de inicio');
      cy.get('.card-header').should('not.contain', 'Home');
      cy.get('[data-testid=langSwitch]')
        .contains('EN')
        .click();
      cy.get('.card-header').contains('Home');
      cy.get('.card-header').should('not.contain', 'Página de inicio');
    });
  });
  
});

describe('Side Navigation', () => {
  describe('backend returns empty set of app features', () => {
    beforeEach(() => {
      cy.stubAuthenticate();
      cy.viewport('macbook-13');
    });

    it('shows all menu items', () => {
      cy.visit('/');
      cy.wait('@getAppFeatures');
      cy.get('[data-testid=verticalNavigation]').contains('Home');
      cy.get('[data-testid=verticalNavigation]').contains('Sessions');
      cy.get('[data-testid=verticalNavigation]').contains('Create Session');
      cy.get('[data-testid=verticalNavigation]').contains('Friends');
    });

    it('toggles only one menu on click', () => {
      cy.visit('/');
      cy.wait('@getAppFeatures');
      cy.get('[data-testid=verticalNavigation]')
        .contains('Sessions')
        .click();
      cy.get('[data-testid=verticalNavigation]')
        .contains('Create Session')
        .should('not.visible');
      cy.get('[data-testid=verticalNavigation]')
        .contains('Browse Sessions')
        .should('not.visible');
      cy.get('[data-testid=verticalNavigation]')
        .contains('Session History')
        .should('not.visible');
      //the Friends menu is not toggled
      cy.get('[data-testid=verticalNavigation]').contains('Friends');
      cy.get('[data-testid=verticalNavigation]').contains('My Friends');
    });
   
  });

  describe('backend returns app features', () => { 
    beforeEach(() => {
      cy.stubAuthenticate();
      cy.viewport('macbook-13');
      cy.intercept('GET', /\S+\/app_features/, {
        statusCode: 200,
        body: [
          {
            handle: '/sessions',
            is_enabled: true,
            feature_type: 'page',
            env: 'development'
          },
          {
            handle: '/sessions/new',
            is_enabled: false,
            feature_type: 'page',
            env: 'development'
          },
          {
            handle: '/sessions/history',
            is_enabled: true,
            feature_type: 'page',
            env: 'production'
          },
          {
            handle: '/friends',
            is_enabled: true,
            feature_type: 'page',
            env: 'development'
          },
          {
            handle: '/friends/my',
            is_enabled: true,
            feature_type: 'page',
            env: 'development'
          }
        ]
      }).as('getAppFeatures');
    });

    it('shows only enabled menu items', () => {
      cy.visit('/');
      cy.wait('@getAppFeatures');
      cy.get('[data-testid=verticalNavigation]').contains('Friends');
      cy.get('[data-testid=verticalNavigation]').contains('Sessions');
      cy.get('[data-testid=verticalNavigation]').should('not.include.text', 'Create Session')
      cy.get('[data-testid=verticalNavigation]').should('not.include.text', 'Session History')
    });

    
   })
});
