import type { ReactNode } from 'react';
import { turn } from '@/site/components/art/kit';
import { cx } from '@/site/lib/cx';

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

/** The ticks of the gauge: seven, a sixth of a half turn apart. */
const ticks = Array.from({ length: 7 }, (_, index) => {
    const angle = Math.PI - (index * Math.PI) / 6;
    const [cos, sin] = [Math.cos(angle), Math.sin(angle)];

    return `M${round(60 + 46 * cos)} ${round(86 - 46 * sin)}L${round(60 + 51 * cos)} ${round(86 - 51 * sin)}`;
}).join('');

/**
 * Small line drawings of the four working principles (about page), in the
 * order of the dictionary's `about.principles`, on a 120×120 grid:
 *   1  a path of one's own among the usual ones (individual planning),
 *   2  a sheet read through a lens (open information),
 *   3  a gauge resting well short of its end (a measured approach),
 *   4  a circle that comes round again, its check-ups marked (continuity).
 * They draw themselves when their card comes into view; the card's text
 * says the rest, so they are decorative.
 */
const drawings: ReactNode[] = [
    <>
        <path
            pathLength={1}
            className="ln sk"
            opacity="0.6"
            d="M10 46c22-12 44 12 100 0"
        />
        <path
            pathLength={1}
            className="ln sk"
            style={turn(1)}
            d="M10 66c22-12 44 12 100 0"
        />
        <path
            pathLength={1}
            className="ln sk"
            style={turn(2)}
            opacity="0.6"
            d="M10 86c22-12 44 12 100 0"
        />
        <path
            pathLength={1}
            className="acf sk"
            strokeWidth="2.2"
            style={turn(5)}
            d="M10 66c20-11 34 2 50-8s24-28 44-32"
        />
        <circle cx="104" cy="26" r="11" className="acf sf" style={turn(11)} />
        <circle cx="104" cy="26" r="4.5" className="aff sf" style={turn(10)} />
    </>,
    <>
        <rect
            pathLength={1}
            x="18"
            y="14"
            width="62"
            height="86"
            rx="8"
            className="ln sk"
        />
        <path
            pathLength={1}
            className="ln sk"
            style={turn(3)}
            d="M30 34h38M30 48h38M30 62h20"
        />
        <circle
            pathLength={1}
            cx="78"
            cy="76"
            r="21"
            className="pane acf sk"
            style={turn(6)}
        />
        <path
            pathLength={1}
            className="acf sk"
            strokeWidth="2.2"
            style={turn(9)}
            d="M93 91l15 15"
        />
        <path
            pathLength={1}
            className="acf sk"
            style={turn(11)}
            d="M67 72h22M67 82h13"
        />
    </>,
    <>
        <path pathLength={1} className="ln sk" d="M18 86a42 42 0 0 1 84 0" />
        <path className="ln so" style={turn(3)} d={ticks} />
        <path
            pathLength={1}
            className="acf sk"
            strokeWidth="2.6"
            style={turn(5)}
            d="M18 86a42 42 0 0 1 21-36.4"
        />
        <path
            pathLength={1}
            className="acf sk"
            strokeWidth="2.2"
            style={turn(8)}
            d="M60 86 43 56.6"
        />
        <circle cx="60" cy="86" r="4.5" className="aff sf" style={turn(10)} />
        <path
            pathLength={1}
            className="ln sk"
            style={turn(2)}
            opacity="0.6"
            d="M26 102h68"
        />
    </>,
    <>
        <path
            pathLength={1}
            className="acf sk"
            strokeWidth="2.2"
            d="M93.5 44.2A38 38 0 1 0 97.8 66"
        />
        <path
            pathLength={1}
            className="acf sk"
            strokeWidth="2.2"
            style={turn(7)}
            d="M83.5 40l10.5 4.5 2-11.5"
        />
        <circle
            cx="60"
            cy="62"
            r="24"
            className="ln so"
            style={turn(3)}
            strokeDasharray="1 6"
        />
        {[
            [27.1, 81],
            [27.1, 43],
            [60, 100],
        ].map(([x, y], index) => (
            <circle
                key={index}
                cx={x}
                cy={y}
                r="5"
                className="pane acf sf"
                style={turn(9 + index * 2)}
            />
        ))}
        <circle cx="60" cy="62" r="3.6" className="aff sf" style={turn(4)} />
    </>,
];

type PrincipleArtProps = {
    /** Which principle, from 0. */
    index: number;
    className?: string;
};

export function PrincipleArt({ index, className }: PrincipleArtProps) {
    return (
        <svg
            viewBox="0 0 120 120"
            aria-hidden="true"
            className={cx('art', className)}
        >
            {drawings[index % drawings.length]}
        </svg>
    );
}
