<?php

declare(strict_types=1);

namespace PHPNative\Tailwind\Parser;

class Margin implements Parser
{
    public static function parse(string $style): ?\PHPNative\Tailwind\Style\Margin
    {
        $l = null;
        $r = null;
        $t = null;
        $b = null;

        preg_match_all('/m-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $l = (int)$output_array[1][0] * 4;
            $r = (int)$output_array[1][0] * 4;
            $t = (int)$output_array[1][0] * 4;
            $b = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/mx-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $l = (int)$output_array[1][0] * 4;
            $r = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/my-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $t = (int)$output_array[1][0] * 4;
            $b = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/mt-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $t = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/mb-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $b = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/ml-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $l = (int)$output_array[1][0] * 4;
        }

        preg_match_all('/mr-(\d*)/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $r = (int)$output_array[1][0] * 4;
        }

        if($l != null || $r != null || $t != null || $b != null) {
            return new \PHPNative\Tailwind\Style\Margin($l, $r, $t, $b);
        }

        return null;
    }

    public static function merge(\PHPNative\Tailwind\Style\Margin $class, \PHPNative\Tailwind\Style\Margin $style)
    {
        if($style->left != null) {
            $class->left = $style->left;
        }
        if($style->right != null) {
            $class->right = $style->right;
        }
        if($style->top != null) {
            $class->top = $style->top;
        }
        if($style->bottom != null) {
            $class->bottom = $style->bottom;
        }
    }
}
