import '@testing-library/jest-dom/extend-expect';
import { cleanup } from '@testing-library/react';
import { prepareSportsData, addSportsDataToPage, addExtraFieldsToShowcaseEvents } from './sports';
import getSportsData from '../../../tests/factories/sportsData/sportsData';
import getSportsDataPagesList from '../../../tests/factories/sportsData/sportsDataPage';

describe('Prepare Sports Data', () => {
  const currentFullYear = new Date().getFullYear();
  const prevFullYear = parseInt(currentFullYear) - 1;
  const nextFullYear = parseInt(currentFullYear) + 1;

  let data = null;

  beforeEach(() => {
    data = getSportsData();
  });

  test('remove past events from tournaments', () => {
    // before
    expect(data.tournaments['122'].events.length).toEqual(2);
    expect(data.tournaments['122'].events.indexOf(30185)).toEqual(0);

    // after
    const result = prepareSportsData(data);
    expect(result.tournaments['122'].events.length).toEqual(1);
    expect(result.tournaments['122'].events.indexOf(30185)).toEqual(-1);
  });

  test('remove past events from teams', () => {
    // before
    expect(data.teams['3784'].events.length).toEqual(3);
    expect(data.teams['3784'].events.indexOf(30185)).toEqual(1);

    // after
    const result = prepareSportsData(data);
    expect(result.teams['3784'].events.length).toEqual(1);
    expect(result.teams['3784'].events.indexOf(30185)).toEqual(-1);
  });

  test('remove past events from schedule', () => {
    // before
    expect(Object.values(data.schedule.soccer).length).toEqual(2);
    expect(Object.keys(data.schedule.soccer).indexOf(`${prevFullYear}-09-10`)).toEqual(0);
    expect(Object.keys(data.schedule.soccer).indexOf(`${nextFullYear}-09-10`)).toEqual(1);

    // after
    const result = prepareSportsData(data);
    expect(Object.values(result.schedule.soccer).length).toEqual(1);
    expect(Object.keys(result.schedule.soccer).indexOf(`${prevFullYear}-09-10`)).toEqual(-1);
    expect(Object.keys(result.schedule.soccer).indexOf(`${nextFullYear}-09-10`)).toEqual(0);
  });

  test('add provider object to events', () => {
    // before
    expect(data.events['30186'].providers.length).toEqual(2);
    expect(data.events['30186'].providers[0]).toEqual(2);
    expect(data.events['30186'].providers[0].name).toEqual(undefined);

    // after
    const result = prepareSportsData(data);
    expect(result.events['30186'].providers[0].name).toEqual('Bet365');
    expect(result.events['30186'].providers[1].name).toEqual('Viaplay.dk');
  });

  test('add events object to schedule', () => {
    // before
    expect(
      data.schedule.soccer[`${nextFullYear}-09-10`].tournaments.championship.events[0]
    ).toEqual(30190);
    expect(
      data.schedule.soccer[`${nextFullYear}-09-10`].tournaments.championship.events[0].id
    ).toEqual(undefined);
    expect(
      data.schedule.soccer[`${nextFullYear}-09-10`].tournaments.superleague.events[1].id
    ).toEqual(undefined);

    // after
    const result = prepareSportsData(data);
    expect(
      result.schedule.soccer[`${nextFullYear}-09-10`].tournaments.championship.events[0].id
    ).toEqual(30190);
    expect(
      result.schedule.soccer[`${nextFullYear}-09-10`].tournaments.superleague.events[1].id
    ).toEqual(30189);
  });

  test('add events object to featured events', () => {
    // before
    expect(data.featured_events.soccer[1]).toEqual(30185);
    expect(data.featured_events.soccer[1].id).toEqual(undefined);

    // after
    const result = prepareSportsData(data);
    expect(result.featured_events.soccer.indexOf('30185')).toEqual(-1);
    expect(result.featured_events.soccer[1].id).toEqual(30185);
  });

  test('add teams object to tournaments', () => {
    // before
    expect(data.tournaments['324'].teams[1]).toEqual(2461);
    expect(data.tournaments['324'].teams[1].name).toEqual(undefined);
    expect(data.tournaments['122'].teams[0].name).toEqual(undefined);

    // after
    const result = prepareSportsData(data);
    expect(data.tournaments['324'].teams.indexOf(2461)).toEqual(-1);
    expect(result.tournaments['324'].teams[1].name).toEqual('Borussia Dortmund');
    expect(result.tournaments['122'].teams[0].name).toEqual('Cardiff');
  });
});

