<?php

namespace App\Support;

class LetterLayout
{
    /**
     * @return array{
     *     page_format: string,
     *     margins_mm: array{0: int, 1: int, 2: int, 3: int},
     *     css: array<string, string>
     * }
     */
    public static function resolve(): array
    {
        $page = config('letter.page');
        $content = config('letter.content');
        $typography = config('letter.typography');
        $spacing = config('letter.spacing');
        $colors = config('letter.colors');

        $marginTop = self::length($page['margin_top_mm']);
        $marginRight = self::length($page['margin_right_mm']);
        $marginBottom = self::length($page['margin_bottom_mm']);
        $marginLeft = self::length($page['margin_left_mm']);
        $contentPaddingX = self::length($content['padding_x_mm']);
        $pageHeight = $page['height_mm'].'mm';

        return [
            'page_format' => $page['format'],
            'margins_mm' => [
                $page['margin_top_mm'],
                $page['margin_right_mm'],
                $page['margin_bottom_mm'],
                $page['margin_left_mm'],
            ],
            'css' => [
                'margin_top' => $marginTop,
                'margin_right' => $marginRight,
                'margin_bottom' => $marginBottom,
                'margin_left' => $marginLeft,
                'content_padding_x' => $contentPaddingX,
                'page_height' => $pageHeight,
                'sheet_min_height' => sprintf(
                    'calc(%s - %s - %s)',
                    $pageHeight,
                    $marginTop,
                    $marginBottom,
                ),
                'font_family' => $typography['font_family'],
                'font_size' => $typography['font_size'],
                'line_height' => (string) $typography['line_height'],
                'body_color' => $typography['body_color'],
                'signatory_title_size' => $typography['signatory_title_size'],
                'signatory_title_color' => $typography['signatory_title_color'],
                'footer_font_size' => $typography['footer_font_size'],
                'footer_line_height' => (string) $typography['footer_line_height'],
                'letterhead_registration_size' => $typography['letterhead_registration_size'],
                'letterhead_margin_bottom' => $spacing['letterhead_margin_bottom'].'px',
                'meta_margin_bottom' => $spacing['meta_margin_bottom'].'px',
                'recipient_margin_bottom' => $spacing['recipient_margin_bottom'].'px',
                'subject_margin_bottom' => $spacing['subject_margin_bottom'].'px',
                'body_margin_bottom' => $spacing['body_margin_bottom'].'px',
                'body_paragraph_margin_bottom' => $spacing['body_paragraph_margin_bottom'].'px',
                'signature_block_margin_bottom' => $spacing['signature_block_margin_bottom'].'px',
                'signature_company_margin_bottom' => $spacing['signature_company_margin_bottom'].'px',
                'signature_space_height' => $spacing['signature_space_height'].'px',
                'logo_height' => $spacing['logo_height'].'px',
                'color_blue' => $colors['blue'],
                'color_orange' => $colors['orange'],
                'color_white' => $colors['white'],
                'color_black' => $colors['black'],
            ],
        ];
    }

    private static function length(int $millimetres): string
    {
        $centimetres = $millimetres / 10;

        return rtrim(rtrim(number_format($centimetres, 2, '.', ''), '0'), '.').'cm';
    }
}
