import cv2
import numpy as np

from app.service.design_fast.utils.design_ensemble import get_seg_result
from app.service.utils.new_oss_client import oss_upload_image


class BackPerspective:
    def __init__(self, minio_client):
        self.minio_client = minio_client

    def __call__(self, result):

        # 如果sketch为系统图 查看是否有对应的 背后视角图
        if result['path'].split('/')[0] == 'aida-sys-image':
            file_path = result['path'].replace("images", 'images_back', 1)
            if self.is_file_exists(bucket_name='aida-sys-image', file_name=file_path[file_path.find('/') + 1:]):
                result['back_perspective_url'] = file_path
                return result
            else:
                seg_result = get_seg_result("1", result['image'])[0]
        elif result['name'] in ['blouse', 'outwear', 'dress', 'tops']:
            seg_result = result['seg_result']
        else:
            seg_result = get_seg_result("1", result['image'])[0]

        m = self.thicken_contours_and_display(seg_result, thickness=10, color=(0, 0, 0))
        back_sketch = result['image'].copy()
        back_sketch[m > 100] = 255
        # 上传背后视角图
        _, img_encoded = cv2.imencode(".jpg", back_sketch)

        resp = oss_upload_image(self.minio_client, bucket='test', object_name=result['path'], image_bytes=img_encoded.tobytes())
        result['back_perspective_url'] = f"{resp.bucket_name}/{resp.object_name}"
        return result

    def thicken_contours_and_display(self, mask, thickness=10, color=(0, 0, 0)):
        mask = mask.astype(np.uint8) * 255
        # 查找轮廓
        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        # 创建一个彩色副本用于绘制轮廓
        mask_color = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

        def thicken_contour_inward(contour, thick):
            # 创建一个空白的黑色图像与原始掩码大小相同
            blank = np.zeros_like(mask)
            # 在空白图像上绘制白色的轮廓
            cv2.drawContours(blank, [contour], -1, 255, thickness=thick)
            # 找到轮廓的中心（可以用重心等方法近似）
            M = cv2.moments(contour)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            # 进行距离变换，离中心越近的值越小
            dist_transform = cv2.distanceTransform(255 - blank, cv2.DIST_L2, 5)
            # 根据距离变换的值来决定是否保留像素，离中心近的像素更容易被保留
            result = np.zeros_like(mask)
            for i in range(dist_transform.shape[0]):
                for j in range(dist_transform.shape[1]):
                    if dist_transform[i, j] < thick:
                        result[i, j] = 255
            return result

        for contour in contours:
            thickened_contour = thicken_contour_inward(contour, thickness)
            mask_color[thickened_contour > 0] = color

        _, binary_result = cv2.threshold(mask_color, 127, 255, cv2.THRESH_BINARY)

        # 转换为掩码形式
        mask_result = cv2.cvtColor(binary_result, cv2.COLOR_BGR2GRAY)
        return mask_result

    def is_file_exists(self, bucket_name, file_name):
        try:
            self.minio_client.stat_object(bucket_name, file_name)
            return True
        except Exception:
            return False
