<?php

namespace EasyLinkAutomations\Helpers;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Builds the EasyLink Automations logo mark as inline SVG / data URIs.
 *
 * The path data is shared between the monochrome admin menu icon and the
 * full-color mark used on the app loading screen, so it lives in one place.
 */
final class BrandHelper
{
    public const GRADIENT_FROM = '#635cf0';

    public const GRADIENT_TO = '#3b30da';

    /**
     * Bounding box of the mark once TRANSFORM has been applied.
     */
    private const VIEW_BOX = '0 0 794 639.86';

    private const TRANSFORM = 'translate(-142 -179.14)';

    /** The "E" letterform, including the puzzle notch. */
    private const PATH_E = 'M294.23,545.17H432.4l12.72-35.08c-46.37,23.67-35.15-56.2,8.14-22.45l12.24-33.75H294.23V323.62H541.62L598.7,179.14H142V819H598.7V695.16H294.23Z';

    /** The "A" letterform. */
    private const PATH_A = 'M936,819,631.53,179.14,574.44,323.62q30,65.14,60,130.27H520.89'
        . 'c-7.79,21.14-11.21,34.23-12.6,34.2-43.48-34.2-54.7,45.67-8.84,24l-12.21,33.12'
        . 'H676.57Q739.68,682.09,802.79,819Z';

    /**
     * Single-color mark, for contexts that recolor or dim the icon
     * (the WordPress admin menu applies its own opacity).
     *
     * @param string $fill Any CSS color. Defaults to the admin menu icon grey.
     */
    public static function monoSvg(string $fill = '#a7aaad'): string
    {
        return self::open()
            . '<g fill="' . $fill . '">'
            . self::path(self::PATH_E)
            . self::path(self::PATH_A)
            . '</g>'
            . '</svg>';
    }

    /**
     * Full-color mark using the brand gradient.
     */
    public static function gradientSvg(): string
    {
        return self::open()
            . '<defs>'
            . self::gradient('easylink-brand-e', '370.35', '499.07', '277.94')
            . self::gradient('easylink-brand-a', '702.97', '499.07', '279.87')
            . '</defs>'
            . self::path(self::PATH_E, 'url(#easylink-brand-e)')
            . self::path(self::PATH_A, 'url(#easylink-brand-a)')
            . '</svg>';
    }

    /**
     * Base64 data URI wrapper, for `src` attributes and menu icons.
     */
    public static function toDataUri(string $svg): string
    {
        return 'data:image/svg+xml;base64,' . base64_encode($svg);
    }

    // ──────────────────────────────────────────────
    //  SVG builders
    // ──────────────────────────────────────────────

    private static function open(): string
    {
        return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="' . self::VIEW_BOX . '">';
    }

    private static function path(string $d, string $fill = ''): string
    {
        return '<path' . ($fill ? ' fill="' . $fill . '"' : '')
            . ' transform="' . self::TRANSFORM . '" d="' . $d . '"/>';
    }

    private static function gradient(string $id, string $cx, string $cy, string $r): string
    {
        return '<radialGradient id="' . $id . '" cx="' . $cx . '" cy="' . $cy . '" r="' . $r . '"'
            . ' gradientUnits="userSpaceOnUse">'
            . '<stop offset="0" stop-color="' . self::GRADIENT_FROM . '"/>'
            . '<stop offset="0.99" stop-color="' . self::GRADIENT_TO . '"/>'
            . '</radialGradient>';
    }
}
