# This file is the main part for keeping the CSV and saving them with their original names.

import glob

from enums.Args import Args
from enums.Column import Column
from enums.Extension import Extension
from pathlib import Path
from polars import scan_csv

class LazyFrame(object):
    def __init__(self, pathList):
        # Handle the lazyFrames for processing.
        self.lazyFrames = list()

        # Handle the names for saving.
        self.names = list()

        # This condition should be only accessed when executing the benchmark.
        if (type(pathList) != list):
            path = list()
            path.append(pathList)
            pathList = path

        # For each path
        for path in pathList:
            path = Path(path)

            # For each extension.
            for extension in Extension:
                
                # Path is a file.
                if (path.is_file() and path.suffix == extension.value):
                    self.names.append(path.name)
                    self.lazyFrames.append(scan_csv(path, ignore_errors = Args.IGNORE_ERRORS.value,
                        sep = Args.SEPARATOR.value))
                
                # Path is a directory.
                elif (path.is_dir()):

                    # For each file of the directory.
                    for file in path.glob(Args.WILDCARD.value + extension.value):
                        self.names.append(file.name)
                        self.lazyFrames.append(scan_csv(file,
                            ignore_errors = Args.IGNORE_ERRORS.value, sep = Args.SEPARATOR.value))

                # Path is a regex.
                elif (glob.glob(str(path))):

                    # For each result in the regexp.
                    for result in glob.glob(str(path)):
                        result = Path(result)

                        # Result is a file.
                        if (result.is_file() and result.suffix == extension.value):
                            self.names.append(result.name)
                            self.lazyFrames.append(scan_csv(result,
                            ignore_errors = Args.IGNORE_ERRORS.value,
                            sep = Args.SEPARATOR.value))
                
                        # Result is a directory.
                        elif (result.is_dir()):

                            # For each file of the directory.
                            for file in path.glob(Args.WILDCARD.value + extension.value):
                                self.names.append(file.name)
                                self.lazyFrames.append(scan_csv(file,
                                    ignore_errors = Args.IGNORE_ERRORS.value,
                                    sep = Args.SEPARATOR.value))

                # The path is not valid. Skip it.
                else:
                    print("The path \"" + str(path) + "\" is not a valid path. It will not be processed.")
                self.verifyColumns()

    # Since CSV are all the same, we know the order of columns sometimes there is some CSV without
    # columns names this function will add the columns names if missing.
    def verifyColumns(self):
        columns = list()
        for column in Column:
            columns.append(column.value)
        for i in range(len(self.lazyFrames)):
            if (self.lazyFrames[i].columns != columns):
                self.lazyFrames[i].columns = columns

    # Collect the processed lazyframes (Polars will optimize) then save them in ../resources/csv
    # folder with their original names.
    # 
    # Uncomment the line in green in order to do parallelization with Polars, replace
    # range(len(self.lazyFrames)) by range(len(dataFrames)), delete
    # "self.lazyFrames[i] = self.lazyFrames[i].collect()" and replace self.lazyFrames[i] by
    # dataFrames[i].

    # It's in comment since it's "Process stopped" on my side.
    def save(self):
        #dataFrames = collect_all(self.lazyFrames)
        for i in range(len(self.lazyFrames)):
            print("Doing work for " + self.names[i])
            self.lazyFrames[i] = self.lazyFrames[i].collect()
            self.lazyFrames[i].to_csv(Args.SAVING_FOLDER.value + self.names[i],
                sep = Args.SEPARATOR.value)