<?php

namespace App\Traits;

use App\Entities\Mangboard\BoardEntity;
use App\Libraries\MyStorage\MangboardStorage;
use RuntimeException;

trait ImageTrait
{
    private $_image;
    private $_imageType;

    final public function getWidth_ImageTrait()
    {
        return imagesx($this->_image);
    }

    // 이미지의 현재 높이를 반환하는 메소드
    final public function getHeight_ImageTrait()
    {
        return imagesy($this->_image);
    }

    // 이미지 파일을 로드하는 메소드
    final public function load_ImageTrait($file)
    {
        $imageInfo = getimagesize($file);
        $this->_imageType = $imageInfo[2];
        switch ($this->_imageType) {
            case IMAGETYPE_JPEG:
                $this->_image = imagecreatefromjpeg($file);
                break;
            case IMAGETYPE_GIF:
                $this->_image = imagecreatefromgif($file);
                break;
            case IMAGETYPE_PNG:
                $this->_image = imagecreatefrompng($file);
                break;
            case IMAGETYPE_WEBP:
                $this->_image = imagecreatefromwebp($file);
                break;
            default:
                throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 Unsupported image type: " . $this->_imageType);
        }
    }

    // 이미지 크기를 지정된 너비, 높이로 변경하는 메소드
    final public function resize_ImageTrait($width, $height)
    {
        $newImage = imagecreatetruecolor($width, $height);
        imagecopyresampled(
            $newImage,
            $this->_image,
            0,
            0,
            0,
            0,
            $width,
            $height,
            $this->getWidth_ImageTrait(),
            $this->getHeight_ImageTrait()
        );
        $this->_image = $newImage;
    }

    // 이미지 비율을 유지하면서 크기를 조정하는 메소드
    final public function resizeToWidth_ImageTrait($width)
    {
        $ratio = $width / $this->getWidth_ImageTrait();
        $height = $this->getHeight_ImageTrait() * $ratio;
        $this->resize_ImageTrait($width, $height);
    }

    final public function resizeToHeight_ImageTrait($height)
    {
        $ratio = $height / $this->getHeight_ImageTrait();
        $width = $this->getWidth_ImageTrait() * $ratio;
        $this->resize_ImageTrait($width, $height);
    }

    final public function scale($scale)
    {
        $width = $this->getWidth_ImageTrait() * ($scale / 100);
        $height = $this->getHeight_ImageTrait() * ($scale / 100);
        $this->resize_ImageTrait($width, $height);
    }

    // 이미지를 저장하는 메소드
    final public function save_ImageTrait($file, $imageType = IMAGETYPE_WEBP, $compression = 75)
    {
        switch ($imageType) {
            case IMAGETYPE_JPEG:
                imagejpeg($this->_image, $file, $compression);
                break;
            case IMAGETYPE_GIF:
                imagegif($this->_image, $file);
                break;
            case IMAGETYPE_PNG:
                imagepng($this->_image, $file);
                break;
            case IMAGETYPE_WEBP:
                imagewebp($this->_image, $file, $compression);
                break;
            default:
                throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 Unsupported image type: " . $imageType);
        }
    }

    // 메모리 해제를 위한 메소드
    // final public function destroy_ImageTrait()
    // {
    //     imagedestroy($this->_image);
    // }
}
