/* eslint-disable import/prefer-default-export */
import {
  breadcrumbsSchema,
  webPageSchema,
  organizationSchema,
  moduleSchemas,
  customSchema,
  getAuthors,
  generateSchemaObject,
  prosAndConsData,
} from 'gatsby-core-theme/src/helpers/schema';
import { getGameRating, getUrl, imagePrettyUrl } from 'gatsby-core-theme/src/helpers/getters';
import { SchemaConstant } from 'gatsby-core-theme/src/constants/schema';

const knowsAbout = [
  {
    '@type': 'Thing',
    name: 'Norway',
    sameAs: [
      'https://no.wikipedia.org/wiki/Norge',
      'https://www.google.no/search?q=Norge&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
  {
    '@type': 'Thing',
    name: 'LottStift',
    sameAs: [
      'https://no.wikipedia.org/wiki/Lotteri-_og_stiftelsestilsynet',
      'https://www.google.no/search?q=lottstift&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
  {
    '@type': 'Thing',
    name: 'Kasino',
    sameAs: [
      'https://no.wikipedia.org/wiki/Kasino',
      'https://www.google.no/search?q=Kasino&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
  {
    '@type': 'Thing',
    name: 'Norsk Tipping Kommisjonær',
    sameAs: [
      'https://no.wikipedia.org/wiki/Norsk_Tipping',
      'https://www.google.no/search?q=norsk+tipping&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
];
export function templateSchemas(page, pageImage) {
  const sharedProps = {
    author: getAuthors(page),
    datePublished: page?.manual_published_date || page?.created_at,
    dateModified: page?.manual_updated_date || page?.updated_at,
    publisher: {
      '@type': 'Organization',
      name:
        page.siteSchema?.owned_by ||
        customSchema[process.env.GATSBY_SITE_NAME] ||
        page.siteInfo?.site_name ||
        process.env.GATSBY_SITE_NAME,
      logo:
        (page.siteInfo?.site_logo && `${process.env.IMAGE_CDN_URL}/${page.siteInfo.site_logo}`) ||
        page.featured_image_object?.url ||
        pageImage,
    },
  };
  switch (page.relation_type) {
    case 'article': {
      const schema = {
        '@context': 'https://schema.org',
        '@type': 'Article',
        '@id': `${getUrl(page.path)}#article`,
        url: getUrl(page.path),
        headline: page.title,
        mainEntityOfPage: {
          '@type': 'WebPage',
          url: process.env.GATSBY_SITE_URL,
        },
        ...sharedProps,
      };

      return JSON.stringify(generateSchemaObject(schema));
    }
    case 'operator': {
      const schema = {
        '@context': 'https://schema.org',
        '@type': 'Review',
        '@id': `${getUrl(page.path)}#review`,
        ...prosAndConsData(page),
        name: page.relation?.name || '',
        reviewBody: page.extra_fields?.operator_summary || '',
        itemReviewed: {
          '@type': 'Organization',
          url: getUrl(page.path),
          name: page.title || '',
          image: imagePrettyUrl(
            page.relation?.logo?.filename,
            SchemaConstant.operatorHeaderWidth,
            SchemaConstant.operatorHeaderHeight
          ),
        },
        reviewRating: {
          '@type': 'Rating',
          bestRating: '5',
          worstRating: '0',
          ratingValue: page.relation?.rating?.toString() || '',
        },
        ...sharedProps,
      };

      return JSON.stringify(generateSchemaObject(schema));
    }
    case 'game': {
      const rating = Math.floor(getGameRating(page.relation) * 2);
      const schema = {
        '@context': 'https://schema.org',
        '@type': 'VideoGame',
        '@id': `${getUrl(page.path)}#videogame`,
        url: getUrl(page.path),
        name: page.relation?.name || '',
        description: page.extra_fields?.game_summary || '',
        operatingSystem: 'Multi-platform',
        applicationCategory: ['Game'],
        author: {
          '@type': 'Organization',
          name: process.env.GATSBY_SITE_NAME,
          url: process.env.GATSBY_SITE_URL,
        },
        review: {
          '@type': 'Review',
          ...prosAndConsData(page),
          name: page.relation?.name || '',
          reviewRating: {
            '@type': 'Rating',
            bestRating: '10',
            worstRating: '0',
            ratingValue: rating?.toString() || '',
          },
          ...sharedProps,
        },
      };

      return JSON.stringify(generateSchemaObject(schema));
    }
    default:
      return null;
  }
}
export function schemaGenerator(page = {}, pageImage) {
  const isNSA = process.env.GATSBY_SITE_NAME === 'norskespilleautomater.com';
  const isHomepage = page.path === '/';
  const tracker = page?.template === 'tracker';
  if (isNSA) {
    page.knowsAbout = knowsAbout;
  }
  const includeWebpageSchemaInAllPages =
    process.env.GATSBY_INCLUDE_WEBPAGE_SCHEMA_IN_ALL_PAGES === 'true';
  // Temporary Change for removing webPageSchema
  const hasWebPageSchema =
    (page.relation_type !== 'operator' && page.relation_type !== 'game') ||
    isNSA ||
    includeWebpageSchemaInAllPages;
  const hasOrganisationSchema = page.path === '/' || page.path === 'about-us';
  const jsonSchema = [
    // Page Schema
    page.seo_json_schema,
    // Breadcrumbs Schema
    !isHomepage && !tracker ? breadcrumbsSchema(page) : null,
    hasWebPageSchema && !tracker ? webPageSchema(page, pageImage) : null,
    hasOrganisationSchema ? organizationSchema(page, pageImage) : null,
    templateSchemas(page, pageImage),
    // Modules Schemas
    ...(page.sections?.main?.modules
      ? moduleSchemas(page.sections.main.modules, page.path, page, pageImage)
      : [null]),
  ];

  return jsonSchema;
}
