import os

from _file_stuff import is_file

basedir = os.path.dirname(__file__)

class FindFiles:
    def __init__(self,
                 search_types,
                 src_dir,
                 file_total,
                 file_types,
                 *args,
                 **kwargs):
        super(FindFiles,self).__init__(*args,**kwargs)

        self.file_total                 = file_total
        self.src_dir                    = src_dir
        self.search_types               = search_types
        self.file_types                 = file_types

        self.file_name                  = None
        self.path_file_source           = None
        self.file_type                  = None
        self.file_ext                   = None

    def t_find_files(self,
                     progress_callback,
                     current_file_progress_callback,
                     imported_file_count_callback,
                     found_file_callback,
                     total_file_count_callback):
        file_count = int(0)
        if len(self.search_types) > 0:
            for folder, subfolders, filename in os.walk(self.src_dir):
                for self.file_type in self.search_types:
                    for self.file_ext in self.file_types[self.file_type]:
                        for self.file_name in filename:
                            if self.file_name.lower().endswith(self.file_ext):
                                self.path_file_source = os.path.join(folder, self.file_name)
                                if is_file(self.path_file_source):
                                    file_count += int(1)
                                    # process_file(current_file)
                                    found_file_callback.emit(self.path_file_source)
                                else:
                                    print(f"Skipping {self.path_file_source} as it does not look like a real file.")
                                total_file_count_callback.emit(file_count)
                                progress_callback.emit(round((file_count / self.file_total) * 100, 0))
        else:
            print("Nothing to search for.")

