<?php

namespace App\Modifiers;

use Imagick;
use ImagickPixel;
use Illuminate\Support\Facades\Storage;
use App\Models\ToolStats;

class Rotater
{
    public function make($fileName, string $type)
    {
        $filePath = Storage::disk('temp')
            ->path($fileName);
        $imagick = new Imagick($filePath);

        if ($type == 'reflect-h') {
            $imagick->flopImage();
        } elseif ($type == 'reflect-v') {
            $imagick->flipImage();
        } elseif ($type == 'rotate-l-90') {
            $imagick->rotateImage(new ImagickPixel(), -90);
        } elseif ($type == 'rotate-l-180') {
            $imagick->rotateImage(new ImagickPixel(), -180);
        } elseif ($type == 'rotate-l-270') {
            $imagick->rotateImage(new ImagickPixel(), -270);
        } elseif ($type == 'rotate-r-90') {
            $imagick->rotateImage(new ImagickPixel(), 90);
        } elseif ($type == 'rotate-r-180') {
            $imagick->rotateImage(new ImagickPixel(), 180);
        } elseif ($type == 'rotate-r-270') {
            $imagick->rotateImage(new ImagickPixel(), 270);
        }

        $result = $imagick->writeImage($filePath);
        $imagick->clear();
        $imagick->destroy();

        if ($result) {
            $toolStats = new ToolStats();
            $toolStats->processNew('rotate_reflect');
            return [
                true,
                $fileName
            ];
        }

        return [
            false,
            __('lang.file_rotate_error')
        ];
    }

}