describe('Add Sports Data', () => {
  const themeType = null;
  let pages = null;
  let sportsData = null;
  let relation = {};

  beforeEach(() => {
    pages = getSportsDataPagesList();
    sportsData = getSportsData();
  });

  test('active sport and filter providers by sport', () => {
    const activeSportPages = pages.filter(
      (page) =>
        (page.template === 'sport' || page.template === 'live_stream_providers') &&
        page.relation_id &&
        sportsData.sports[page.relation_id]
    );

    relation = addSportsDataToPage(sportsData, activeSportPages[0], themeType);
    expect(relation.schedule.length).toEqual(0);
    expect(relation.featured_events.length).toEqual(0);
    expect(relation.providers.length).toEqual(1);
    expect(relation.sport.name).toEqual('tennis');

    relation = addSportsDataToPage(sportsData, activeSportPages[1], themeType);
    expect(Object.values(relation.schedule).length).toEqual(2);
    expect(Object.values(relation.featured_events).length).toEqual(3);
    expect(Object.values(relation.providers).length).toEqual(2);
    expect(relation.sport.name).toEqual('soccer');
  });

  test('add event data to "event" page type', () => {
    const eventPages = pages.filter(
      (page) => page.template === 'event' && page.relation_id && sportsData.events[page.relation_id]
    );

    relation = addSportsDataToPage(sportsData, eventPages[0], themeType);
    expect(relation.event.id).toEqual(eventPages[0].relation_id);
    expect(relation.sport.name).toEqual('soccer');
  });

  test('add providers to "homepage" and "provider_reviews" page type', () => {
    const homeProviderPages = pages.filter(
      (page) => page.template === 'homepage' || page.template === 'provider_reviews'
    );

    relation = addSportsDataToPage(sportsData, homeProviderPages[0], themeType);
    expect(relation.providers.length).toEqual(2);
    expect(relation.providers[1].name).toEqual('Viaplay.dk');

    relation = addSportsDataToPage(sportsData, homeProviderPages[1], themeType);
    expect(relation.providers[0].name).toEqual('Bet365');
  });

  test('add a provider to "provider review" page type', () => {
    const providerReviewPages = pages.filter(
      (page) => page.template === 'provider_review' && page.relation_id
    );

    relation = addSportsDataToPage(sportsData, providerReviewPages[0], themeType);
    expect(relation.provider.name).toEqual('Bet365');
    expect(relation.provider.id).toEqual(`${providerReviewPages[0].relation_id}`);
  });

  test('add all events schedule to "live stream" page type', () => {
    const liveStreamPages = pages.filter((page) => page.template === 'live_stream');

    relation = addSportsDataToPage(sportsData, liveStreamPages[0], themeType);
    expect(Object.keys(relation.schedule).length).toEqual(3);
    expect(Object.keys(relation.schedule.soccer).length).toEqual(2);
  });

  // rage-seo pages data
  test('rage-seo homepage data', () => {
    const seoHomePages = pages.filter((page) => page.template === 'homepage');

    relation = addSportsDataToPage(sportsData, seoHomePages[0], 'rage_seo');
    expect(Object.values(relation.schedule).length).toEqual(2);
    expect(relation.featured_events.length).toEqual(3);
    expect(Object.values(relation.tournaments).length).toEqual(2);
  });

  test('rage-seo live_stream data', () => {
    const seoLiveStreamPages = pages.filter((page) => page.template === 'live_stream');

    relation = addSportsDataToPage(sportsData, seoLiveStreamPages[0], 'rage_seo');
    expect(Object.values(relation.schedule).length).toEqual(2);
    expect(relation.providers.length).toEqual(2);
    expect(relation.providers[0].name).toEqual('Bet365');
  });

  test('rage-seo live_stream_providers data', () => {
    const seoLiveStreamProviderPages = pages.filter(
      (page) => page.template === 'live_stream_providers'
    );

    relation = addSportsDataToPage(sportsData, seoLiveStreamProviderPages[0], 'rage_seo');
    expect(relation.providers.length).toEqual(2);
    expect(relation.providers[0].name).toEqual('Bet365');
  });

  test('rage-seo team data', () => {
    const seoTeamPages = pages.filter(
      (page) => page.template === 'team' && page.relation_id && sportsData.teams[page.relation_id]
    );

    relation = addSportsDataToPage(sportsData, seoTeamPages[0], 'rage_seo');
    expect(relation.team.name).toEqual('FC Midtjylland');
    expect(relation.event.id).toEqual(30184);
  });

  test('rage-seo tournament data', () => {
    const seoTournamentPages = pages.filter(
      (page) =>
        page.template === 'tournament' &&
        page.relation_id &&
        sportsData.tournaments[page.relation_id]
    );

    relation = addSportsDataToPage(sportsData, seoTournamentPages[0], 'rage_seo');
    expect(relation.event.id).toEqual(30184);
    expect(relation.providers.length).toEqual(2);
    expect(relation.providers[0].name).toEqual('Bet365');
  });

  test('rage-seo event data', () => {
    const seoEventPages = pages.filter(
      (page) => page.template === 'event' && page.relation_id && sportsData.events[page.relation_id]
    );

    relation = addSportsDataToPage(sportsData, seoEventPages[0], 'rage_seo');
    expect(relation.event.id).toEqual(seoEventPages[0].relation_id);
    expect(relation.tournament.name).toEqual('England Championship');
    expect(relation.schedule.length).toEqual(2);
  });

  test('rage-seo team/tournament/event data', () => {
    const seoTeamTournamentEventPages = pages.filter(
      (page) =>
        page.template === 'team' || page.template === 'tournament' || page.template === 'event'
    );

    relation = addSportsDataToPage(sportsData, seoTeamTournamentEventPages[0], 'rage_seo');
    expect(relation.mainProviders[0].name).toEqual('Bet365');
  });

  test('rage-seo leagues_list data', () => {
    const seoLeaguesPages = pages.filter((page) => page.template === 'leagues_list');

    relation = addSportsDataToPage(sportsData, seoLeaguesPages[0], 'rage_seo');
    expect(Object.values(relation.tournaments).length).toEqual(2);
    expect(Object.values(relation.tournaments)[1].name).toEqual('Super Cup');
  });
});

describe('Add Extra fields to showcase events', () => {
  let pages = null;
  let sportsData = null;

  beforeEach(() => {
    pages = getSportsDataPagesList();
    sportsData = getSportsData();
  });

  test('addExtraFieldsToShowcaseEvents', () => {
    const eventPages = pages.filter((page) => page.template === 'event' && page.relation_id);

    const events = addExtraFieldsToShowcaseEvents(eventPages, Object.values(sportsData.events));
    events.forEach((event) => expect(event.extra_fields).toBeTruthy());
  });
});

afterEach(() => {
  cleanup();
});
