#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# */AIPND-revision/intropyproject-classify-pet-images/get_pet_labels.py
#                                                                             
# PROGRAMMER: Josephine H.
# DATE CREATED: 10/7/2021                                 
# REVISED DATE: 
# PURPOSE: Create the function get_pet_labels that creates the pet labels from 
#          the image's filename. This function inputs: 
#           - The Image Folder as image_dir within get_pet_labels function and 
#             as in_arg.dir for the function call within the main function. 
#          This function creates and returns the results dictionary as results_dic
#          within get_pet_labels function and as results within main. 
#          The results_dic dictionary has a 'key' that's the image filename and
#          a 'value' that's a list. This list will contain the following item
#          at index 0 : pet image label (string).
#
##
# Imports python modules
from os import listdir

# Define get_pet_labels function below 
# 
def get_pet_labels(image_dir):
    """
    Creates a dictionary of pet labels (results_dic) based upon the filenames 
    of the image files. These pet image labels are used to check the accuracy 
    of the labels that are returned by the classifier function, since the 
    filenames of the images contain the true identity of the pet in the image.
    Be sure to format the pet labels so that they are in all lower case letters
    and with leading and trailing whitespace characters stripped from them.
    (ex. filename = 'Boston_terrier_02259.jpg' Pet label = 'boston terrier')
    Parameters:
     image_dir - The (full) path to the folder of images that are to be
                 classified by the classifier function (string)
    Returns:
      results_dic - Dictionary with 'key' as image filename and 'value' as a 
      List. The list contains for following item:
         index 0 = pet image label (string)
    """
    # Creates empty dictionary for the results (pet labels, etc.)
    results_dict = {}
    # Create empty dist for all pet labels
    pet_labels = []
    
    # obtain the dict keys as a list from filenames of the image files
    filename_list = listdir(image_dir)
    
    def get_label_name(filename):
        """
        process filename in the filename_list to extract the dog breed name.
        Parameters: 
            filename in the directory, all filenames are in the format of "Pet_Name_#####.jpg" (string)

        Returns:
            pet label extracted from filenmae (string)

        """
        str1 = '_'
        locator = filename.rfind(str1)
        raw_filename = filename[:locator]
        raw_filename1 = raw_filename.replace("_"," ") 
        pet_name = raw_filename1.lower().strip()
        return pet_name    
    
    # alternative way to get label name
    #low_pet_image = pet_image.lower
    #word_list_pet_image = low_pet_image.split("_")
    #pet_name = ""
    #for word in word_list_pet_image:
    #    if word.isalpha():  #isalpha() returns true only when a string contains only alphabetic characters.
    #        pet_name += word + " "
    
    
    for idx in range(len(filename_list)): 
        # Skips file if starts with . (like .DS_Store of Mac OSX) because it isn't an pet image file
        if filename_list[idx][0] != ".":
            # use get_label_name function to pet label name for the corresponding filename
            pet_label = get_label_name(filename_list[idx])
            
           # If filename doesn't already exist in dictionary add it and it's pet label - otherwise print an error message
            if filename_list[idx] not in results_dict:
                results_dict[filename_list[idx]] = [pet_label]
                pet_labels.append(pet_label)
            else:
                print("\n** Warning: Key={} already exists in results_dict".format(filename_list[idx]))
    
    return results_dict

        
        
        
        
