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

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

describe('User subscribe to a plan', () => { 
  beforeEach(() => {
    // Log in to the application or navigate to the account page
    // where the change email feature is available
    const currentUser = makeFakeUser({
      email: 'sam@example.com'
    });
    cy.stubAuthenticate({ ...currentUser });
    cy.stubGonSubscriptionCodes();
    
    
    cy.intercept('GET', /\S+\/get_subscription/, {
      statusCode: 200,
      body:  {
        past_due: false,
        has_billing_info: true,
        plan_code: 'jamsubsilver',
        desired_plan_code: 'jamsubgold',
        admin_overide_plan_code: null,
        admin_overide_ends_at: null,
        in_trial: false,
        trial_ends_at: null,
        subscription: {

        },
        subscription_rules: {
          remaining_monthly_play_time: null
        }
      }
    }).as('getSubscription');

    cy.intercept('GET', /\S+\/get_subscription/, {
      statusCode: 200,
      body:  {
        past_due: false,
        has_billing_info: true,
        plan_code: '',
        desired_plan_code: '',
        admin_overide_plan_code: null,
        admin_overide_ends_at: null,
        in_trial: false,
        trial_ends_at: null,
        subscription: {

        },
        subscription_rules: {
          remaining_monthly_play_time: null
        }
      }
    }).as('getSubscriptionWithNoPlan');
   
  });

  it('should display the current plan', () => {
    cy.visit('/account/subscription');
    cy.wait('@getSubscription')
    // Assert that the current plan is displayed
    cy.get('[data-testid=changeSubscriptionForm]').within($form => {
      cy.get('label').contains('Subscription Plan');
      cy.get('.select-plan__single-value').should('contain', 'Gold ($9.99/month)');
      cy.get('input[type=submit]').should('have.value', 'Save Plan').should('be.disabled');
    });

    cy.get('[data-testid=playtime]').within($playtime => {
      cy.get('label').contains('You have unlimited play time');
    });

    cy.get('[data-testid=subscription-explanation]').within($expl => {
      cy.get('.alert').contains('You are currently on the Silver (monthly) level, thank you!');
    });
  });

  it.only('should display the current plan when no plan', () => {
    cy.visit('/account/subscription');
    cy.wait('@getSubscriptionWithNoPlan')
    // Assert that the current plan is displayed
    cy.get('[data-testid=changeSubscriptionForm]').within($form => {
      cy.get('label').contains('Subscription Plan');
      cy.get('.select-plan__single-value').should('contain', 'Free ($0.00/month)');
      cy.get('input[type=submit]').should('have.value', 'Save Plan').should('be.disabled');
    });

    cy.get('[data-testid=playtime]').within($playtime => {
      cy.get('label').contains('You have unlimited play time.');
    });

    cy.get('[data-testid=subscription-explanation]').within($expl => {
      cy.get('.alert').contains('You are currently on the Free (monthly) plan.');
    });
  });

  it('change plan', () => {
    cy.visit('/account/subscription');
    cy.wait('@getSubscription')
    cy.get('[data-testid=changeSubscriptionForm]').within($form => {
      cy.get('.select-plan__control').click();
      cy.get('.select-plan__option').contains('Platinum ($19.99/month)').click();
      cy.get('input[type=submit]').should('be.enabled').click();
    });
    
    const text = 'You have selected the PLATINUM MONTHLY PLAN and will be charged US$19.99'
    cy.contains('.modal-body', text).should('be.visible');
    cy.get('.modal-content').find('.modal-footer').contains('OK').click();
    
    cy.get('.modal-body').should('not.be.visible');
    // cy.get('[data-testid=subscription-explanation]').within($expl => {
    //   cy.get('.alert').contains('You are currently on the Silver (monthly) level, thank you! And your plan and billing will switch to the Platinum (monthly) level on the next billing cycle.');
    // });
    cy.get('input[type=submit]').should('be.disabled')
  });


 });