# @Author  : liu fan
import numpy as np
import tensorflow as tf

from segment.sheet_resolve.lib.ssd_model.utils import label_map_util, ops as utils_ops
from segment.sheet_resolve.tools import tf_settings

from segment.sheet_resolve.tools.tf_sess import SsdSess
from PIL import Image

tf_sess_dict = {
    'choice_ssd': SsdSess('choice_ssd'),
}

choice_ssd_sess = tf_sess_dict['choice_ssd']
sess = choice_ssd_sess.sess
detection_graph = choice_ssd_sess.graph


def load_image_into_numpy_array(image):
    # print(image)
    image = image.convert('RGB')
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)


def run_inference_for_single_image(image):
    ops = detection_graph.get_operations()
    all_tensor_names = {output.name for op in ops for output in op.outputs}
    tensor_dict = {}
    for key in [
        'num_detections', 'detection_boxes', 'detection_scores',
        'detection_classes', 'detection_masks'
    ]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
            tensor_dict[key] = detection_graph.get_tensor_by_name(
                tensor_name)
    if 'detection_masks' in tensor_dict:
        # The following processing is only for single image
        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
        # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
            detection_masks, detection_boxes, image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(
            tf.greater(detection_masks_reframed, 0.5), tf.uint8)
        # Follow the convention by adding back the batch dimension
        tensor_dict['detection_masks'] = tf.expand_dims(
            detection_masks_reframed, 0)
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Run inference
    # start = time.time()
    output_dict = sess.run(tensor_dict,
                           feed_dict={image_tensor: np.expand_dims(image, 0)})
    # print(time.time()-start)
    # all outputs are float32 numpy arrays, so convert types as appropriate
    output_dict['num_detections'] = int(output_dict['num_detections'][0])
    output_dict['detection_classes'] = output_dict[
        'detection_classes'][0].astype(np.uint8)
    output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
    output_dict['detection_scores'] = output_dict['detection_scores'][0]
    if 'detection_masks' in output_dict:
        output_dict['detection_masks'] = output_dict['detection_masks'][0]
    return output_dict


def image_detect(image_np, category, score_threshold):
    image_np = load_image_into_numpy_array(image_np)
    detections = []
    w, h = image_np.shape[1], image_np.shape[0]
    with tf.device("/device:GPU:{}".format(0)):
        output_dict = run_inference_for_single_image(image_np)
    boxes = output_dict['detection_boxes']
    scores = output_dict['detection_scores']
    labels = output_dict['detection_classes']
    indices = np.where(scores > score_threshold)
    image_scores = scores[indices]
    image_boxes = boxes[indices]
    image_labels = labels[indices]
    image_detections = np.concatenate(
        [image_boxes, np.expand_dims(image_scores, axis=1), np.expand_dims(image_labels, axis=1)], axis=1)
    for detection in image_detections:
        y0 = int(detection[0] * h)
        x0 = int(detection[1] * w)
        y1 = int(detection[2] * h)
        x1 = int(detection[3] * w)
        label_index = int(detection[5])
        label_name = category[label_index]['name']
        detections.append((x0, y0, x1, y1, label_index, detection[4], label_name))
    return detections


