"use strict";
const cdnHelper = require("./cdn.helper");
const fetch = require("node-fetch");

const partnersCurrent = [
  {
    name: "CantabriaLabs",
    image: "https://cdnapp2.loquedeverdadimporta.org/partners/cantabria_labs/banner_live.jpg",
    width: "1080",
    height: "540",
    link: "https://es.surveymonkey.com/r/HABITOS-AL-SOL-HELIOCARE-2021",
  },
  {
    name: "Gonvarri",
    image: "https://cdnapp2.loquedeverdadimporta.org/partners/gonvarri/banner_live.jpg",
    width: "1080",
    height: "540",
    link: "http://www.emotionaldriving.com/mensajes/",
  },
  {
    name: "Bizum",
    image: "https://cdnapp2.loquedeverdadimporta.org/banners/banner-bizum-alt-app.png",
    width: "1080",
    height: "540",
    link: "",
  },
];

const partnersPast = [
  {
    name: "CantabriaLabs",
    image: "https://cdnapp2.loquedeverdadimporta.org/partners/cantabria_labs/banner_past.jpg",
    width: "1080",
    height: "445",
    link: "default",
  },
  {
    name: "Gonvarri",
    image: "https://cdnapp2.loquedeverdadimporta.org/partners/gonvarri/banner_past.jpg",
    width: "1080",
    height: "445",
    link: "default", //"https://twitter.com/hashtag/ED${city}?src=hash"
  },
];

const valuesComposer = (values) => (values ? values.map((value) => ({ id: value.id, name: value.name })) : values);

const citiesComposer = (city, context) => {
  if (city) {
    if (city.location) city.location = locationComposer(city.location, context);

    return city;
  } else return city;
};

const locationComposer = (location, context) => {
  if (location) {
    let multimedias = [];
    if (location.multimedias) multimedias = multimediaComposer(location.multimedias);

    return Object.assign({}, location, { multimedias: multimedias });
  } else return location;
};

const commentComposer = (comment, context) => {
  if (comment.user) {
    comment.user_name = comment.user.name + " " + comment.user.surname;
    comment.profile_picture = cdnHelper.getCDNMediaUrl(comment.user.profile_picture);
  } else {
    comment.user_name = comment.nameUserOld;
    comment.profile_picture = cdnHelper.getCDNMediaUrl(comment.profile_pictureOld);
  }
  delete comment.nameUserOld;
  delete comment.profile_pictureOld;
  delete comment.userId;
  delete comment.user;

  return Object.assign({}, comment);
};

const commentsComposer = (comments) => {
  if (comments) {
    return comments.map((comment) => ({
      ...commentComposer(comment),
    }));
  } else return comments;
};

const multimediaComposer = (multimedias) => {
  if (multimedias) {
    return multimedias.map((multimedia) => ({
      ...multimedia,
      ...multimedia.multimediaFile,
      type: multimedia.type,
      media_type: multimedia.multimediaFile.type,
      multimediaFile: undefined,
      createdAt: undefined,
      updatedAt: undefined,
      userId: undefined,
      url:
        multimedia.multimediaFile.provider === "cdn"
          ? cdnHelper.getCDNMediaUrl(multimedia.multimediaFile.url)
          : multimedia.multimediaFile.url,
    }));
  } else return multimedias;
};

const socialNetworksComposer = (speaker) => {
  if (speaker) {
    return {
      rrss: {
        twitter: speaker.twitter ? speaker.twitter : null,
        facebook: speaker.facebook ? speaker.facebook : null,
        youtube: speaker.youtube ? speaker.youtube : null,
        linkedin: speaker.linkedin ? speaker.linkedin : null,
        instagram: speaker.instagram ? speaker.instagram : null,
        web: speaker.web ? speaker.web : null,
      },
      twitter: undefined,
      facebook: undefined,
      youtube: undefined,
      linkedin: undefined,
      instagram: undefined,
      web: undefined,
    };
  } else return speaker;
};

const speakerComposer = (speaker, context) => {
  let multimedias = [];
  if (
    context.scopes &&
    (context.scopes.includes("includeMultimedias") || context.scopes.includes("includeMultimediaAvatar"))
  ) {
    multimedias = multimediaComposer(speaker.multimedias);
  }

  let comments = [];
  if (context.scopes && context.scopes.includes("includeComments")) {
    comments = commentsComposer(speaker.comments);
  }

  if (speaker.type) {
    speaker.typename = speaker.type.name;
    delete speaker.type;
  }

  speaker.values = valuesComposer(speaker.values);

  const rrss = socialNetworksComposer(speaker, context);

  return Object.assign({}, speaker, rrss, { multimedias: multimedias }, { comments: comments });
};

const eventComposer = (event, context) => {
  const getPartnersData = () => {
    let partners = [];

    if (
      event &&
      event.type &&
      event.type.name == "conference" &&
      event.location &&
      event.location.country == "España"
    ) {
      if (event.stateCode && event.stateCode === "current_event") {
        return partnersCurrent;
      } else if (event.stateCode === "closed_event") {
        return partnersPast;
      }
    }
    return partners;

    if (event && event.location && event.location.country == "España") {
      try {
        let urlJSON = undefined;
        if (event.stateCode && event.stateCode === "current_event") {
          urlJSON = cdnHelper.getCDNCurrentPartnersJSON;
        } else if (event.stateCode === "closed_event") {
          urlJSON = cdnHelper.getCDNPastPartnersJSON;
        } else {
          return partners;
        }

        let response = fetch(urlJSON);
        let partners = response.json();

        return partners;
      } catch (error) {
        return partners;
      }
    }

    return partners;
  };

  if (context.scopes && context.scopes.includes("includeVenue")) {
    if (event.venue) {
      delete event.venue.updatedAt;
      delete event.venue.createdAt;
      //event.venue.image_url = cdnHelper.getCDNCityMediaUrl(event.venue.city); <-- se hace en el modelo
    }
  }

  let multimedias = [];
  if (
    context.scopes &&
    (context.scopes.includes("includeMultimedias") || context.scopes.includes("includeMultimediaAvatar"))
  ) {
    multimedias = multimediaComposer(event.multimedias);
  }

  let comments = [];
  if (context.scopes && context.scopes.includes("includeComments")) {
    comments = commentsComposer(event.comments);
  }

  let speakers = [];
  let details = [];
  if (context.scopes && context.scopes.includes("includeDetails")) {
    event.details.map((detail) => {
      if (detail.type == "speaker")
        speakers.push({
          order: detail.order,
          ...speakerComposer(detail.speaker, context),
        });
      if (detail.type == "info")
        details.push({
          ...detail,
          speaker: undefined,
        });
    });
  }

  let partners = getPartnersData();

  return Object.assign(
    {},
    event,
    { multimedias: multimedias },
    { details: details },
    { speakers: speakers },
    { partners: partners }
  );
};

const entityComposer = (entity, context) => {
  return Object.assign({}, entity);
};

const usersIdsComposer = (inscriptions) => {
  let usersId = [];
  if (inscriptions) {
    inscriptions.map((inscription) => {
      usersId.push(inscription.userId);
    });
  }
  return usersId;
};

module.exports = {
  entityComposer,
  speakerComposer,
  eventComposer,
  commentComposer,
  citiesComposer,
  locationComposer,
  usersIdsComposer,
};
