import { CategoryGlyph } from '@/site/components/art/category-icon';
import { about, depth, turn } from '@/site/components/art/kit';
import type { Accent } from '@/site/types';

type AtlasArea = { key: string; icon: string; accent: Accent };

type AtlasArtProps = {
    /** Every area, in the order of the page. */
    areas: AtlasArea[];
    /** The area to light up (the jump link under the pointer), if any. */
    active?: string | null;
    /** Pointer moved onto / off a mark. */
    onHover?: (key: string | null) => void;
};

const SIZE = 440;
const CENTER = SIZE / 2;

const round = (value: number) => Math.round(value * 10) / 10;

/**
 * Drawing of the treatments page: every area of expertise as a mark on one
 * spiral that starts at the centre — the departments first, in their order.
 * The spiral draws itself, the marks follow one by one, each in its own
 * colour, and the one whose jump link is under the pointer lights up.
 *
 * The marks are a pointer-only shortcut to the area's block further down
 * (the same links exist as real ones beside the drawing), so they are
 * hidden from assistive technology and the keyboard.
 */
export function AtlasArt({ areas, active, onHover }: AtlasArtProps) {
    const step = Math.min(13, 130 / Math.max(1, areas.length - 1));

    /** A point of the spiral; whole numbers are the marks. */
    const at = (index: number) => {
        const angle = ((index * 47 - 100) * Math.PI) / 180;
        const radius = 62 + index * step;

        return {
            x: round(CENTER + Math.cos(angle) * radius),
            y: round(CENTER + Math.sin(angle) * radius),
        };
    };

    // The line through the marks, a little past both ends.
    const points: string[] = [];

    for (let index = -1.4; index <= areas.length - 0.4; index += 0.2) {
        const { x, y } = at(index);

        points.push(`${x} ${y}`);
    }

    return (
        <svg
            viewBox={`0 0 ${SIZE} ${SIZE}`}
            aria-hidden="true"
            className="art size-full"
        >
            {/* Quiet rings behind, turning */}
            <g className="so" data-depth="" style={depth(-1)}>
                <g className="art-turn" style={about(CENTER, CENTER)}>
                    {[76, 142, 206].map((radius) => (
                        <circle
                            key={radius}
                            cx={CENTER}
                            cy={CENTER}
                            r={radius}
                            className="ln"
                            opacity="0.3"
                            strokeDasharray="1 10"
                        />
                    ))}
                </g>
            </g>

            <g data-depth="" style={depth(1)}>
                <path
                    pathLength={1}
                    className="ln sk atlas-line"
                    opacity="0.6"
                    d={`M${points.join('L')}`}
                />
                {/* Where it starts */}
                <g className="so" style={turn(2)}>
                    <circle
                        cx={CENTER}
                        cy={CENTER}
                        r="8"
                        className="ac box animate-pulse-ring"
                    />
                </g>
                <circle cx={CENTER} cy={CENTER} r="4.5" className="af sf" />
            </g>

            <g data-depth="" style={depth(2)}>
                {areas.map((area, index) => {
                    const { x, y } = at(index);

                    return (
                        <a
                            key={area.key}
                            href={`#${area.key}`}
                            tabIndex={-1}
                            data-accent={area.accent}
                            data-active={area.key === active ? '' : undefined}
                            className="atlas-mark"
                            style={turn(index)}
                            onPointerEnter={() => onHover?.(area.key)}
                            onPointerLeave={() => onHover?.(null)}
                        >
                            <g transform={`translate(${x} ${y})`}>
                                {/* The entrance on the group, the hover on the disc. */}
                                <g className="sf">
                                    <circle r="22" className="atlas-disc box" />
                                </g>
                                <g
                                    className="atlas-glyph"
                                    transform="translate(-13 -13) scale(0.542)"
                                >
                                    <CategoryGlyph name={area.icon} />
                                </g>
                            </g>
                        </a>
                    );
                })}
            </g>
        </svg>
    );
}
