import logging
import random


def divide_set(filenames, labels):
    """
    Divide Training set

    Function that takes training set
    and randomly divides it in two parts:
    training set  -- 80%
    validation set -- 20%

        Args:
        - filnames: array of paths to training images
        - labels: array of class-probabilities for each training image

        Returns:
        - train_filenames: 80% of images
        - train_labels: probabilities of the training images
        - valid_filenames: 20% of images
        - valid_labels: probabilities of the validation images
    """
    # Config Logging
    logging.basicConfig(level=logging.INFO)

    # Get number of images
    dataset_size = len(filenames)

    # Create dictionary filename: it's scores and shuffle filenames
    dictionary = dict(zip(filenames, labels))
    random.shuffle(filenames)

    train_filenames = []
    train_labels = []

    valid_filenames = []
    valid_labels = []

    # Calculate 80% and 20% of number of images
    train_dataset_size = dataset_size * 8 // 10
    valid_dataset_size = dataset_size - train_dataset_size

    # Log number of train and valid samples
    logging.info("-----------------------------------------")
    logging.info("   Found {0} images for training dataset".format(train_dataset_size))
    logging.info("   Found {0} images for validation dataset".format(valid_dataset_size))
    logging.info("-----------------------------------------")

    # Split samples in Train and Val sets
    for i, file in enumerate(filenames):
        if i <= train_dataset_size:
            train_filenames.append(file)
            train_labels.append(dictionary[file])
        else:
            valid_filenames.append(file)
            valid_labels.append(dictionary[file])

    return train_filenames, train_labels, valid_filenames, valid_labels