'use client'

import { useState, useMemo, memo } from 'react'

import type { MapLegendProps, LegendItem } from '../types'

const positionStyles: Record<string, React.CSSProperties> = {
  'top-left': { top: 10, left: 10 },
  'top-right': { top: 10, right: 10 },
  'bottom-left': { bottom: 10, left: 10 },
  'bottom-right': { bottom: 10, right: 10 },
}

function LegendIcon({
  type,
  color,
}: {
  type: LegendItem['type']
  color?: string
}) {
  const fill = color || '#888'

  switch (type) {
    case 'circle':
      return (
        <svg width="16" height="16" viewBox="0 0 16 16">
          <circle cx="8" cy="8" r="6" fill={fill} />
        </svg>
      )
    case 'line':
      return (
        <svg width="16" height="16" viewBox="0 0 16 16">
          <line
            x1="0"
            y1="8"
            x2="16"
            y2="8"
            stroke={fill}
            strokeWidth="3"
            strokeLinecap="round"
          />
        </svg>
      )
    case 'fill':
      return (
        <svg width="16" height="16" viewBox="0 0 16 16">
          <rect x="1" y="1" width="14" height="14" fill={fill} rx="2" />
        </svg>
      )
    case 'symbol':
    default:
      return (
        <svg width="16" height="16" viewBox="0 0 16 16">
          <path
            d="M8 1l2.5 5 5.5.8-4 3.9.9 5.3L8 13.5l-4.9 2.5.9-5.3-4-3.9 5.5-.8z"
            fill={fill}
          />
        </svg>
      )
  }
}

function MapLegendComponent({
  items,
  position = 'bottom-right',
  title,
  collapsible = false,
  defaultCollapsed = false,
  className = '',
  style,
  onItemClick,
}: MapLegendProps) {
  const [collapsed, setCollapsed] = useState(defaultCollapsed)

  const containerStyle = useMemo<React.CSSProperties>(
    () => ({
      position: 'absolute',
      ...positionStyles[position],
      backgroundColor: 'white',
      borderRadius: 8,
      boxShadow: '0 2px 8px rgba(0,0,0,0.15)',
      padding: collapsed ? 8 : 12,
      minWidth: collapsed ? 'auto' : 120,
      zIndex: 1,
      ...style,
    }),
    [position, collapsed, style]
  )

  const headerStyle = useMemo<React.CSSProperties>(
    () => ({
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
      gap: 8,
      cursor: collapsible ? 'pointer' : 'default',
      fontWeight: 600,
      fontSize: 12,
      color: '#333',
      marginBottom: collapsed ? 0 : 8,
    }),
    [collapsible, collapsed]
  )

  const itemStyle = useMemo<React.CSSProperties>(
    () => ({
      display: 'flex',
      alignItems: 'center',
      gap: 8,
      padding: '4px 0',
      fontSize: 12,
      color: '#666',
      cursor: onItemClick ? 'pointer' : 'default',
    }),
    [onItemClick]
  )

  const handleHeaderClick = collapsible ? () => setCollapsed(!collapsed) : undefined

  return (
    <div className={className} style={containerStyle}>
      {(title || collapsible) && (
        <div style={headerStyle} onClick={handleHeaderClick}>
          {title && <span>{title}</span>}
          {collapsible && (
            <svg
              width="12"
              height="12"
              viewBox="0 0 12 12"
              style={{
                transform: collapsed ? 'rotate(-90deg)' : 'rotate(0deg)',
                transition: 'transform 0.2s',
              }}
            >
              <path d="M3 4.5L6 7.5L9 4.5" stroke="#666" fill="none" />
            </svg>
          )}
        </div>
      )}
      {!collapsed && (
        <div>
          {items.map((item) => (
            <div
              key={item.id}
              style={{
                ...itemStyle,
                opacity: item.visible === false ? 0.5 : 1,
              }}
              onClick={onItemClick ? () => onItemClick(item) : undefined}
            >
              {item.icon || <LegendIcon type={item.type} color={item.color} />}
              <span>{item.label}</span>
            </div>
          ))}
        </div>
      )}
    </div>
  )
}

export const MapLegend = memo(MapLegendComponent)
