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 FlipImage(ImageManipulation):
    def getManipulationName(self) -> str:
        return "Flip"

    def getParameters(self) -> List[str]:
        return ["mode"]

    def manipulateImage(self, image: ImageContainer, parameters: Any) -> None:
        if image is None or image.getImage() is None:
            return
        pil_img = image.getImage()
        mode = "horizontal"
        if isinstance(parameters, dict) and parameters.get("mode") in ("horizontal", "vertical"):
            mode = parameters.get("mode")
        cv_img = pil_to_cv2(pil_img)
        flip_code = 1 if mode == "horizontal" else 0
        flipped = cv2.flip(cv_img, flip_code)
        image._imageData = cv2_to_pil(flipped)

