/* eslint-disable react/display-name */
import type { Icon } from '~/src/components/Icon/toIcon';
import type { SelectedAccountConfig } from '~/src/store/lib/selectors';

import { ServiceTypes } from '~/lib/types';
import AmazonIcon from '~/src/components/Icon/AmazonIcon';
import AnghamiIcon from '~/src/components/Icon/AnghamiIcon';
import AppleIcon from '~/src/components/Icon/AppleIcon';
import AudiomackIcon from '~/src/components/Icon/AudiomackIcon';
import AudiusIcon from '~/src/components/Icon/AudiusIcon';
import AwaIcon from '~/src/components/Icon/AwaIcon';
import BandcampIcon from '~/src/components/Icon/BandcampIcon';
import BandsInTownIcon from '~/src/components/Icon/BandsInTownIcon';
import BeatportIcon from '~/src/components/Icon/BeatportIcon';
import BeatstarsIcon from '~/src/components/Icon/BeatstarsIcon';
import BleepIcon from '~/src/components/Icon/BleepIcon';
import BoomplayIcon from '~/src/components/Icon/BoomplayIcon';
import DeezerIcon from '~/src/components/Icon/DeezerIcon';
import DiscordIcon from '~/src/components/Icon/DiscordIcon';
import DubsmashIcon from '~/src/components/Icon/DubsmashIcon';
import FacebookIcon from '~/src/components/Icon/FacebookIcon';
import GaanaIcon from '~/src/components/Icon/GaanaIcon';
import GoogleIcon from '~/src/components/Icon/GoogleIcon';
import IHeartRadioIcon from '~/src/components/Icon/IHeartRadioIcon';
import InstagramIcon from '~/src/components/Icon/InstagramIcon';
import JioSaavnIcon from '~/src/components/Icon/JioSaavnIcon';
import JooxIcon from '~/src/components/Icon/JooxIcon';
import KkBoxIcon from '~/src/components/Icon/KkBoxIcon';
import LastFmIcon from '~/src/components/Icon/LastFmIcon';
import LineMusicIcon from '~/src/components/Icon/LineMusicIcon';
import LinkedInIcon from '~/src/components/Icon/LinkedInIcon';
import LinkIcon from '~/src/components/Icon/LinkIcon';
import MailchimpIcon from '~/src/components/Icon/MailchimpIcon';
import MixcloudIcon from '~/src/components/Icon/MixcloudIcon';
import MoneyIcon from '~/src/components/Icon/MoneyIcon';
import MoraIcon from '~/src/components/Icon/MoraIcon';
import NapsterIcon from '~/src/components/Icon/NapsterIcon';
import OtotoyIcon from '~/src/components/Icon/OtotoyIcon';
import PandoraIcon from '~/src/components/Icon/PandoraIcon';
import PatreonIcon from '~/src/components/Icon/PatreonIcon';
import PayPalIcon from '~/src/components/Icon/PayPalIcon';
import PlayIcon from '~/src/components/Icon/PlayIcon';
import QobuzIcon from '~/src/components/Icon/QobuzIcon';
import RakutenIcon from '~/src/components/Icon/RakutenIcon';
import RecochokuIcon from '~/src/components/Icon/RecochokuIcon';
import RessoIcon from '~/src/components/Icon/RessoIcon';
import SevenDigitalIcon from '~/src/components/Icon/SevenDigitalIcon';
import SnapchatIcon from '~/src/components/Icon/SnapchatIcon';
import SongwhipIcon from '~/src/components/Icon/SongwhipIcon';
import SoundCloudIcon from '~/src/components/Icon/SoundCloudIcon';
import SpotifyIcon from '~/src/components/Icon/SpotifyIcon';
import StarIcon from '~/src/components/Icon/StarIcon';
import TelegramIcon from '~/src/components/Icon/TelegramIcon';
import TidalIcon from '~/src/components/Icon/TidalIcon';
import TikTokIcon from '~/src/components/Icon/TikTokIcon';
import TraxsourceIcon from '~/src/components/Icon/TraxsourceIcon';
import TrillerIcon from '~/src/components/Icon/TrillerIcon';
import TwitchIcon from '~/src/components/Icon/TwitchIcon';
import TwitterIcon from '~/src/components/Icon/TwitterIcon';
import VkIcon from '~/src/components/Icon/VkIcon';
import WynkIcon from '~/src/components/Icon/WynkIcon';
import YandexIcon from '~/src/components/Icon/YandexIcon';
import YouTubeIcon from '~/src/components/Icon/YouTubeIcon';
import YouTubeMusicIcon from '~/src/components/Icon/YouTubeMusicIcon';
import darkTheme from './theme/dark';

