import cv2

from app.core.config import PRIORITY_DICT


def organize_body(layer):
    body_layer = dict(priority=0,
                      name=layer["name"].lower(),
                      image=layer['body_image'],
                      image_url=layer['body_path'],
                      mask_image=None,
                      mask_url=None,
                      sacle=1,
                      # mask=layer['body_mask'],
                      position=(0, 0))
    return body_layer


def organize_clothing(layer):
    # 起始坐标
    start_point = calculate_start_point(layer['keypoint'], layer['scale'], layer['clothes_keypoint'], layer['body_point_test'], layer["offset"], layer["resize_scale"])
    # 前片数据
    front_layer = dict(priority=layer['priority'] if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_front', None),
                       name=f'{layer["name"].lower()}_front',
                       image=layer["front_image"],
                       # mask_image=layer['front_mask_image'],
                       image_url=layer['front_image_url'],
                       mask_url=layer['mask_url'],
                       sacle=layer['scale'],
                       clothes_keypoint=layer['clothes_keypoint'],
                       position=start_point,
                       resize_scale=layer["resize_scale"],
                       mask=cv2.resize(layer['mask'], layer["front_image"].size),
                       gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
                       pattern_image_url=layer['pattern_image_url'],
                       pattern_image=layer['pattern_image'],
                       # back_perspective_url=layer['back_perspective_url'] if 'back_perspective_url' in layer.keys() else ""
                       )
    # 后片数据
    back_layer = dict(priority=-layer.get("priority", 0) if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_back', None),
                      name=f'{layer["name"].lower()}_back',
                      image=layer["back_image"],
                      # mask_image=layer['back_mask_image'],
                      image_url=layer['back_image_url'],
                      mask_url=layer['mask_url'],
                      sacle=layer['scale'],
                      clothes_keypoint=layer['clothes_keypoint'],
                      position=start_point,
                      resize_scale=layer["resize_scale"],
                      mask=cv2.resize(layer['mask'], layer["front_image"].size),
                      gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
                      pattern_image_url=layer['pattern_image_url'],
                      # back_perspective_url=layer['back_perspective_url'] if 'back_perspective_url' in layer.keys() else ""
                      )
    return front_layer, back_layer


def organize_others(layer):
    # 起始坐标
    start_point = (0, 0)
    # 前片数据
    front_layer = dict(priority=layer['priority'] if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_front', None),
                       name=f'{layer["name"].lower()}_front',
                       image=layer["front_image"],
                       # mask_image=layer['front_mask_image'],
                       image_url=layer['front_image_url'],
                       mask_url=layer['mask_url'],
                       sacle=layer['scale'],
                       clothes_keypoint=(0, 0),
                       position=start_point,
                       resize_scale=layer["resize_scale"],
                       mask=cv2.resize(layer['mask'], layer["front_image"].size),
                       gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
                       pattern_image_url=layer['pattern_image_url'],
                       pattern_image=layer['pattern_image'],
                       # back_perspective_url=layer['back_perspective_url'] if 'back_perspective_url' in layer.keys() else ""
                       )
    # 后片数据
    back_layer = dict(priority=-layer.get("priority", 0) if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_back', None),
                      name=f'{layer["name"].lower()}_back',
                      image=layer["back_image"],
                      # mask_image=layer['back_mask_image'],
                      image_url=layer['back_image_url'],
                      mask_url=layer['mask_url'],
                      sacle=layer['scale'],
                      clothes_keypoint=(0, 0),
                      position=start_point,
                      resize_scale=layer["resize_scale"],
                      mask=cv2.resize(layer['mask'], layer["front_image"].size),
                      gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
                      pattern_image_url=layer['pattern_image_url'],
                      # back_perspective_url=layer['back_perspective_url'] if 'back_perspective_url' in layer.keys() else ""
                      )
    return front_layer, back_layer


def calculate_start_point(keypoint_type, scale, clothes_point, body_point, offset, resize_scale):
    """
    Align left
    Args:
        keypoint_type: string, "waistband" | "shoulder" | "ear_point"
        scale: float
        clothes_point: dict{'left': [x1, y1, z1], 'right': [x2, y2, z2]}
        body_point: dict, containing keypoint data of body figure

    Returns:
        start_point: tuple (x', y')
        x' = y_body - y1 * scale + offset
        y' = x_body - x1 * scale + offset

    """
    side_indicator = f'{keypoint_type}_left'
    start_point = (
        int(body_point[side_indicator][1] + offset[1] - int(clothes_point[side_indicator][0]) * scale),  # y
        int(body_point[side_indicator][0] + offset[0] - int(clothes_point[side_indicator][1]) * scale)  # x
    )
    return start_point
