from pathlib import Path
import fnmatch
import os
import shutil

# SETTINGS ###################################################################################
# path of origin images folder
path_images_origin = Path("Image_database/gen_03/merge_images_without_duplicate")
# path of origin labels folder
path_labels_origin = Path("Image_database/gen_03/merge_labels_without_duplicate")
# path of origin parameters folder
path_parameters_origin = Path("Image_database/gen_03/merge_parameters_without_duplicate")

# path of picked images for reverse search
path_images_search = Path("Image_database/gen_04/new_iter_images")

# path of images folder without picked images
path_images_without_search = Path("Image_database/gen_04/previous_gen_without_new_iter/images")
# path of labels folder without picked images
path_labels_without_search = Path("Image_database/gen_04/previous_gen_without_new_iter/labels")
# path of parameters without picked images
path_parameters_without_search = Path("Image_database/gen_04/previous_gen_without_new_iter/parameters")
##############################################################################################


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


# GET DATA ###################################################################################
# find all labels, images and parameters in origin path folder
labels_array_origin = listdir(str(path_labels_origin), "*.txt")
parameters_array_origin = listdir(str(path_parameters_origin), "*.txt")
images_array_origin = listdir(str(path_images_origin), "*.jpg")

# names of images and labels must be the same
valid = 1
if len(labels_array_origin) == len(images_array_origin) == len(parameters_array_origin):
    for i in range(len(labels_array_origin)):
        name1 = labels_array_origin[i][:-4]
        name2 = images_array_origin[i][:-4]
        name3 = parameters_array_origin[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.")

# find all images in folder for search
images_array_search = listdir(str(path_images_search), "*.jpg")
##############################################################################################

if valid:
    # GET PARAMETERS #########################################################################
    for i in range(len(images_array_origin)):
        in_search = 0
        for j in range(len(images_array_search)):
            if images_array_origin[i] == images_array_search[j]:
                in_search = 1
                break
        if in_search == 0:
            shutil.copy(str(path_images_origin) + '/' + str(images_array_origin[i]),
                        str(path_images_without_search))
            shutil.copy(str(path_labels_origin) + '/' + str(labels_array_origin[i]),
                        str(path_labels_without_search))
            shutil.copy(str(path_parameters_origin) + '/' + str(parameters_array_origin[i]),
                        str(path_parameters_without_search))
##############################################################################################
