import logging

import numpy as np
import cv2
from matplotlib import pyplot as plt

from PIL import Image


def show(img, win_name="temp"):
    cv2.imshow(win_name, img)
    cv2.waitKey(0)


def crop(img):
    mid_point_h, mid_point_w = int(img.shape[0] / 2 + 30), int(img.shape[1] / 2)
    img_roi = img[mid_point_h - 520: mid_point_h + 520, mid_point_w - 340: mid_point_w + 340]
    return img_roi


class Layer(object):
    def __init__(self):
        self._layer = []

    @property
    def layer(self):
        return self._layer

    def insert(self, layer_instance):
        if layer_instance['name'] == 'body':
            self._body = layer_instance
        self._layer.append(layer_instance)

    def sort(self, priority):
        self._layer.sort(key=lambda x: priority[x['name']])

    # def merge(self, cfg):
    #     """
    #     opencv shape order (height, width, channel)
    #     image coordinate system:
    #     |------------->x (width)
    #     |
    #     |
    #     |
    #     y (height)
    #     Returns:
    #
    #
    #     """
    #     base_image = Image.new('RGBA', self._layer[1]['image'].size, (0, 0, 0, 0))
    #     for layer in self._layer:
    #         y, x = layer['position']
    #         base_image.paste(layer['image'], (x, y), layer['image'])
    #         # base_image.show()
    #
    #     for x in self._layer:
    #         if np.all(x['mask'] == 0):
    #             continue
    #             # obtain region of interest about roi(roi) and item-image(roi_image, roi_mask)
    #             roi, roi_mask, roi_image, signal = self.get_roi(dst=dst, image=x)
    #         temp_bg = np.expand_dims(cv2.bitwise_not(roi_mask), axis=2).repeat(3, axis=2)
    #         tmp1 = (roi * (temp_bg / 255)).astype(np.uint8)
    #         temp_fg = np.expand_dims(roi_mask, axis=2).repeat(3, axis=2)
    #         tmp2 = (roi_image * (temp_fg / 255)).astype(np.uint8)
    #
    #         roi[:] = cv2.add(tmp1, tmp2)
    #         # show(cv2.resize(dst, (int(dst.shape[1] * 0.5), int(dst.shape[0] * 0.5)), interpolation=cv2.INTER_AREA),
    #         #      win_name=x.get('name'))
    #     # crop image and get the central part
    #     if cfg.get('basic')['self_template'] == False:
    #         dst_roi = crop(dst)
    #     else:
    #         dst_roi = dst
    #     return dst_roi, signal
    #
    # @staticmethod
    # def get_roi(dst, image):
    #     signal = False
    #     dst_y, dst_x = dst.shape[:2]
    #     roi_height, roi_width = image['mask'].shape
    #     roi_y0, roi_x0 = image['position']
    #
    #     if roi_y0 < 0:
    #         roi_yin = 0
    #         mask_yin = -roi_y0
    #         signal = True
    #     else:
    #         roi_yin = roi_y0
    #         mask_yin = 0
    #     if roi_y0 + roi_height > dst_y:
    #         roi_yout = dst_y
    #         mask_yout = dst_y - roi_y0
    #         signal = True
    #     else:
    #         roi_yout = roi_height + roi_y0
    #         mask_yout = roi_height
    #     # x part
    #     if roi_x0 < 0:
    #         roi_xin = 0
    #         mask_xin = -roi_x0
    #         signal = True
    #     else:
    #         roi_xin = roi_x0
    #         mask_xin = 0
    #     if roi_x0 + roi_width > dst_x:
    #         roi_xout = dst_x
    #         mask_xout = dst_x - roi_x0
    #         signal = True
    #     else:
    #         roi_xout = roi_width + roi_x0
    #         mask_xout = roi_width
    #
    #     roi = dst[roi_yin: roi_yout, roi_xin: roi_xout]
    #     roi_mask = image['mask'][mask_yin: mask_yout, mask_xin: mask_xout]
    #     roi_image = image['image'][mask_yin: mask_yout, mask_xin: mask_xout]
    #     return roi, roi_mask, roi_image, signal
