<?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();

        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,
                ),
            };
        }

        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,
            );
        }

        preg_match_all('/rounded/', $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,
            );
        }

        preg_match_all('/border-([tblr])-(.*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            return match ((string) $output_array[1][0]) {
                't' => new \PHPNative\Tailwind\Style\Border(true, top: (int) $output_array[2][0]),
                'b' => new \PHPNative\Tailwind\Style\Border(true, bottom: (int) $output_array[2][0]),
                'r' => new \PHPNative\Tailwind\Style\Border(true, right: (int) $output_array[2][0]),
                'l' => new \PHPNative\Tailwind\Style\Border(true, left: (int) $output_array[2][0]),
            };
        }

        preg_match_all('/border-(.*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $colorStyle = $output_array[1][0];
            $color = Color::parse($colorStyle);
            return new \PHPNative\Tailwind\Style\Border(false, $color);
        }

        preg_match_all('/border/', $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;
        }
        if ($style2->color->red != null) {
            $style1->color->red = $style2->color->red;
        }
        if ($style2->color->green != null) {
            $style1->color->green = $style2->color->green;
        }
        if ($style2->color->blue != null) {
            $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;
        }
    }
}
