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
import numpy as np


class HueShift(ImageManipulation):
    def getManipulationName(self) -> str:
        return "Hue Shift"

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

    def manipulateImage(self, image: ImageContainer, parameters: Any) -> None:
        if image is None or image.getImage() is None:
            return
        pil_img = image.getImage()
        cv_img = pil_to_cv2(pil_img)
        hsv = cv2.cvtColor(cv_img, cv2.COLOR_BGR2HSV).astype(np.uint8)
        hue_delta = 50
        if isinstance(parameters, dict) and parameters.get("hue") is not None:
            hue_delta = int(parameters.get("hue"))
        h, s, v = cv2.split(hsv)
        # OpenCV H range is [0,179]; wrap around using modulo
        h = ((h.astype(np.int16) + hue_delta) % 180).astype(np.uint8)
        hsv_shifted = cv2.merge([h, s, v])
        bgr = cv2.cvtColor(hsv_shifted, cv2.COLOR_HSV2BGR)
        image._imageData = cv2_to_pil(bgr)


