import filecmp
import os
import fnmatch
from pathlib import Path
import shutil

# SETTINGS ###################################################################################
# MODE 0-analyze
# 1-write non duplicate images to new folder
# 2-write non duplicate images+labels to new folder
# 3-write non duplicate images+labels+parameters to new folder

mode = 2
##############################################################################################
# path of images folder
path_images = Path("Image_database/output_gen_00-04/images_plus_insect")
# path of labels folder
path_labels = Path("Image_database/output_gen_00-04/labels_plus_insect")
# path of parameters
path_parameters = Path("Image_database/output_gen_00-04/")
##############################################################################################
# path of new images folder
path_images_new = Path("Image_database/output_gen_00-04/images_plus_insect_non_duplicate")
# path of new labels folder
path_labels_new = Path("Image_database/output_gen_00-04/labels_plus_insect_non_duplicate")
# path of new parameters
path_parameters_new = Path("Image_database/gen_03/merge_parameters_without_duplicate")
##############################################################################################


# get all files of defined type from folder
def listdir(dir_name, pattern="*"):
    return fnmatch.filter(os.listdir(dir_name), pattern)


# find all labels, images and parameters in path folder
images_array = listdir(str(path_images), "*.jpg")
if mode == 3:
    labels_array = listdir(str(path_labels), "*.txt")
    parameters_array = listdir(str(path_parameters), "*.txt")

    # names of images and labels must be the same
    valid = 1
    if len(labels_array) == len(images_array) == len(parameters_array):
        for i in range(len(labels_array)):
            name1 = labels_array[i][:-4]
            name2 = images_array[i][:-4]
            name3 = parameters_array[i][:-4]
            if (name1 != name2) or (name2 != name3[:len(name3) - 6]):
                valid = 0
                break
    else:
        valid = 0

    if valid == 1:
        print("Name arrays of images and labels are the same => valid.")
    else:
        print("Name arrays of images and labels are not the same => invalid.")

if mode == 2:
    labels_array = listdir(str(path_labels), "*.txt")

    # names of images and labels must be the same
    valid = 1
    if len(labels_array) == len(images_array):
        for i in range(len(labels_array)):
            name1 = labels_array[i][:-4]
            name2 = images_array[i][:-4]
            if name1 != name2:
                valid = 0
                break
    else:
        valid = 0

    if valid == 1:
        print("Name arrays of images and labels are the same => valid.")
    else:
        print("Name arrays of images and labels are not the same => invalid.")

duplicate = []
same_counter = 0
print(len(images_array))
for i in range(len(images_array)):
    pic1 = (str(path_images) + '/' + str(images_array[i]))
    for j in range(i+1, len(images_array)):
        pic2 = (str(path_images) + '/' + str(images_array[j]))
        if i != j:
            if filecmp.cmp(pic1, pic2):
                same_counter += 1
                print("same files num. "+str(same_counter)+":")
                print(pic1)
                print(pic2)
                print("")
                duplicate.append(images_array[j])


not_copy = len(duplicate)
print("start copy of unique images")
for i in range(len(images_array)):
    if images_array[i] not in duplicate:
        pic = (str(path_images) + '/' + str(images_array[i]))
        shutil.copy(str(pic),
                    str(path_images_new))
        if mode == 2:
            label = (str(path_labels) + '/' + str(labels_array[i]))
            shutil.copy(str(label),
                        str(path_labels_new))

        if mode == 3:
            label = (str(path_labels) + '/' + str(labels_array[i]))
            parameters = (str(path_parameters) + '/' + str(parameters_array[i]))
            shutil.copy(str(label),
                        str(path_labels_new))
            shutil.copy(str(parameters),
                        str(path_parameters_new))
