'use client'

import {Box, IconButton} from '@mui/material'
import PropTypes from 'prop-types'
import {usePathname} from 'next/navigation'

import {
  Tidal,
  Spotify,
  Deezer,
  Soundcloud,
  Applemusic,
  Peertube
} from '@icons-pack/react-simple-icons'
import {useEffect, useState} from 'react'

const kouteyAchteyIcons = {
  Tidal: <Tidal />,
  Deezer: <Deezer />,
  Spotify: <Spotify />,
  Soundcloud: <Soundcloud />,
  Applemusic: <Applemusic />
}

function RannIframe({plateforme, url, titre, isMobile}) {
  let src = ''
  switch (plateforme) {
    case 'Tidal': {
      const trackArray = url.split('/')
      const trackId = trackArray[trackArray.length - 1]
      src = `https://embed.tidal.com/tracks/${trackId}?disableAnalytics=true`

      break
    }

    case 'Deezer': {
      const trackArray = url.split('/')
      const trackId = trackArray[trackArray.length - 1]
      src = `https://widget.deezer.com/widget/auto/track/${trackId}?tracklist=false`

      break
    }

    case 'Spotify': {
      const trackArray = url.split('/')
      const trackId = trackArray[trackArray.length - 1]
      src = `https://open.spotify.com/embed/track/${trackId}`

      break
    }

    case 'Soundcloud': {
      src = `https://w.soundcloud.com/player/?url=${url}`

      break
    }

    case 'Applemusic': {
      const embedUrl = url.replace('//music', '//embed.music')
      src = embedUrl

      break
    }
  // No default
  }

  return (
    <div style={{marginTop: 5}}>
      {plateforme === 'Tidal' && (
        <iframe
          src={src}
          title={titre}
          allowFullScreen='allowfullscreen'
          frameBorder='0'
          width={`${isMobile ? '100%' : '50%'}`}
          height='96px'
          sandbox='allow-same-origin allow-scripts allow-popups'
        />
      )}

      {plateforme === 'Gade' && (
        <iframe
          src={`${url}?subtitle=fr&amp;warningTitle=0&amp;peertubeLink=0`}
          allowFullScreen='allowfullscreen'
          title={titre}
          frameBorder='0'
          height={isMobile ? '' : '315'}
          width={isMobile ? '' : '560'}
          sandbox='allow-same-origin allow-scripts allow-popups'
        />
      )}

      {plateforme === 'Deezer' && (
        <iframe
          title={titre || 'deezer-widget'}
          frameBorder='0'
          src={src}
          allow='encrypted-media; clipboard-write'
          allowtransparency='true'
          sandbox='allow-same-origin allow-scripts allow-popups'
        />
      )}

      {plateforme === 'Spotify' && (
        <iframe
          src={src}
          title={titre}
          width={`${isMobile ? '100%' : '50%'}`}
          height='80'
          frameBorder='0'
          allowtransparency='true'
          allow='encrypted-media'
          sandbox='allow-same-origin allow-scripts allow-popups'
        />
      )}

      {plateforme === 'Soundcloud' && (
        <iframe
          src={src}
          title={titre}
          width={`${isMobile ? '100%' : '50%'}`}
          height='166'
          scrolling='no'
          frameBorder='no'
          sandbox='allow-same-origin allow-scripts allow-popups'
        />
      )}

      {plateforme === 'Applemusic' && (
        <iframe
          src={src}
          title={titre}
          width={`${isMobile ? '100%' : '50%'}`}
          height='175'
          frameBorder='0'
          allowtransparency='true'
          allow='encrypted-media'
          sandbox='allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation'
        />
      )}
    </div>
  )
}

function EntegreMizik({parole, isMobile}) {
  const [chwaMizik, meteChwaMizik] = useState(null)
  const pathname = usePathname()
  const {streamAudio, slug, okiMizikID, gadeEmbed, titre} = parole

  const filteredKouteyAchtey = streamAudio.filter(({plateforme}) => plateforme === 'Tidal' || plateforme === 'Spotify' || plateforme === 'Deezer' || plateforme === 'Soundcloud' || plateforme === 'Applemusic')

  const handleClick = (plateforme, url, titre = null) => {
    if (chwaMizik && chwaMizik.plateforme === plateforme) {
      return meteChwaMizik(null)
    }

    meteChwaMizik({plateforme, url, titre})
  }

  useEffect(() => {
    if (pathname === `/paroles/${slug}`) {
      meteChwaMizik(null)
    }
  }, [pathname, slug])

  return (
    <Box align='center'>
      {!okiMizikID && filteredKouteyAchtey.map(({id, plateforme, url}) => (
        <IconButton
          key={id}
          aria-label='player'
          size='large'
          onClick={() => handleClick(plateforme, url, titre)}
        >
          {kouteyAchteyIcons[plateforme]}
        </IconButton>
      ))}

      {!okiMizikID && gadeEmbed && (
        <IconButton
          aria-label='player'
          size='large'
          onClick={() => handleClick('Gade', gadeEmbed, titre)}
        >
          <Peertube />
        </IconButton>
      )}

      {chwaMizik && (
        <RannIframe plateforme={chwaMizik.plateforme} url={chwaMizik.url} titre={chwaMizik.titre} isMobile={isMobile} />
      )}
    </Box>
  )
}

EntegreMizik.propTypes = {
  parole: PropTypes.object.isRequired,
  isMobile: PropTypes.bool.isRequired
}

RannIframe.propTypes = {
  plateforme: PropTypes.string.isRequired,
  url: PropTypes.string.isRequired,
  titre: PropTypes.string,
  isMobile: PropTypes.bool.isRequired
}

RannIframe.defaultProp = {
  titre: null
}

export default EntegreMizik
