
import cv2
import numpy as np
from typing import List, Dict, Any
from shapely.geometry import Polygon
import logging

class MaskModule:
    def __init__(self, logger, base_dir):
        self.logger = logger
        self.base_dir = base_dir
        self.logger.log("마스크 모듈 초기화 완료", level=logging.INFO)

    def create_masks(self, image_path: str, ocr_results: List[Dict], expansion_size: int = 10, blur_size: int = 15, mask_option: str = "basic") -> np.ndarray:
        image = cv2.imread(image_path)
        if image is None:
            self.logger.log(f"이미지를 읽을 수 없습니다: {image_path}", level=logging.ERROR)
            return None
        height, width = image.shape[:2]
        mask = np.zeros((height, width), dtype=np.uint8)
        for i, result in enumerate(ocr_results, 1):
            polygon = result['polygon']
            expanded_poly = self.expand_polygon(polygon, offset=5)
            cv2.fillPoly(mask, [expanded_poly], 255)
        processed_mask = self.process_mask(mask, expansion_size, blur_size)
        return processed_mask
    
    def expand_polygon(self, polygon, offset=15):
        poly = Polygon(polygon)
        expanded = poly.buffer(offset)
        if expanded.is_empty:
            return np.array(polygon, dtype=np.int32)
        return np.array(expanded.exterior.coords, dtype=np.int32)
    
    def process_mask(self, mask: np.ndarray, expansion_size: int = 5, blur_size: int = 3) -> np.ndarray:
        processed_mask = mask.copy()
        if expansion_size > 0:
            kernel = np.ones((expansion_size, expansion_size), np.uint8)
            processed_mask = cv2.dilate(processed_mask, kernel, iterations=1)
        if blur_size > 0:
            blur_size = blur_size if blur_size % 2 == 1 else blur_size + 1
            processed_mask = cv2.GaussianBlur(processed_mask, (blur_size, blur_size), 0)
        return processed_mask
    