export type ServiceDisplayData = {
  name: string;
  urlPattern: RegExp;
  color: string;
  colorInverted?: string;
  Icon: Icon;
  countries?: string[];
};

export const SERVICES: { [key: string]: ServiceDisplayData } = {
  [ServiceTypes.AMAZON_MUSIC]: {
    name: 'Amazon Music',
    urlPattern: /music\.amazon\./,
    color: '#166AB5',
    Icon: AmazonIcon,
  },

  [ServiceTypes.AMAZON]: {
    name: 'Amazon',
    urlPattern: /amazon\./,
    color: '#FF9900',
    Icon: AmazonIcon,
  },

  [ServiceTypes.ANGHAMI]: {
    name: 'Anghami',
    urlPattern: /anghami\./,
    color: '#f300f9',
    Icon: AnghamiIcon,
  },

  [ServiceTypes.AUDIOMACK]: {
    name: 'Audiomack',
    urlPattern: /audiomack\.com/,
    color: '#ffa201',
    Icon: AudiomackIcon,
  },

  [ServiceTypes.AUDIUS]: {
    name: 'Audius',
    urlPattern: /audius\.co/,
    color: '#bd10e0',
    Icon: AudiusIcon,
  },

  [ServiceTypes.AWA]: {
    name: 'AWA',
    urlPattern: /awa\.fm/,
    color: '#000000',
    Icon: AwaIcon,
    countries: ['JP'],
  },

  [ServiceTypes.BANDCAMP]: {
    name: 'Bandcamp',
    urlPattern: /bandcamp\.com/,
    color: '#1DA0C3',
    Icon: BandcampIcon,
  },

  [ServiceTypes.BANDS_IN_TOWN]: {
    name: 'Bandsintown',
    urlPattern: /bandsintown\./,
    color: '#00b4b3',
    Icon: BandsInTownIcon,
  },

  [ServiceTypes.BAJAO]: {
    name: 'Bajao',
    urlPattern: /bajao\.pk/,
    color: '#cf0056',
    Icon: PlayIcon,
    countries: ['PK'],
  },

  [ServiceTypes.BEATSTARS]: {
    name: 'BeatStars',
    urlPattern: /beatstars\./,
    color: '#fe2b0c',
    Icon: BeatstarsIcon,
  },

  [ServiceTypes.BEATPORT]: {
    name: 'Beatport',
    urlPattern: /beatport\./,
    color: '#93d501',
    Icon: BeatportIcon,
  },

  [ServiceTypes.BLEEP]: {
    name: 'Bleep',
    urlPattern: /bleep\./,
    color: '#373641',
    Icon: BleepIcon,
  },

  [ServiceTypes.BOOMPLAY]: {
    name: 'Boomplay',
    urlPattern: /boomplay\./,
    color: '#0052ff',
    Icon: BoomplayIcon,
  },

  [ServiceTypes.CASH_APP]: {
    name: 'Cash App',
    urlPattern: /\bcash\.app/,
    color: '#00cf31',
    Icon: MoneyIcon,
  },

  [ServiceTypes.DISCORD]: {
    name: 'Discord',
    urlPattern: /discord\./,
    color: '#5865f2',
    Icon: DiscordIcon,
  },

  [ServiceTypes.DUBSMASH]: {
    name: 'Dubsmash',
    urlPattern: /dubsmash\./,
    color: '#00acfe',
    Icon: DubsmashIcon,
  },

  [ServiceTypes.DEEZER]: {
    name: 'Deezer',

    // matches deezer.com and deezer.page.link
    urlPattern: /deezer\./,

    color: '#8a22de',
    Icon: DeezerIcon,
  },

  [ServiceTypes.FACEBOOK]: {
    name: 'Facebook',
    urlPattern: /facebook\.com/,
    color: '#3b5998',
    Icon: FacebookIcon,
  },

  [ServiceTypes.GAANA]: {
    name: 'Gaana',
    urlPattern: /gaana\.com/,
    color: '#e72d30',
    Icon: GaanaIcon,
    countries: ['IN', 'NP', 'PK'],
  },

  [ServiceTypes.GOOGLE]: {
    name: 'Google',
    urlPattern: /google\.com/,
    color: '#db3236',
    Icon: GoogleIcon,
  },

  [ServiceTypes.I_HEART_RADIO]: {
    name: 'iHeartRadio',
    urlPattern: /iheart\.com/,
    color: '#c6002b',
    Icon: IHeartRadioIcon,
  },

  [ServiceTypes.INSTAGRAM]: {
    name: 'Instagram',
    urlPattern: /instagram\./,
    color: '#FD1D1D',
    Icon: InstagramIcon,
  },

  [ServiceTypes.ITUNES_STORE]: {
    name: 'iTunes',
    urlPattern: /(apple\.com.+app=itunes|itunes\.apple\.com)/,
    color: '#F9425D',
    Icon: AppleIcon,
  },

  [ServiceTypes.ITUNES]: {
    name: 'Apple Music',
    urlPattern: /music\.apple\.com/,
    color: '#F9425D',
    Icon: AppleIcon,
  },

  [ServiceTypes.JOOX]: {
    name: 'Joox',
    urlPattern: /joox\./,
    color: '#02cb5d',
    Icon: JooxIcon,
  },

  [ServiceTypes.JIO_SAAVN]: {
    name: 'JioSaavn',
    urlPattern: /jiosaavn\./,
    color: '#2bc5b4',
    Icon: JioSaavnIcon,
    countries: ['IN', 'NP', 'PK'],
  },

  [ServiceTypes.KKBOX]: {
    name: 'KKBOX',
    urlPattern: /kkbox\.(fm|com)/,
    color: '#09CEF6',
    Icon: KkBoxIcon,
  },

  [ServiceTypes.LAST_FM]: {
    name: 'Last.fm',
    urlPattern: /last\.fm/,
    color: '#b90000',
    Icon: LastFmIcon,
  },

  [ServiceTypes.LINE_MUSIC]: {
    name: 'Line Music',
    urlPattern: /music\.line\.me/,
    color: '#0ad269',
    Icon: LineMusicIcon,
    countries: ['JP'],
  },

  [ServiceTypes.LINKED_IN]: {
    name: 'LinkedIn',
    urlPattern: /\blinkedin\./,
    color: '#0277b5',
    Icon: LinkedInIcon,
  },

  [ServiceTypes.MAILCHIMP]: {
    name: 'Mailchimp',
    urlPattern: /mailchimp.|eepurl./,
    color: '#007c89',
    Icon: MailchimpIcon,
  },

  [ServiceTypes.MORA]: {
    name: 'Mora',
    urlPattern: /mora\.jp/,
    color: '#08112e',
    Icon: MoraIcon,
  },

  // REVIEW: maybe not consumer facing
  [ServiceTypes.MIXCLOUD]: {
    name: 'Mixcloud',
    urlPattern: /mixcloud\./,
    color: '#586573',
    Icon: MixcloudIcon,
  },

  [ServiceTypes.NAPSTER]: {
    name: 'Napster',
    urlPattern: /napster\.com/,
    color: '#5db8f6',
    Icon: NapsterIcon,
  },

  [ServiceTypes.OTOTOY]: {
    name: 'OTOTOY',
    urlPattern: /ototoy\.jp/,
    color: '#2AB002',
    Icon: OtotoyIcon,
    countries: ['JP'],
  },

  [ServiceTypes.PANDORA]: {
    name: 'Pandora',
    urlPattern: /pandora\./,
    color: '#2296f3',
    Icon: PandoraIcon,
    countries: ['US'],
  },

  [ServiceTypes.PATREON]: {
    name: 'Patreon',
    urlPattern: /patreon.com/,
    color: '#ff424d',
    Icon: PatreonIcon,
  },

  [ServiceTypes.PAYPAL]: {
    name: 'PayPal',
    urlPattern: /paypal\./,
    color: '#253B80',
    Icon: PayPalIcon,
  },

  [ServiceTypes.QOBUZ]: {
    name: 'Qobuz',
    urlPattern: /qobuz\./,
    color: '#0070EF',
    Icon: QobuzIcon,
  },

  [ServiceTypes.RAKUTEN]: {
    name: 'Rakuten',
    urlPattern: /rakuten\./,
    color: '#bf0000',
    Icon: RakutenIcon,
  },

  [ServiceTypes.RECOCHOKU]: {
    name: 'RecoChoku',
    urlPattern: /recochoku\.jp/,
    color: '#F40071',
    Icon: RecochokuIcon,
  },

  [ServiceTypes.RESSO]: {
    name: 'Resso',
    urlPattern: /resso\./,
    color: '#f3005b',
    Icon: RessoIcon,
  },

  [ServiceTypes.REVERB_NATION]: {
    name: 'ReverbNation',
    urlPattern: /reverbnation\./,
    color: '#e43526',
    Icon: StarIcon,
  },

  [ServiceTypes.SEVEN_DIGITAL]: {
    name: '7digital',
    urlPattern: /7digital\.com/,
    Icon: SevenDigitalIcon,
    color: '#3254F0',
  },

  [ServiceTypes.SNAPCHAT]: {
    name: 'Snapchat',
    urlPattern: /snapchat\.com/,
    Icon: SnapchatIcon,

    // TODO: this color is barely visible on white
    color: '#ffff00',
  },

  [ServiceTypes.SOUND_CLOUD]: {
    name: 'SoundCloud',

    // matches soundcloud.com and soundcloud.app.goo.gl
    urlPattern: /soundcloud\./,

    color: '#FC3D0A',
    Icon: SoundCloudIcon,
  },

  [ServiceTypes.SPOTIFY]: {
    name: 'Spotify',
    urlPattern: /spotify\.com/,
    color: '#1DB954',
    Icon: SpotifyIcon,
  },

  [ServiceTypes.SONGWHIP]: {
    name: 'Songwhip',
    urlPattern: /songwhip\.com|sng\.to|orch\.stream/,
    color: darkTheme.accentColor,
    colorInverted: '#fff',
    Icon: SongwhipIcon,
  },

  [ServiceTypes.TELEGRAM]: {
    name: 'Telegram',
    urlPattern: /telegram\.|t\.me/,
    color: '#0088cc',
    Icon: TelegramIcon,
  },

  [ServiceTypes.TIDAL]: {
    name: 'Tidal',
    urlPattern: /tidal\.com/,
    color: '#3b3573',
    colorInverted: '#fff',
    Icon: TidalIcon,
  },

  [ServiceTypes.TIKTOK]: {
    name: 'TikTok',
    urlPattern: /tiktok\.com/,
    color: '#ff004f',
    Icon: TikTokIcon,
  },

  [ServiceTypes.TRILLER]: {
    name: 'Triller',
    urlPattern: /triller\./,
    color: '#ff3960',
    Icon: TrillerIcon,
  },

  [ServiceTypes.TWITCH]: {
    name: 'Twitch',
    urlPattern: /twitch\./,
    color: '#6441a4',
    Icon: TwitchIcon,
  },

  [ServiceTypes.TWITTER]: {
    name: 'X',
    urlPattern: /twitter\.com|\/\/x\.com|\/\/t\.co/,
    color: '#1da1f2',
    colorInverted: '#fff',
    Icon: TwitterIcon,
  },

  [ServiceTypes.TRAXSOURCE]: {
    name: 'Traxsource',
    urlPattern: /traxsource\./,
    color: '#3fa0ff',
    Icon: TraxsourceIcon,
  },

  [ServiceTypes.YOU_TUBE_MUSIC]: {
    name: 'YouTube Music',
    urlPattern: /music\.youtube\.com/,
    color: '#FF0000',
    Icon: YouTubeMusicIcon,
  },

  [ServiceTypes.YOU_TUBE]: {
    name: 'YouTube',

    // match youtube.com and youtu.be but not music.youtube.com
    urlPattern: /^(?!.*\.?music\.)(.*\.?)(youtube\.|youtu\.be)/,

    color: '#FF0000',
    Icon: YouTubeIcon,
  },

  [ServiceTypes.VK]: {
    name: 'VK',

    // match vk.com, vk.ru, foo.vk.com, https://vk.com
    urlPattern: /\bvk\./,

    color: '#0576fe',
    Icon: VkIcon,
  },

  [ServiceTypes.WYNK]: {
    name: 'Wynk',
    urlPattern: /wynk\./,
    color: '#fa3335',
    Icon: WynkIcon,
  },

  [ServiceTypes.YANDEX]: {
    name: 'Yandex',
    urlPattern: /yandex\./,
    color: '#ff3333',
    Icon: YandexIcon,
    countries: [
      'RU',
      'AM',
      'AZ',
      'BY',
      'GE',
      'KZ',
      'KG',
      'MD',
      'TJ',
      'TM',
      'UZ',
    ],
  },
};

