<?php

declare(strict_types=1);

namespace PHPNative\Tailwind\Parser;

class Border implements Parser
{
    public static function parse(string $style): null|\PHPNative\Tailwind\Style\Border
    {
        $color = new \PHPNative\Tailwind\Style\Color();

        // Rounded per side: rounded-t-sm / rounded-b-lg / rounded-l-md / rounded-r-xl
        preg_match_all('/rounded-(t|b|l|r)-(none|sm|md|lg|xl|2xl|3xl)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $size = match ((string) $output_array[2][0]) {
                'none' => 0,
                'sm' => 2,
                'md' => 6,
                'lg' => 8,
                'xl' => 12,
                '2xl' => 16,
                '3xl' => 24,
            };
            return match ((string) $output_array[1][0]) {
                't' => new \PHPNative\Tailwind\Style\Border(
                    roundTopLeft: $size,
                    roundTopRight: $size,
                ),
                'b' => new \PHPNative\Tailwind\Style\Border(
                    roundBottomLeft: $size,
                    roundBottomRight: $size,
                ),
                'r' => new \PHPNative\Tailwind\Style\Border(
                    roundTopRight: $size,
                    roundBottomRight: $size,
                ),
                'l' => new \PHPNative\Tailwind\Style\Border(
                    roundTopLeft: $size,
                    roundBottomLeft: $size,
                ),
            };
        }

        // Rounded all corners: rounded-sm / rounded-lg / ...
        preg_match_all('/rounded-(none|sm|md|lg|xl|2xl|3xl)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $size = match ((string) $output_array[1][0]) {
                'none' => 0,
                'sm' => 2,
                'md' => 6,
                'lg' => 8,
                'xl' => 12,
                '2xl' => 16,
                '3xl' => 24,
            };

            return new \PHPNative\Tailwind\Style\Border(
                roundTopLeft: $size,
                roundTopRight: $size,
                roundBottomLeft: $size,
                roundBottomRight: $size,
            );
        }

        // Generic rounded (no size) -> small radius
        preg_match_all('/(?<![a-z-])rounded(?![a-z-])/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $size = 4;

            return new \PHPNative\Tailwind\Style\Border(
                roundTopLeft: $size,
                roundTopRight: $size,
                roundBottomLeft: $size,
                roundBottomRight: $size,
            );
        }

        // Directional borders with explicit numeric width, e.g. border-t-2
        if (preg_match('/^border-(t|b|l|r)-(\d+)$/', $style, $m)) {
            $w = (int) $m[2];
            return match ($m[1]) {
                't' => new \PHPNative\Tailwind\Style\Border(true, top: $w),
                'b' => new \PHPNative\Tailwind\Style\Border(true, bottom: $w),
                'r' => new \PHPNative\Tailwind\Style\Border(true, right: $w),
                'l' => new \PHPNative\Tailwind\Style\Border(true, left: $w),
            };
        }

        // Directional borders without width -> default 1px, e.g. border-r
        if (preg_match('/^border-(t|b|l|r)$/', $style, $m)) {
            $w = 1;
            return match ($m[1]) {
                't' => new \PHPNative\Tailwind\Style\Border(true, top: $w),
                'b' => new \PHPNative\Tailwind\Style\Border(true, bottom: $w),
                'r' => new \PHPNative\Tailwind\Style\Border(true, right: $w),
                'l' => new \PHPNative\Tailwind\Style\Border(true, left: $w),
            };
        }

        // Color-only border: border-red-500 / border-lime-300
        if (preg_match('/^border-(.+)$/', $style, $output_array)) {
            $colorStyle = $output_array[1];
            $color = Color::parse($colorStyle);
            // Nur Farbe setzen, keine Breiten – Breiten kommen von "border" oder "border-r" etc.
            return new \PHPNative\Tailwind\Style\Border(
                enabled: false,
                color: $color,
            );
        }

        // Plain "border" -> 1px Rahmen rundherum, Standardfarbe
        preg_match_all('/(?<![a-z-])border(?![a-z-])/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return new \PHPNative\Tailwind\Style\Border(
                enabled: true,
                left: 1,
                right: 1,
                top: 1,
                bottom: 1,
            );
        }

        return null;
    }

    public static function merge(
        \PHPNative\Tailwind\Style\Border $style1,
        \PHPNative\Tailwind\Style\Border $style2,
    ): void {
        if ($style2->enabled && !$style1->enabled) {
            $style1->enabled = true;
        }

        // Farbe nur übernehmen, wenn im zweiten Style wirklich gesetzt ist
        if ($style2->color->isNotSet()) {
            $style1->color->red = $style2->color->red;
            $style1->color->green = $style2->color->green;
            $style1->color->blue = $style2->color->blue;
        }
        if ($style2->color->alpha !== null) {
            $style1->color->alpha = $style2->color->alpha;
        }

        if ($style2->top !== null) {
            $style1->top = $style2->top;
        }
        if ($style2->bottom !== null) {
            $style1->bottom = $style2->bottom;
        }
        if ($style2->left !== null) {
            $style1->left = $style2->left;
        }
        if ($style2->right !== null) {
            $style1->right = $style2->right;
        }
        if ($style2->roundTopLeft !== null) {
            $style1->roundTopLeft = $style2->roundTopLeft;
        }
        if ($style2->roundTopRight !== null) {
            $style1->roundTopRight = $style2->roundTopRight;
        }
        if ($style2->roundBottomLeft !== null) {
            $style1->roundBottomLeft = $style2->roundBottomLeft;
        }
        if ($style2->roundBottomRight !== null) {
            $style1->roundBottomRight = $style2->roundBottomRight;
        }
    }
}