# def get_choice_m_row_and_col(left, top, image):
#     im_resize = 300
#     ''' choice_m resize to 300*300'''
#     image_src = Image.fromarray(image)
#     if image_src.mode == 'RGB':
#         image_src = image_src.convert("L")
#     w, h = image_src.size
#     if h > w:
#         image_src = image_src.resize((int(im_resize / h * w), im_resize))
#     else:
#         image_src = image_src.resize((im_resize, int(im_resize / w * h)))
#     w_, h_ = image_src.size
#     image_300 = Image.new(image_src.mode, (im_resize, im_resize), (255))
#     image_300.paste(image_src, [0, 0, w_, h_])
#
#     category_index = label_map_util.create_category_index_from_labelmap(tf_settings.choice_m_ssd_label,
#                                                                         use_display_name=True)
#     detections = image_detect(image_300, category_index, 0.5)
#     if len(detections) > 1:
#         box_xmin = []
#         box_ymin = []
#         box_xmax = []
#         box_ymax = []
#         x_distance_all = []
#         y_distance_all = []
#         x_width_all = []
#         y_height_all = []
#         all_small_coordinate = []
#         ssd_column = 1
#         ssd_row = 1
#         count_x = 0
#         count_y = 0
#         for index, box in enumerate(detections):
#             if box[-1] != 'T' and box[2] <= w_ and box[3] <= h_:
#                 box0 = round(box[0] * (w / w_))  # Map to the original image
#                 box1 = round(box[1] * (h / h_))
#                 box2 = round(box[2] * (w / w_))
#                 box3 = round(box[3] * (h / h_))
#                 box_xmin.append(box0)
#                 box_ymin.append(box1)
#                 box_xmax.append(box2)
#                 box_ymax.append(box3)
#                 small_coordinate = {'xmin': box0 + left,
#                                     'ymin': box1 + top,
#                                     'xmax': box2 + left,
#                                     'ymax': box3 + top}
#                 all_small_coordinate.append(small_coordinate)
#                 x_width = box2 - box0
#                 y_height = box3 - box1
#                 x_width_all.append(x_width)
#                 y_height_all.append(y_height)
#
#         sorted_xmin = sorted(box_xmin)
#         sorted_ymin = sorted(box_ymin)
#         sorted_xmax = sorted(box_xmax)
#         sorted_ymax = sorted(box_ymax)
#
#         x_width_all_sorted = sorted(x_width_all, reverse=True)
#         y_height_all_sorted = sorted(y_height_all, reverse=True)
#         len_x = len(x_width_all)
#         len_y = len(y_height_all)
#         x_width_median = np.median(x_width_all_sorted)
#         y_height_median = np.median(y_height_all_sorted)
#
#         for i in range(len(sorted_xmin) - 1):
#             x_distance = abs(sorted_xmin[i + 1] - sorted_xmin[i])
#             y_distance = abs(sorted_ymin[i + 1] - sorted_ymin[i])
#             if x_distance > 20:
#                 ssd_column = ssd_column + 1
#                 x_distance_all.append(x_distance)
#                 if x_distance > 2 * x_width_median + 4:
#                     count_x = count_x + 1
#             if y_distance > 10:
#                 ssd_row = ssd_row + 1
#                 y_distance_all.append(y_distance)
#                 if y_distance > 2 * y_height_median + 3:
#                     count_y = count_y + 1
#             if x_width_all_sorted[i] - x_width_median > 40:
#                 ssd_column = ssd_column - 1
#             elif x_width_median - x_width_all_sorted[i] > 40:
#                 ssd_column = ssd_column - 1
#             if y_height_all_sorted[i] - y_height_median > 20:
#                 ssd_row = ssd_row - 1
#             elif y_height_median - y_height_all_sorted[i] > 20:
#                 ssd_row = ssd_row - 1
#
#         if count_x < len(x_distance_all) / 2 + 1:
#             ssd_column = ssd_column + count_x
#         elif count_y < len(y_distance_all) / 2 + 1:
#             ssd_row = ssd_row + count_y
#
#         average_height = int(np.mean(y_height_all))
#         average_width = int(np.mean(x_width_all))
#
#         # average_height = format(np.mean(y_height_all), '.2f')
#         # average_width = format(np.mean(x_width_all), '.2f')
#         # average_height = int(np.mean(y_distance_all))
#         # average_width = int(np.mean(x_distance_all))
#         location_ssd = {'xmin': sorted_xmin[0] + left,
#                         'ymin': sorted_ymin[0] + top,
#                         'xmax': sorted_xmax[-1] + left,
#                         'ymax': sorted_ymax[-1] + top}
#
#         choice_m_ssd = {'bounding_box': location_ssd,
#                         "single_height": average_height,
#                         "single_width": average_width,
#                         "rows": ssd_row,
#                         "cols": ssd_column,
#                         'class_name': 'choice_m',
#                         'all_small_coordinate': all_small_coordinate
#                         }
#     else:
#         choice_m_ssd = {}
#     return choice_m_ssd