const getServiceDisplayData = (
  serviceKey: string | undefined
): (ServiceDisplayData & { key: ServiceTypes }) | undefined => {
  if (!serviceKey) return undefined;

  const service = SERVICES[serviceKey];
  if (!service) return undefined;

  return { ...service, key: serviceKey as ServiceTypes };
};

export interface ResolvedServiceData
  extends Omit<ServiceDisplayData, 'color' | 'urlPattern'> {
  key: ServiceTypes | string;
  type?: ServiceTypes;
  url?: string;
  color?: string;
  match: boolean;
}

export const resolveServiceDataFromUrl = (
  url: string,
  customBrands: SelectedAccountConfig['brands'] = []
): ResolvedServiceData => {
  for (let i = 0; i < customBrands.length; i++) {
    const { urlPattern, name, Icon } = customBrands[i];

    if (new RegExp(urlPattern).test(url)) {
      return {
        key: urlPattern,
        match: true,
        name,
        Icon,
      };
    }
  }

  for (const key in SERVICES) {
    const service = SERVICES[key];
    const isMatch = service.urlPattern.test(url);

    if (isMatch) {
      return {
        key: key as ServiceTypes,
        type: key as ServiceTypes,
        match: true,
        ...service,
      };
    }
  }

  // if the url is not known then we attempt to parse a name
  // from the hostname (eg. facebook.com => 'facebook')
  const name = resolveNameFromUrl(url) || 'Unknown';

  return {
    name,
    Icon: LinkIcon,
    key: name.toLowerCase(),
    color: darkTheme.accentColor,
    match: false,
  };
};

// WARN: this don't handle hostnames w/ subdomains well
// (eg. open.spotify.com => 'Open')
const resolveNameFromUrl = (url: string) => {
  try {
    const { host } = new URL(url);

    // take the longest part of the hostname
    const result = host
      .split('.')
      .sort((a, b) => (a.length > b.length ? -1 : 1))[0];

    return result[0].toUpperCase() + result.slice(1);
  } catch (e) {}
};

export default getServiceDisplayData;
