from .ImageManipulation import ImageManipulation
from ImageContainer import ImageContainer
from typing import Any, List
from utils.image_utils import pil_to_cv2, cv2_to_pil
import cv2


class ResizeImage(ImageManipulation):
    def getManipulationName(self) -> str:
        return "Resize"

    def getParameters(self) -> List[str]:
        return ["width", "height"]

    def manipulateImage(self, image: ImageContainer, parameters: Any) -> None:
        if image is None or image.getImage() is None:
            return
        pil_img = image.getImage()
        width = None
        height = None
        if isinstance(parameters, dict):
            width = parameters.get("width")
            height = parameters.get("height")
        if not width or not height:
            # Default: scale to half
            w, h = pil_img.size
            width, height = max(1, w // 2), max(1, h // 2)
        cv_img = pil_to_cv2(pil_img)
        resized = cv2.resize(cv_img, (int(width), int(height)), interpolation=cv2.INTER_AREA)
        image._imageData = cv2_to_pil(resized)