def get_choice_m_row_and_col(left, top, image):
    im_resize = 300
    da_number_h = 0
    da_number_w = 0

    w_ = 300
    h_ = 300
    ''' choice_m resize to 300*300'''
    image_src = Image.fromarray(image)
    if image_src.mode == 'RGB':
        image_src = image_src.convert("L")
    w, h = image_src.size
    bounder_w = w
    bounder_h = h
    image_300 = Image.new(image_src.mode, (im_resize, im_resize), (255))

    if h > w:
        if h > 300:
            # w = int(w/(h/300))
            h_1 = 300
            image_src_resize = image_src.resize((w, 300))
        else:
            h_1 = h
            image_src_resize = image_src
        da_number_h = int((im_resize - w) / (w + 4))
        if da_number_h > 0:
            w_ = w
            h_ = h_1
            bounder_w = w
            for idx in range(da_number_h):
                x0 = idx * w + 4
                x1 = idx * w + 4 + w
                image_300.paste(image_src_resize, [x0, 0, x1, h_1])

        else:
            image_src_resize = image_src.resize((int(im_resize / h * w), im_resize))
            w_, h_ = image_src_resize.size
            image_300.paste(image_src_resize, [0, 0, w_, h_])
    else:
        if w > 300:
            # h = int(h/(w/300))
            w_1 = 300
            image_src_resize = image_src.resize((300, h))
        else:
            w_1 = w
            image_src_resize = image_src
        da_number_w = int((im_resize - h) / (h + 4))
        if da_number_w > 0:
            h_ = h
            w_ = w_1
            bounder_h = h
            for idx in range(da_number_w):
                h0 = idx * h + 4
                h1 = idx * h + 4 + h
                image_300.paste(image_src_resize, [0, h0, w_1, h1])
        else:
            image_src_resize = image_src.resize((im_resize, int(im_resize / w * h)))
            w_, h_ = image_src_resize.size
            image_300.paste(image_src_resize, [0, 0, w_, h_])

    w_resize, h_resize = image_src_resize.size
    category_index = label_map_util.create_category_index_from_labelmap(tf_settings.choice_m_ssd_label,
                                                                        use_display_name=True)
    detections = image_detect(image_300, category_index, 0.5)
    if len(detections) > 1:
        box_xmin = []
        box_ymin = []
        box_xmax = []
        box_ymax = []
        x_distance_all = []
        y_distance_all = []
        x_width_all = []
        y_height_all = []
        all_small_coordinate = []
        ssd_column = 1
        ssd_row = 1
        count_x = 0
        count_y = 0
        for index, box in enumerate(detections):
            if box[-1] != 'T' and box[2] <= 300 and box[3] <= 300:
                box0 = round(box[0] * (w / w_))  # Map to the original image
                box1 = round(box[1] * (h / h_))
                box2 = round(box[2] * (w / w_))
                box3 = round(box[3] * (h / h_))
                box_xmin.append(box0)
                box_ymin.append(box1)
                box_xmax.append(box2)
                box_ymax.append(box3)
                if box2 < bounder_w and box3 < bounder_h:
                    small_coordinate = {'xmin': box0 + left,
                                        'ymin': box1 + top,
                                        'xmax': box2 + left,
                                        'ymax': box3 + top}
                    all_small_coordinate.append(small_coordinate)
                x_width = box2 - box0
                y_height = box3 - box1
                x_width_all.append(x_width)
                y_height_all.append(y_height)

        sorted_xmin = sorted(box_xmin)
        sorted_ymin = sorted(box_ymin)
        sorted_xmax = sorted(box_xmax)
        sorted_ymax = sorted(box_ymax)

        x_width_all_sorted = sorted(x_width_all, reverse=True)
        y_height_all_sorted = sorted(y_height_all, reverse=True)
        len_x = len(x_width_all)
        len_y = len(y_height_all)
        x_width_median = np.median(x_width_all_sorted)
        y_height_median = np.median(y_height_all_sorted)

        for i in range(len(sorted_xmin) - 1):
            if sorted_xmin[i + 1] < bounder_w:
                x_distance = abs(sorted_xmin[i + 1] - sorted_xmin[i])
            else:
                x_distance = 0
            if sorted_ymin[i + 1] < bounder_h:
                y_distance = abs(sorted_ymin[i + 1] - sorted_ymin[i])
            else:
                y_distance = 0
            if x_distance > 20:
                ssd_column = ssd_column + 1
                x_distance_all.append(x_distance)
                if x_distance > 2 * x_width_median + 4:
                    count_x = count_x + 1
            if y_distance > 10:
                ssd_row = ssd_row + 1
                y_distance_all.append(y_distance)
                if y_distance > 2 * y_height_median + 3:
                    count_y = count_y + 1
            if x_width_all_sorted[i] - x_width_median > 40:
                ssd_column = ssd_column - 1
            elif x_width_median - x_width_all_sorted[i] > 40:
                ssd_column = ssd_column - 1
            if y_height_all_sorted[i] - y_height_median > 20:
                ssd_row = ssd_row - 1
            elif y_height_median - y_height_all_sorted[i] > 20:
                ssd_row = ssd_row - 1

        if count_x < len(x_distance_all) / 2 + 1:
            ssd_column = ssd_column + count_x
        elif count_y < len(y_distance_all) / 2 + 1:
            ssd_row = ssd_row + count_y

        average_height = int(np.mean(y_height_all))
        average_width = int(np.mean(x_width_all))

        if da_number_w > 1 and da_number_h < 1:
            location_ssd = {'xmin': sorted_xmin[0] + left,
                            'ymin': sorted_ymin[0] + top,
                            'xmax': sorted_xmax[-1] + left,
                            'ymax': h + top}

            choice_m_ssd = {'bounding_box': location_ssd,
                            "single_height": average_height,
                            "single_width": average_width,
                            "rows": ssd_row,
                            "cols": ssd_column,
                            'class_name': 'choice_m',
                            'all_small_coordinate': all_small_coordinate
                            }

        elif da_number_h > 1 and da_number_w < 1:
            location_ssd = {'xmin': sorted_xmin[0] + left,
                            'ymin': sorted_ymin[0] + top,
                            'xmax': w + left,
                            'ymax': sorted_ymax[-1] + top}

            choice_m_ssd = {'bounding_box': location_ssd,
                            "single_height": average_height,
                            "single_width": average_width,
                            "rows": ssd_row,
                            "cols": ssd_column,
                            'class_name': 'choice_m',
                            'all_small_coordinate': all_small_coordinate
                            }
        elif da_number_h > 1 and da_number_w > 1:
            location_ssd = {'xmin': sorted_xmin[0] + left,
                            'ymin': sorted_ymin[0] + top,
                            'xmax': w + left,
                            'ymax': h + top}

            choice_m_ssd = {'bounding_box': location_ssd,
                            "single_height": average_height,
                            "single_width": average_width,
                            "rows": ssd_row,
                            "cols": ssd_column,
                            'class_name': 'choice_m',
                            'all_small_coordinate': all_small_coordinate
                            }
        else:
            location_ssd = {'xmin': sorted_xmin[0] + left,
                            'ymin': sorted_ymin[0] + top,
                            'xmax': sorted_xmax[-1] + left,
                            'ymax': sorted_ymax[-1] + top}

            choice_m_ssd = {'bounding_box': location_ssd,
                            "single_height": average_height,
                            "single_width": average_width,
                            "rows": ssd_row,
                            "cols": ssd_column,
                            'class_name': 'choice_m',
                            'all_small_coordinate': all_small_coordinate
                            }

    else:
        choice_m_ssd = {}
    return choice_m_ssd