<?php

declare(strict_types=1);

namespace svoboda\pechatalka\models;

// Files of the project
use svoboda\pechatalka\models\core,
	svoboda\pechatalka\models\enumerations\products\type as product_type;

// Svoboda time
use svoboda\time\statement as svoboda;

// Baza database
use mirzaev\baza\database,
	mirzaev\baza\column,
	mirzaev\baza\record,
	mirzaev\baza\enumerations\encoding,
	mirzaev\baza\enumerations\type;

// Framework for Telegram
use Zanzara\Telegram\Type\User as telegram;

// Built-in libraries
use Imagick as imagick,
	ImagickPixel as imagick_pixel,
	ImagickDraw as imagick_draw,
	Exception as exception,
	RuntimeException as exception_runtime;

/**
 * Paper
 *
 * @package svoboda\pechatalka\models
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class paper extends core
{
	/**
	 * A5
	 *
	 * 	 
	 *
	 * @return record The account record from the database
	 */
	public static function a5(product_type $type, array $products, array $canvas = []): mixed
	{
		// Initializing the print DPI
		define('dpi', 80);
		/* define('dpi', 300); */

		// Initializing the A5 paper dimensions
		$a5 = [
			'width' => (int) round(10.5 * dpi),
			'height' => (int) round(14.8 * dpi)
		];

		// Initializing the paper
		$paper = new imagick();
		$paper->setResolution(dpi, dpi);
		$paper->newImage($a5['width'], $a5['height'], new imagick_pixel("white"));
		$paper->setImageUnits(imagick::RESOLUTION_PIXELSPERCENTIMETER);
		$paper->setImageFormat("jpg");

		foreach ($products as $product) {
			// Iterating over products

			foreach ($product as $layer) {
				// Iterating over the product layers

				// Filtering
				empty($layer['scale']) || $layer['scale'] == 0 and $layer['scale'] = 1;

				// Initializing the product areas
				$areas = $type->areas();

				// Calculating the product blank dimensions by the print DPI
				$blank = [
					'width' => (int) round($areas['blank']['width'] / 10 * dpi),
					'height' => (int) round($areas['blank']['height'] / 10 * dpi)
				];

				// Calculating the DPI ratio of the pechatalka interface to the blank dimensions
				$ratio = [
					'width' => $blank['width'] / $canvas['width'],
					'height' => $blank['height'] / $canvas['height']
				];

				// Initializing the circle mask
				$circle = new imagick();
				$circle->setResolution(dpi, dpi);
				$circle->newImage($blank['width'], $blank['height'], '#0008');
				$circle->setImageUnits(imagick::RESOLUTION_PIXELSPERCENTIMETER);
				$circle->setimageformat('png');
				$circle->setimagematte(true);
				$draw = new imagick_draw();
				$draw->setfillcolor('#fff');
				$draw->circle($blank['width'] / 2, $blank['height'] / 2, $blank['width'] / 2, $blank['height']);
				$circle->drawimage($draw);

				// Initializing the layer image
				$image = new imagick();
				$image->setResolution(dpi, dpi);
				$image->readImage($layer['image']);
				$image->setImageUnits(imagick::RESOLUTION_PIXELSPERCENTIMETER);
				$image->setimagematte(true);

				$width = (int) round($blank['width'] * $layer['scale']);
				$image->adaptiveResizeImage($width, 0);
				/* $image->roundCornersImage(50, 50); */

				$offset = [
					'x' => ($blank['width'] - $image->getImageWidth()) / 2,
					'y' => ($blank['height'] - $image->getImageHeight()) / 2
				];

				$vertical = $blank['height'] - $image->getImageHeight();



				 // Добавить нормальные комментарии после глубокго тестирования




				// Reinitializing the product layer offsets
				$layer['x'] = (int) round($layer['x'] * $ratio['width']);
				$layer['y'] = (int) round($layer['y'] * $ratio['height']);

				$image->compositeImage(
					$circle,
					imagick::COMPOSITE_DSTIN,
					(int) round(-$layer['x'] - $offset['x']),
					/* (int) round(-$layer['y'] - $offset['y'] - $vertical / 2) */
					(int) round(-$layer['y'] - $offset['y'])
				);

				// Compositing the layer with the paper
				$paper->setImageVirtualPixelMethod(imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
				$paper->setImageArtifact('compose:args', "1,0,-0.5,0.5");
				$paper->compositeImage(
					$image,
					imagick::COMPOSITE_MATHEMATICS,
					(int) round($a5['width'] / 2 - $blank['width'] / 2 + $layer['x'] + $offset['x']),
					/* (int) round($a5['height'] / 2 - $blank['height'] / 2 + $layer['y'] + $offset['y'] + $vertical / 2) */
					(int) round($a5['height'] / 2 - $blank['height'] / 2 + $layer['y'] + $offset['y'])
				);

				// Writing the paper file
				$paper->writeImage(INDEX . DIRECTORY_SEPARATOR . 'test.jpg');
			}
		}

		return true;
	}
}
