<?php

declare(strict_types=1);

namespace PHPNative\Tailwind\Parser;

class Color implements Parser
{
    public static function parse(string $style): \PHPNative\Tailwind\Style\Color
    {
        $red = 0;
        $green = 0;
        $blue = 0;

        $data = json_decode(file_get_contents(__DIR__ . '/../Data/colors.json'), true);

        if($style == "black") {
            return new \PHPNative\Tailwind\Style\Color(0, 0, 0);
        }
        if($style == "white") {
            return new \PHPNative\Tailwind\Style\Color(255, 255, 255);
        }

        preg_match_all('/(\w{1,8})/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $color = (string)$output_array[1][0];
            if (isset($data[$color]['500'])) {
                [$red, $green, $blue] = sscanf($data[$color]['500'], "#%02x%02x%02x");
            }
        }

        preg_match_all('/(\w{1,8})-(\d{1,3})/', $style, $output_array);
        if (count($output_array[0]) > 0) {
            $color = (string)$output_array[1][0];
            $variant = (string)$output_array[2][0];
            if (isset($data[$color][$variant])) {
                [$red, $green, $blue] = sscanf($data[$color][$variant], "#%02x%02x%02x");
            }
        }



        return new \PHPNative\Tailwind\Style\Color($red, $green, $blue);
    }
}
