import { CreationListItem } from '@/types/Creation';

import { getImageByIdAndName } from './images';

/* eslint-disable @typescript-eslint/no-explicit-any */
export const getPlaceOfOrigin = (placeoforigin: any): string => {
  if (!placeoforigin) return '';

  return placeoforigin?.city ? `, ${placeoforigin.city}` : '';
};

export const getAttributes = (param: any) => {
  const names = [
    'Alkotó szerepe',
    'Akadémikus szerepe eredménye',
    'Akadémikus szerepe',
    'Fejezetcím',
    'Szerep',
    'Előadott mű címe',
    'Kiadás száma',
    'Műfaj',
    'Film műfaja',
    'Intézmény neve',
    'Filmművészti alkotás státusza',
    'Gyártó stúdió',
    'Kapcsolódó szerző',
  ];
  let attributes = '';
  const attributeList = param.attributumok;

  if (attributeList) {
    for (let i = 0; i < attributeList.length; i++) {
      if (names.includes(attributeList[i].attributumNeve))
        attributes += ', ' + attributeList[i].attributumErtek;
      if (attributeList[i].attributumNeve === 'Szerző')
        attributes += ', Szerző: ' + attributeList[i].attributumErtek;
      if (attributeList[i].attributumNeve === 'Sorozat')
        attributes += ', Sorozatcím: ' + attributeList[i].attributumErtek;
      if (attributeList[i].attributumNeve === 'Terjedelem')
        attributes += ', Terjedelem: ' + attributeList[i].attributumErtek;
      if (attributeList[i].attributumNeve === 'Borítótervező')
        attributes += ', Borítótervező: ' + attributeList[i].attributumErtek;
      if (attributeList[i].attributumNeve === 'Nyelv')
        attributes += ', Kiadvány nyelve: ' + attributeList[i].attributumErtek;
      if (attributeList[i].attributumNeve === 'Rendező')
        attributes += ', Rendező: ' + attributeList[i].attributumErtek;
    }
  }

  return attributes;
};

export const getMainImage = (mainImage: any) => {
  let img = '';

  if (mainImage) {
    img = process.env.NEXT_PUBLIC_API_URL + '/imageRepository/getImage?key=' + mainImage.key;
  }

  return img;
};

export const getCreationDateLabel = (item: CreationListItem): string | null => {
  if (!item) return null;
  if (item.label) return item.label;
  return null;
};

export const getCreationCardVideoId = (creation: CreationListItem): string | null => {
  if (creation.cardType === 'creation') {
    // Check for "alkotasrol_szolo_video" type in hivatkozasLista
    const alkotasrolSzoloVideo = creation.hivatkozasLista?.find(
      (item) => item.tipus === 'alkotasrol_szolo_video'
    );

    if (alkotasrolSzoloVideo?.hivatkozas) {
      return alkotasrolSzoloVideo.hivatkozas.trim() || null;
    }

    // Fallback to creation.hivatkozas if exists and not empty
    if (creation.hivatkozas && creation.hivatkozas.trim()) {
      return creation.hivatkozas.trim();
    }

    return null;
  } else if (creation.cardType === 'news' || creation.cardType === 'life') {
    // Find first "esemeny_video" type in hivatkozasLista
    const esemenyVideo = creation.hivatkozasLista?.find((item) => item.tipus === 'esemeny_video');

    return esemenyVideo?.hivatkozas?.trim() || null;
  }

  return null;
};

export const getCreationCardImageSrc = (creation: CreationListItem): string | null => {
  // if its creation type and has necessary img data, return image url
  if (creation.cardType === 'creation') {
    return creation.fokepId && creation.fokepNev
      ? getImageByIdAndName(creation.fokepId, creation.fokepNev)
      : null;
    // if its life or news type return first image of kepek array
  } else if (creation.cardType === 'news' || creation.cardType === 'life') {
    return creation.kepek?.[0]?.fileName && creation.kepek?.[0]?.id
      ? getImageByIdAndName(creation.kepek[0].id, creation.kepek[0].fileName)
      : null;
  } else {
    return null;
  }
};

export const getCreationCardVideoOrImage = (creation: CreationListItem): string | null => {
  const videoId = getCreationCardVideoId(creation);
  if (videoId) return videoId;

  return getCreationCardImageSrc(creation);
};

export const getCreationHref = (
  creation: CreationListItem
): { pathname: string; params: { id: string } } | string | '' => {
  if (creation.alkotasAzonosito) {
    return {
      pathname: '/alkotas/[id]',
      params: { id: creation.alkotasAzonosito },
    };
  }
  if (creation.hivatkozas) {
    return creation.hivatkozas; // external URL
  }
  return '';
};

export const isNonChronologicalData = (creation: CreationListItem): boolean => {
  return creation.labelYear === 1000;
};
