import PrefetchLink from '@/components/PrefetchLink'
import { LIGHT_COLOUR_SHADE, DARK_COLOUR_SHADE, TAG_COLOUR } from '@/consts'
import { cn } from '@/lib/utils'

import type { ComponentProps, FC, HTMLAttributes } from 'react'

import type { Tag } from '@/types'

type CommonProps = { 
    tag:        Tag
    nestLevel?: number
    withWiki?:  boolean
    withCount?: boolean }

type PropsWithLink =
    & CommonProps
    & { linkFlg?: true }
    & Partial<ComponentProps<typeof PrefetchLink>>

type PropsWithoutLink =
    & CommonProps
    & { linkFlg: false }
    & Partial<HTMLAttributes<HTMLSpanElement>>

type Props =
    | PropsWithLink
    | PropsWithoutLink


const TagLink: FC<Props> = ({ tag,
		   nestLevel = 0,
		   linkFlg   = true,
		   withWiki  = true,
		   withCount = true,
		   ...props }) => {
  const spanClass = cn (
      `text-${ TAG_COLOUR[tag.category] }-${ LIGHT_COLOUR_SHADE }`,
      `dark:text-${ TAG_COLOUR[tag.category] }-${ DARK_COLOUR_SHADE }`)
  const linkClass = cn (
      spanClass,
      `hover:text-${ TAG_COLOUR[tag.category] }-${ LIGHT_COLOUR_SHADE - 200 }`,
      `dark:hover:text-${ TAG_COLOUR[tag.category] }-${ DARK_COLOUR_SHADE - 200 }`)

  return (
      <>
	{(linkFlg && withWiki) && (
	    <span className="mr-1">
	      {(tag.materialId != null || tag.hasWiki || tag.hasDeerjikists)
	       ? (
		  tag.materialId == null && !(tag.hasDeerjikists)
		  ? (
		      <PrefetchLink
			  to={`/wiki/${ encodeURIComponent (tag.name) }`}
			  className={linkClass}>
			?
		      </PrefetchLink>)
		  : (
		      tag.materialId != null
		      ? (
			  <PrefetchLink
			      to={`/materials/${ tag.materialId }`}
			      className={linkClass}>
			    ?
			  </PrefetchLink>)
		      : (
			  <PrefetchLink
			      to={`/tags/${ tag.id }/deerjikists`}
			      className={linkClass}>
			    ?
			  </PrefetchLink>)))
	       : (
		  ['character', 'material'].includes (tag.category)
		  ? (
		      <PrefetchLink
			  to={`/materials/new?tag=${ encodeURIComponent (tag.name) }`}
			  className="animate-[wiki-blink_.25s_steps(2,end)_infinite]
				     dark:animate-[wiki-blink-dark_.25s_steps(2,end)_infinite]"
			  title={`${ tag.name } 素材情報が存在しません．`}>
			!
		      </PrefetchLink>)
		  : (
		      tag.category === 'deerjikist'
		      ? (
			  <PrefetchLink
			      to={`/tags/${ tag.id }/deerjikists`}
			      className="animate-[wiki-blink_.25s_steps(2,end)_infinite]
					 dark:animate-[wiki-blink-dark_.25s_steps(2,end)_infinite]"
			      title={`${ tag.name } に関する情報が存在しません．`}>
			    !
			  </PrefetchLink>)
		      : (
			  <PrefetchLink
			      to={`/wiki/${ encodeURIComponent (tag.name) }`}
			      className="animate-[wiki-blink_.25s_steps(2,end)_infinite]
					 dark:animate-[wiki-blink-dark_.25s_steps(2,end)_infinite]"
			      title={`${ tag.name } Wiki が存在しません．`}>
			    !
			  </PrefetchLink>)))}
	    </span>)}
	{nestLevel > 0 && (
	    <span
		className="ml-1 mr-1"
		style={{ paddingLeft: `${ (nestLevel - 1) }rem` }}>
	      ↳
	    </span>)}
	{tag.matchedAlias != null && (
	    <>
	      <span className={spanClass} {...props}>
		{tag.matchedAlias}
	      </span>
	      <> → </>
	    </>)}
	{linkFlg
	 ? (
	    <PrefetchLink
		to={`/posts?${ (new URLSearchParams ({ tags: tag.name })).toString () }`}
		className={linkClass}
		{...props}>
	      {tag.name}
	    </PrefetchLink>)
	 : (
	    <span className={spanClass}
		  {...props}>
	      {tag.name}
	    </span>)}
	{withCount && (
	    <span className="ml-1">{tag.postCount}</span>)}
      </>)
}

export default TagLink
