from glob import glob
import numpy as np
import pandas as pd


def add_label(csv_path, output_folder, label):
    """Add column with classification label in a csv file.

    Arguments:
        csv_path {str} -- path of csv file
        output_folder {str} -- path where the new csv will be saved
        label {srt} -- classification label
    """
    csv_name = csv_path.split("/")[-1]
    df = pd.read_csv(csv_path, index_col=False)
    df["class"] = label
    df.to_csv(f"{output_folder}/{csv_name}", index=False)


def convert_to_one(dataset_folder):
    """Concatenate all files of type csv, which it was generated by the input dataset, \
        to a single one.

    Arguments:
        dataset_folder {str} -- path to the folder with all csv files
    """
    folder_name = dataset_folder.split("/")[-2]
    csv_list = [csv for csv in glob(dataset_folder + "/*.csv")]
    combine_csv = pd.concat([pd.read_csv(csv, index_col=False) for csv in csv_list])
    combine_csv.to_csv(f"{dataset_folder}/{folder_name}_dataset.csv", index=False)


def mount_dataset(dataset_list):
    """Concatenate the main csv files from the datasets to a single one file, \
        where it will be used to train the model.
    """
    dataset_list = dataset_list.split(",")
    csv_list = [f"data/{dataset}/{dataset}_dataset.csv" for dataset in dataset_list]
    concat_csv = pd.concat([pd.read_csv(csv, index_col=False) for csv in csv_list])
    concat_csv.to_csv(f"data/dataset.csv", index=False)


def get_row(dataframe, position):
    """Select a row in csv file

    Arguments:
        path {str} -- path to the csv file
        position {int} -- position of the row

    Returns:
        numpy.ndarray -- array of landmarks of a frame (row)
    """
    selected_row = dataframe.iloc[position].values.tolist()
    return np.array([selected_row[i:i+2] for i in range(0, 42, 2)])
