import {
  URL_AMAZON_PODCAST_REG,
  URL_APPLE_PODCAST_REG,
  URL_AUDIBLE_PODCAST_REG,
  URL_REG,
  URL_YOUTUBE_CHANNEL_REG,
  URL_YOUTUBE_PLAYLIST_REG,
  URL_YOUTUBE_REG,
} from "constants/regexp";
import { isVideo } from "is-youtube-url";
import { SocialProfileLinkTypes } from "redux/User/types";
import spotifyUri from "spotify-uri";

export const validateIndividualYoutube = (value: string) => {
  if (!value) {
    return "Please enter a YouTube link";
  }
  if (!URL_YOUTUBE_REG.test(value)) {
    return "Please enter a valid YouTube link";
  }
  if (!isVideo(value)) {
    return "Please enter a YouTube link to an individual video";
  }
  return;
};
export const validateCollectionYoutube = (value: string) => {
  if (!value) {
    return "Please enter a YouTube link";
  }
  if (!URL_YOUTUBE_REG.test(value)) {
    return "Please enter a valid YouTube link";
  }

  // SB-426: There is a checking in komi-server to differentiate if the link is a playlist / channel / invalid
  // So only checking the basic regular expression here should be enough

  // if (
  //   !URL_YOUTUBE_CHANNEL_REG.test(value) &&
  //   !URL_YOUTUBE_PLAYLIST_REG.test(value)
  // ) {
  //   return "Please enter a YouTube link to a Channel or Playlist";
  // }
  return;
};

export const isYoutubeUrlPlaylist = (value: string) => {
  return URL_YOUTUBE_PLAYLIST_REG.test(value)
}

export enum ValidatePodcast {
  Success = "SUCCESS",
  InvalidApplePodcast = "INVALID_APPLE_PODCAST",
  InvalidSpotifyPodcast = "INVALID_SPOTIFY_PODCAST",
  DifferentPodcastShow = "DIFFERENT_PODCAST_SHOW",
  InvalidQuery = "URL_CANT_BE_NULL_OR_EMPTY",
  APPLE_PODCAST_CONNECTION_FAILED = "APPLE_PODCAST_CONNECTION_FAILED",
  SPOTIFY_CONNECTION_FAILED = "SPOTIFY_CONNECTION_FAILED",
  SPOTIFY_SHOW_DO_NOT_EXISTS = "SPOTIFY_PODCAST_SHOW_DO_NOT_EXISTS",
  APPLE_PODCAST_SHOW_DO_NOT_EXISTS = "APPLE_PODCAST_SHOW_DO_NOT_EXISTS",
  DIFFERENT_PODCAST_NAME = "DIFFERENT_PODCAST_NAME",
  DIFFERENT_PODCAST_AUTHOR = "DIFFERENT_PODCAST_AUTHOR",
  DIFFERENT_PODCAST_EPISODES = "DIFFERENT_PODCAST_EPISODES",
}
export const validatePodcastByType = (type: string) => (value: string) => {
  if (!value) {
    return;
  }
  if (type === SocialProfileLinkTypes.SPOTIFY) {
    try {
      const uri: any = spotifyUri.parse(value);
      if (uri.type !== "show") {
        return "Please enter a valid URL";
      }
      return;
    } catch (error) {
      return "Please enter a valid URL";
    }
  }
  if (
    type === SocialProfileLinkTypes.APPLE_PODCAST &&
    !URL_APPLE_PODCAST_REG.test(value)
  ) {
    return "Please enter a valid URL";
  }

  return;
};
export const validateEpisodeByType = (type: string) => (value: string) => {
  // if (!value) {
  //   return "Please enter a valid URL";
  // }
  // if (type === SocialProfileLinkTypes.SPOTIFY) {
  //   try {
  //     const uri: any = spotifyUri.parse(value);
  //     if (uri.type !== "episode") {
  //       return "Please enter a valid URL";
  //     }
  //     return;
  //   } catch (error) {
  //     return "Please enter a valid URL";
  //   }
  // }
  // if(type === SocialProfileLinkTypes.APPLE_PODCAST && !URL_APPLE_PODCAST_REG.test(value)) {
  //   return "Please enter a valid URL"
  // }
  // if(type === SocialProfileLinkTypes.AMAZON_MUSIC && !URL_AMAZON_PODCAST_REG.test(value)) {
  //   return "Please enter a valid URL"
  // }
  // if(type === SocialProfileLinkTypes.AUDIBLE && !URL_AUDIBLE_PODCAST_REG.test(value)) {
  //   return "Please enter a valid URL"
  // }
  if (!URL_REG.test(value)) {
    return "Please enter a valid URL";
  }

  return;
};
