<?php

namespace Bkwld\Croppa\Test;

use Bkwld\Croppa\Image;
use Intervention\Image\ImageManager;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 * @coversNothing
 */
class TestResizing extends TestCase
{
    private $src;
    private $options = [
        'quality' => 75,
        'interlace' => true,
        'upsize' => false,
    ];

    public function setUp(): void
    {
        parent::setUp();

        // Make an image
        $gd = imagecreate(500, 400);
        ob_start();
        imagejpeg($gd);
        $this->src = ob_get_clean();
    }

    public function testPassthru()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(null, null)->get());
        $this->assertEquals('500x400', $size[0].'x'.$size[1]);
    }

    public function testWidthConstraint()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(200, null)->get());
        $this->assertEquals('200x160', $size[0].'x'.$size[1]);
    }

    public function testHeightConstraint()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(null, 200)->get());
        $this->assertEquals('250x200', $size[0].'x'.$size[1]);
    }

    public function testWidthAndHeightConstraint()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(200, 100)->get());
        $this->assertEquals('200x100', $size[0].'x'.$size[1]);
    }

    public function testTooSmall()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(600, 600)->get());
        $this->assertEquals('400x400', $size[0].'x'.$size[1]);
    }

    public function testNotWideEnough()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(1000, 400)->get());
        $this->assertEquals('500x200', $size[0].'x'.$size[1]);
    }

    public function testNotTallEnough()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(500, 500)->get());
        $this->assertEquals('400x400', $size[0].'x'.$size[1]);
    }

    public function testWidthAndHeightWithUpscale()
    {
        $image = new Image($this->src, array_merge($this->options, ['upsize' => true]));
        $size = getimagesizefromstring($image->process(500, 500)->get());
        $this->assertEquals('500x500', $size[0].'x'.$size[1]);
    }

    public function testWidthAndHeightResize()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(200, 200, ['resize' => null])->get());
        $this->assertEquals('200x160', $size[0].'x'.$size[1]);
    }

    public function testWidthAndHeightPad()
    {
        $image = new Image($this->src, $this->options);
        $imageString = $image->process(200, 200, ['pad' => [100, 100, 100]])->get();
        $size = getimagesizefromstring($imageString);
        $firstPixelColor = ImageManager::gd()->read($imageString)->pickColor(1, 1)->toHex();

        $this->assertEquals('646464', $firstPixelColor);
        $this->assertEquals('200x200', $size[0].'x'.$size[1]);
    }

    public function testWidthAndHeightAndPadWithoutColor()
    {
        $image = new Image($this->src, $this->options);
        $imageString = $image->process(200, 200, ['pad'])->get();
        $size = getimagesizefromstring($imageString);
        $firstPixelColor = ImageManager::gd()->read($imageString)->pickColor(1, 1)->toHex();

        $this->assertEquals('ffffff', $firstPixelColor);
        $this->assertEquals('200x200', $size[0].'x'.$size[1]);
    }

    public function testWidthAndHeightTrim()
    {
        $image = new Image($this->src, $this->options);
        $size = getimagesizefromstring($image->process(200, 200, ['trim_perc' => [0.25, 0.25, 0.75, 0.75]])->get());
        $this->assertEquals('200x200', $size[0].'x'.$size[1]);
    }
}
