import json
import copy
import numpy as np
import pandas as pd
import config


def init_label_file(name, file_name):
    data = {'name': name,
            'label_num': 0,
            'objects': [],
            'labels': []
            }
    with open(file_name, 'w') as f:
        json.dump(data, f)

    return data


def load_json(file_name):
    with open(file_name, 'r') as f:
        data = json.load(f)
    return data


def save_label(label_json, label_file):
    with open(label_file, 'w') as f:
        json.dump(label_json, f)
    # add label to the last element


def init_canvas_json():
    data = {'version': "4.4.0",
            'objects': []
            }
    return data


def update_label_json(objects, label_dict, label):
    print(f"\n\n---------------------label")

    # delete one object
    for index, label_object in enumerate(label_dict["objects"]):
        # label_objects = label_objects.copy()
        if label_object not in objects:
            del label_dict["objects"][index]
            del label_dict["labels"][index]

    # add new object into the label_dict
    for index, object in enumerate(objects):
        # create a label_object
        # label_object = {"label": label,
        #                 "type": object["type"],
        #                 "version": object["version"],
        #                 "originX": object["originX"],
        #                 "originY": object["originY"],
        #                 "left": object["left"],
        #                 "top": object["top"],
        #                 "width": object["width"],
        #                 "height": object["height"],
        #                 "fill": object["fill"],
        #                 "stroke": object["stroke"],
        #                 "strokeWidth": object["strokeWidth"],
        #                 "strokeDashArray": object["strokeDashArray"],
        #                 "strokeLineCap": object["strokeLineCap"],
        #                 "strokeDashOffset": object["strokeDashOffset"],
        #                 "strokeLineJoin": object["strokeLineJoin"],
        #                 "strokeUniform": object["strokeUniform"],
        #                 "strokeMiterLimit": object["strokeMiterLimit"],
        #                 "scaleX": object["scaleX"],
        #                 "scaleY": object["scaleY"],
        #                 "angle": object["angle"],
        #                 "flipX": object["flipX"],
        #                 "flipY": object["flipY"],
        #                 "opacity": object["opacity"],
        #                 "shadow": object["shadow"],
        #                 "visible": object["visible"],
        #                 "backgroundColor": object["backgroundColor"],
        #                 "fillRule": object["fillRule"],
        #                 "paintFirst": object["paintFirst"],
        #                 "globalCompositeOperation": object["globalCompositeOperation"],
        #                 "skewX": object["skewX"],
        #                 "skewY": object["skewY"],
        #                 "rx": object["rx"],
        #                 "ry": object["ry"]
        #                 }
        if object not in label_dict['objects']:
            label_dict['objects'].append(object)
            label_dict['labels'].append(label)
            print(f"add one object to label dict: {label}")

    return label_dict


def get_labels_pd(label_dict):
    objects_pd = pd.json_normalize(label_dict['objects'])

    pos_pd = objects_pd[["left", "top", "width", "height"]]
    print(f"labels:{label_dict['labels']}")
    print(f'pos_pd:{pos_pd}')
    pos_pd.insert(0, "Label", label_dict['labels'])
    # for col in labels_pd.select_dtypes(include=['object']).columns:
    #     labels_pd[col] = labels_pd[col].astype("str")
    return pos_pd

import json
import copy
import numpy as np
import pandas as pd
import config


def init_label_file(name, file_name):
    data = {'name': name,
            'label_num': 0,
            'objects': [],
            'labels': []
            }
    with open(file_name, 'w') as f:
        json.dump(data, f)

    return data


def load_json(file_name):
    with open(file_name, 'r') as f:
        data = json.load(f)
    return data


def save_label(label_json, label_file):
    with open(label_file, 'w') as f:
        json.dump(label_json, f)
    # add label to the last element


def init_canvas_json():
    data = {'version': "4.4.0",
            'objects': []
            }
    return data


