import random

from .builder import ITEMS
from .clothing import Clothing


@ITEMS.register_module()
class Bag(Clothing):
    def __init__(self, **kwargs):
        pipeline = [
            dict(type='LoadImageFromFile', path=kwargs['path'], color=kwargs['color']),
            dict(type='KeypointDetection'),
            dict(type='ContourDetection'),
            dict(type='Painting'),
            dict(type='Scaling'),
            dict(type='Split'),
            # dict(type='ImageShow', key=['image', 'mask', 'pattern_image']),
        ]
        kwargs.update(pipeline=pipeline)
        super(Bag, self).__init__(**kwargs)

    @staticmethod
    def calculate_start_point(keypoint_type, scale, clothes_point, body_point):
        """
        align left
        Args:
            keypoint_type: string, "hand_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 (y', x')
            x' = y_body - y1 * scale
            y' = x_body - x1 * scale
        """
        location = random.choice(seq=['left', 'right'])
        if location == 'left':
            side_indicator = f'{keypoint_type}_left'
        else:
            side_indicator = f'{keypoint_type}_right'
        # clothes_point = {k: tuple(map(lambda x: int(scale * x), v[0: 2])) for k, v in clothes_point.items()}
        start_point = (body_point[side_indicator][1] - int(int(clothes_point[keypoint_type].split("_")[1]) * scale),
                       body_point[side_indicator][0] - int(int(clothes_point[keypoint_type].split("_")[0]) * scale))
        return start_point
