<?php

namespace App\Modifiers;

use Imagick;
use Illuminate\Support\Facades\Storage;
class Thumbnail
{
    private static $defaultStorage;

    public function __construct() 
    {
        self::$defaultStorage = defaultStorage()->storage_key;
    }

    public function make(string $fileName): array
    {
        $fileNameArr = explode(".", $fileName);

        if ($fileNameArr[1] == 'raw') {
            $imagick = new Imagick(public_path("assets/image/raw-thumb.webp"));
        } elseif ($fileNameArr[1] == 'ico') {
            $imagick = new Imagick(public_path("assets/image/ico-thumb.webp"));
        } else {
            if (self::$defaultStorage == 'default') {
                $filePath = Storage::disk(self::$defaultStorage)
                    ->path("{$fileName}");
                $imagick = new Imagick($filePath);
            } else {
                $filePath = Storage::disk(self::$defaultStorage)
                    ->get("{$fileName}");
                $imagick = new Imagick();
                $imagick->readImageBlob($filePath);
            }

            $format = 'jpg';
            $compression = Imagick::COMPRESSION_JPEG;

            $imagick->setFormat($format);
            $imagick->setImageFormat($format);

            $imagick->setCompression($compression);
            $imagick->setImageCompression($compression);

            $imagick->scaleImage(100, 100, true);
        }


        return [
            true,
            $imagick->getImageBlob()
        ];

    }

}