def update_label_json(objects, label_dict, label):
    print(f"\n\n---------------------label")

    # delete one object
    for index, label_object in enumerate(label_dict["objects"]):
        # label_objects = label_objects.copy()
        if label_object not in objects:
            del label_dict["objects"][index]
            del label_dict["labels"][index]

    # add new object into the label_dict
    for index, object in enumerate(objects):
        # create a label_object
        # label_object = {"label": label,
        #                 "type": object["type"],
        #                 "version": object["version"],
        #                 "originX": object["originX"],
        #                 "originY": object["originY"],
        #                 "left": object["left"],
        #                 "top": object["top"],
        #                 "width": object["width"],
        #                 "height": object["height"],
        #                 "fill": object["fill"],
        #                 "stroke": object["stroke"],
        #                 "strokeWidth": object["strokeWidth"],
        #                 "strokeDashArray": object["strokeDashArray"],
        #                 "strokeLineCap": object["strokeLineCap"],
        #                 "strokeDashOffset": object["strokeDashOffset"],
        #                 "strokeLineJoin": object["strokeLineJoin"],
        #                 "strokeUniform": object["strokeUniform"],
        #                 "strokeMiterLimit": object["strokeMiterLimit"],
        #                 "scaleX": object["scaleX"],
        #                 "scaleY": object["scaleY"],
        #                 "angle": object["angle"],
        #                 "flipX": object["flipX"],
        #                 "flipY": object["flipY"],
        #                 "opacity": object["opacity"],
        #                 "shadow": object["shadow"],
        #                 "visible": object["visible"],
        #                 "backgroundColor": object["backgroundColor"],
        #                 "fillRule": object["fillRule"],
        #                 "paintFirst": object["paintFirst"],
        #                 "globalCompositeOperation": object["globalCompositeOperation"],
        #                 "skewX": object["skewX"],
        #                 "skewY": object["skewY"],
        #                 "rx": object["rx"],
        #                 "ry": object["ry"]
        #                 }
        if object not in label_dict['objects']:
            label_dict['objects'].append(object)
            label_dict['labels'].append(label)
            print(f"add one object to label dict: {label}")

    return label_dict


def get_labels_pd(label_dict):
    objects_pd = pd.json_normalize(label_dict['objects'])

    pos_pd = objects_pd[["left", "top", "width", "height"]]
    print(f"labels:{label_dict['labels']}")
    print(f'pos_pd:{pos_pd}')
    pos_pd.insert(0, "Label", label_dict['labels'])
    # for col in labels_pd.select_dtypes(include=['object']).columns:
    #     labels_pd[col] = labels_pd[col].astype("str")
    return pos_pd

# add label to the last element # delete one object # label_objects = label_objects.copy() # add new object into the label_dict # create a label_object # label_object = {"label": label, #                 "type": object["type"], #                 "version": object["version"], #                 "originX": object["originX"], #                 "originY": object["originY"], #                 "left": object["left"], #                 "top": object["top"], #                 "width": object["width"], #                 "height": object["height"], #                 "fill": object["fill"], #                 "stroke": object["stroke"], #                 "strokeWidth": object["strokeWidth"], #                 "strokeDashArray": object["strokeDashArray"], #                 "strokeLineCap": object["strokeLineCap"], #                 "strokeDashOffset": object["strokeDashOffset"], #                 "strokeLineJoin": object["strokeLineJoin"], #                 "strokeUniform": object["strokeUniform"], #                 "strokeMiterLimit": object["strokeMiterLimit"], #                 "scaleX": object["scaleX"], #                 "scaleY": object["scaleY"], #                 "angle": object["angle"], #                 "flipX": object["flipX"], #                 "flipY": object["flipY"], #                 "opacity": object["opacity"], #                 "shadow": object["shadow"], #                 "visible": object["visible"], #                 "backgroundColor": object["backgroundColor"], #                 "fillRule": object["fillRule"], #                 "paintFirst": object["paintFirst"], #                 "globalCompositeOperation": object["globalCompositeOperation"], #                 "skewX": object["skewX"], #                 "skewY": object["skewY"], #                 "rx": object["rx"], #                 "ry": object["ry"] #                 } # for col in labels_pd.select_dtypes(include=['object']).columns: #     labels_pd[col] = labels_pd[col].astype("str")