import { toPublicURL } from '@plone/volto/helpers';
import {
  SchemaOrg,
  SchemaOrgUtils,
  richTextHasContent,
} from 'io-sanita-theme/helpers';

const EventoSchemaOrg = ({ content }) => {
  let schemaOrg = {
    '@type': 'Event',
    name: content?.title,
    startDate: content?.start,
    endDate: content?.end,
    url: toPublicURL(content['@id']),
  };

  let description = [];
  if (content.description) {
    description.push(content.description);
  }
  if (richTextHasContent(content.descrizione_estesa)) {
    description.push(
      SchemaOrgUtils.fieldDataToPlainText(content.descrizione_estesa),
    );
  }
  if (description.length > 0) {
    schemaOrg.description = description.join('. ');
  }

  // se l'evento è fisico priorità all'indirizzo scritto direttamente nel CT, in alternativa la prima struttura associata
  if (content.street || content.city || content.zip_code || content.nome_sede) {
    schemaOrg.location = {
      '@type': 'Place',
      name: content?.nome_sede || 'Sede',
      ...((content.street ||
        content.city ||
        content.zip_code ||
        (content.geolocation?.latitude && content.geolocation?.longitude)) && {
        address: {
          '@type': 'PostalAddress',
          ...(content.street && { streetAddress: content.street }),
          ...(content.city && { addressLocality: content.city }),
          ...(content.zip_code && { postalCode: content.zip_code }),
          addressCountry: 'IT',
        },
      }),
      ...(content.geolocation?.latitude &&
        content.geolocation?.longitude && {
          geo: {
            '@type': 'GeoCoordinates',
            latitude: content.geolocation.latitude,
            longitude: content.geolocation.longitude,
          },
        }),
    };
  } else if (content.struttura_correlata?.length > 0) {
    schemaOrg.location = {
      '@type': 'Place',
      name: content.struttura_correlata[0].title,
      ...((content.struttura_correlata[0].street ||
        content.struttura_correlata[0].city ||
        content.struttura_correlata[0].zip_code ||
        (content.struttura_correlata[0].geolocation?.latitude &&
          content.struttura_correlata[0].geolocation?.longitude)) && {
        address: {
          '@type': 'PostalAddress',
          ...(content.struttura_correlata[0].street && {
            streetAddress: content.struttura_correlata[0].street,
          }),
          ...(content.struttura_correlata[0].city && {
            addressLocality: content.struttura_correlata[0].city,
          }),
          ...(content.struttura_correlata[0].zip_code && {
            postalCode: content.struttura_correlata[0].zip_code,
          }),
          addressCountry: 'IT',
        },
      }),
      ...(content.struttura_correlata[0]?.geolocation?.latitude &&
        content.struttura_correlata[0]?.geolocation?.longitude && {
          geo: {
            '@type': 'GeoCoordinates',
            latitude: content.struttura_correlata[0].geolocation.latitude,
            longitude: content.struttura_correlata[0].geolocation.longitude,
          },
        }),
    };
  }

  // modalità di partecipazione
  const isOnline = richTextHasContent(content.webinar);
  if (isOnline && schemaOrg.location) {
    schemaOrg.eventAttendanceMode =
      'https://schema.org/MixedEventAttendanceMode';
  } else if (isOnline) {
    schemaOrg.eventAttendanceMode =
      'https://schema.org/OnlineEventAttendanceMode';
  } else if (schemaOrg.location) {
    schemaOrg.eventAttendanceMode =
      'https://schema.org/OfflineEventAttendanceMode';
  }

  // se l'evento è online aggiungo il luogo virtuale
  if (isOnline) {
    const webinarText = SchemaOrgUtils.fieldDataToPlainText(content.webinar);
    const webinarUrl =
      JSON.stringify(content.webinar).match(/"url":"([^"]+)"/)?.[1] ||
      webinarText.match(/(https?:\/\/|www\.)\S+/)?.[0];
    const virtualLocation = {
      '@type': 'VirtualLocation',
      name: webinarText,
      ...(webinarUrl && {
        url: /^https?:\/\//.test(webinarUrl)
          ? webinarUrl
          : `https://${webinarUrl}`,
      }),
    };
    schemaOrg.location = schemaOrg.location
      ? [schemaOrg.location, virtualLocation]
      : virtualLocation;
  }

  // organizzatore interno
  if (content.organizzato_da_interno?.length > 0) {
    schemaOrg.organizer = {
      '@type': 'Organization',
      name: content.organizzato_da_interno[0].title,
      url: toPublicURL(content.organizzato_da_interno[0]['@id']),
    };
  }

  // contributo esterno
  if (richTextHasContent(content.organizzato_da_esterno)) {
    schemaOrg.contributor = {
      '@type': 'Organization',
      name: SchemaOrgUtils.fieldDataToPlainText(content.organizzato_da_esterno),
    };
  }

  //chi partecipa
  let attendeeList = [];
  if (richTextHasContent(content.parteciperanno)) {
    attendeeList.push(
      SchemaOrgUtils.fieldDataToPlainText(content.parteciperanno),
    );
  }
  if (content.persona_correlata?.length > 0) {
    attendeeList.push(content.persona_correlata.map((item) => item.title));
  }
  if (attendeeList.length > 0) {
    schemaOrg.attendee = {
      '@type': 'Person',
      name: attendeeList.join(', '),
    };
  }

  // a chi si rivolge
  let audienceList = [];
  if (richTextHasContent(content.a_chi_si_rivolge)) {
    audienceList.push(
      SchemaOrgUtils.fieldDataToPlainText(content.a_chi_si_rivolge),
    );
  }
  if (content?.a_chi_si_rivolge_tassonomia?.length > 0) {
    audienceList.push(
      content.a_chi_si_rivolge_tassonomia.map((item) => item.title),
    );
  }
  if (audienceList.length > 0) {
    schemaOrg.audience = {
      '@type': 'Audience',
      audienceType: audienceList.join(', '),
    };
  }

  // costi o biglietti
  if (richTextHasContent(content.costo)) {
    schemaOrg.offers = {
      '@type': 'Offer',
      price: SchemaOrgUtils.fieldDataToPlainText(content.costo),
      priceCurrency: 'EUR',
    };
  }

  // sponsor
  if (richTextHasContent(content.patrocinato_da)) {
    schemaOrg.sponsor = {
      '@type': 'Organization',
      name: SchemaOrgUtils.fieldDataToPlainText(content.patrocinato_da),
    };
  }

  return <SchemaOrg content={content} schema={schemaOrg} />;
};

export default EventoSchemaOrg;
