/* eslint-disable import/no-extraneous-dependencies */
import loadash from 'lodash/index.js';
import {
  filterScheduleByTournaments,
  filterScheduleByTeam,
  filterScheduleByDate,
  filterEvents,
  findTournaments,
  getSortedEvents,
} from '../schedule.mjs';

const { cloneDeep, intersectionWith, isEqual } = loadash;

export function prepareSportsData(sportsData) {
  Object.keys(sportsData.tournaments).forEach((key) => {
    if (sportsData.tournaments[key].events) {
      sportsData.tournaments[key].events = filterEvents(
        sportsData.tournaments[key].events,
        sportsData.events
      );
    }
  });

  Object.keys(sportsData.teams).forEach((key) => {
    if (sportsData.teams[key].events) {
      sportsData.teams[key].events = filterEvents(sportsData.teams[key].events, sportsData.events);
    }
  });

  Object.keys(sportsData.schedule).forEach((sport) => {
    sportsData.schedule[sport] = filterScheduleByDate(sportsData.schedule[sport]);
  });

  // add provider object to events
  Object.keys(sportsData.events).forEach((eventKey) => {
    const event = sportsData.events[eventKey];
    event.providers.forEach((providerId, index) => {
      event.providers[index] = sportsData.providers[providerId];
    });
  });

  // add events object to schedule
  Object.keys(sportsData.schedule).forEach((sportKey) => {
    const sportSchedule = sportsData.schedule[sportKey];
    Object.keys(sportSchedule).forEach((dateKey) => {
      const dateSchedule = sportSchedule[dateKey].tournaments;
      Object.keys(dateSchedule).forEach((tournamentKey) => {
        const tournament = dateSchedule[tournamentKey];
        tournament.events.forEach((eventId, index) => {
          tournament.events[index] = sportsData.events[eventId];
        });
      });
    });
  });

  // add events object to featured events
  Object.keys(sportsData.featured_events).forEach((sportKey) => {
    sportsData.featured_events[sportKey].forEach((eventId, index) => {
      sportsData.featured_events[sportKey][index] = sportsData.events[eventId];
    });
  });

  Object.keys(sportsData.showcased_events).forEach((sportKey) => {
    const filteredEventIds = filterEvents(sportsData.showcased_events[sportKey], sportsData.events);

    sportsData.showcased_events[sportKey] = filteredEventIds
      .map((eventId) => sportsData.events[eventId])
      .filter((event) => event);
  });

  // add teams object to tournaments
  Object.keys(sportsData.tournaments).forEach((key) => {
    const tournament = sportsData.tournaments[key];
    tournament.teams.forEach((teamId, index) => {
      tournament.teams[index] = sportsData.teams[teamId];
    });
  });

  return sportsData;
}

export function addSportsDataToPage(sportsData, page, themeType) {
  const pageType = page.template;
  const relation = {};
  // map providers object to an array
  const providersArray = Object.keys(sportsData.providers).map((key) => {
    sportsData.providers[key].id = key;
    return sportsData.providers[key];
  });

  if (
    (pageType === 'sport' || pageType === 'live_stream_providers') &&
    page.relation_id &&
    sportsData.sports[page.relation_id]
  ) {
    // active sport
    const sport = sportsData.sports[page.relation_id].name;
    relation.schedule = sportsData.schedule[sport] || null;
    relation.featured_events = sportsData.featured_events[sport] || null;
    relation.showcased_events = sportsData.showcased_events[sport] || null;
    // filter providers by sport
    relation.providers = providersArray.filter((provider) => provider.sports_data[sport] !== null);
    relation.sport = sportsData.sports[page.relation_id];
  }
  // add event data to 'event' page type
  if (pageType === 'event' && page.relation_id && sportsData.events[page.relation_id]) {
    relation.event = sportsData.events[page.relation_id];
    relation.sport = sportsData.sports[relation.event.sport.id];
  }
  // add providers to 'homepage' and 'provider_reviews' page type
  if (pageType === 'homepage' || pageType === 'provider_reviews') {
    relation.providers = providersArray;
  }
  // add a provider to 'provider review' page type
  if (pageType === 'provider_review' && page.relation_id) {
    relation.provider = sportsData.providers[page.relation_id];
  }
  // add all events schedule to 'live stream' page type
  if (pageType === 'live_stream') {
    relation.schedule = sportsData.schedule;
  }
  // rage-seo pages data
  if (themeType === 'rage_seo' || themeType === 'sports') {
    if (pageType === 'homepage' || pageType === 'default') {
      relation.schedule = sportsData.schedule.soccer || null;
      relation.featured_events = sportsData.featured_events.soccer || null;
      relation.showcased_events = sportsData.showcased_events.soccer || null;
      relation.tournaments = sportsData.tournaments;
    }
    if (pageType === 'live_stream') {
      relation.schedule = sportsData.schedule.soccer || null;
      relation.providers = providersArray.filter(
        (provider) => provider.sports_data.soccer !== null
      );
    }
    if (pageType === 'live_stream_providers') {
      relation.providers = providersArray.filter(
        (provider) => provider.sports_data.soccer !== null
      );
    }

    if (pageType === 'team' && page.relation_id && sportsData.teams[page.relation_id]) {
      relation.team = sportsData.teams[page.relation_id];
      // closet team event
      relation.event =
        relation.team.events.length > 0 ? sportsData.events[relation.team.events[0]] : null;
      relation.schedule = filterScheduleByTeam(
        cloneDeep(sportsData.schedule.soccer),
        relation.team.events
      );
    }

    if (pageType === 'tournament' && page.relation_id) {
      relation.tournaments = findTournaments(sportsData.tournaments, page.relation_id);
      relation.schedule = filterScheduleByTournaments(
        cloneDeep(sportsData.schedule.soccer),
        page.relation_id
      );
      // eslint-disable-next-line prefer-destructuring
      relation.event = getSortedEvents(relation.tournaments, sportsData.events)[0];
      // relation.events = findEvents(relation.tournaments, sportsData.events);
      relation.providers = providersArray.filter(
        (provider) => provider.sports_data.soccer !== null
      );
    }
    if (pageType === 'event' && page.relation_id && sportsData.events[page.relation_id]) {
      relation.event = sportsData.events[page.relation_id];
      relation.tournament = relation.event.tournament;
      relation.schedule = filterScheduleByTournaments(
        cloneDeep(sportsData.schedule.soccer),
        relation.tournament.id
      );
    }
    if (pageType === 'team' || pageType === 'tournament' || pageType === 'event') {
      const mainProviders = providersArray.filter(
        (provider) => provider.sports_data.soccer !== null && provider.main === 1
      );
      relation.mainProviders = mainProviders;
      const intersections =
        (relation.event &&
          intersectionWith(relation.event.providers, [relation.mainProviders[0]], isEqual)) ||
        [];
      relation.theEventIsStreamedByMainProvider = intersections.length;
    }

    if (pageType === 'leagues_list') {
      relation.tournaments = sportsData.tournaments;
    }

    // add more
  }

  if (sportsData.sports) {
    relation.sports = Object.keys(sportsData.sports).map((key) => sportsData.sports[key]);
  }

  // etc..
  return relation;
}

export function addExtraFieldsToShowcaseEvents(eventPages, showcasedEvents) {
  return showcasedEvents.reduce((acc, event) => {
    for (let j = 0; j < eventPages.length; j += 1) {
      if (event.id === eventPages[j].relation_id) {
        return [...acc, { ...event, extra_fields: eventPages[j].extra_fields }];
      }
    }

    return acc;
  }, []);
}
