# -*- coding: utf-8 -*-

"""
MushroomPy Module

Functions for interacting with the MushroomPy configuration file. Any configuration 
file defaults and rebuild methods should be added to this file under :code:`update_configfile()`.

Updated January 2021
Tested with:
    Python 3.8, Windows 10

Author Email: yr3g17@soton.ac.uk
"""

__author__ = "Yaseen Reza"


import os
import tkinter as tk
import tkinter.filedialog      


def update_configfile(configdict):
    """Use this method to rebuild the config file from a subset of values provided by the 
    return of the :code:`parse_config()` function.
    
    **Parameters:**

    configdict
        dictionary, empty or containing a subset of configuration keys and values from 
        :code:`parse_config()`.
    
    **Examples:**

    To delete ALL settings, this function should simply be called with an empty dictionary.
    ::

        update_config({})
    
    Alternatively to change a single setting, remove its key from the current configuration dictionary.
    ::

        # Take the current configuration dictionary and remove a key
        currentconfig = parse_configfile()
        del currentconfig["dataroot"]

        # Update the config file to ensure the key/value change persists
        # You will also be prompted to replace the missing key/value pair
        update_config(currentconfig)
    """
    # Find the path of the MushroomPy configuration file
    module_directory = os.path.dirname(__file__)
    config_path = os.path.join(module_directory, "config.ini")

    # ADD LOGIC FOR DEFAULT CONFIGURATIONS HERE

    # dataroot: where the data for input/output operations will be kept
    # Check for key
    if "dataroot" not in configdict.keys():
        configdict["dataroot"] = ""
    # Check for value
    while os.path.exists(configdict["dataroot"]) is False:        
        # Wait for user acknowledgement of prompt
        input("Please specify a valid root directory in which MushroomPy produces and/or "
        "looks for data in - Press [Enter] to browse directories >")
        # Make a top level tkinter window and hide it
        root = tk.Tk()
        root.withdraw()
        # Bring hidden window to "focus"
        root.wm_attributes('-topmost', 1)
        # Populate the key
        configdict["dataroot"] = tkinter.filedialog.askdirectory(
            title="MushroomPy Setup - Choose Data Directory",
            parent=root,
            initialdir=os.path.expanduser("C:"))
        # Get rid of top level tkinter window
        root.destroy()
    
    # next configuration...
    # Check for key
    # ...
    # Check for value
    # ...

    # END OF LOGIC FOR DEFAULT CONFIGURATIONS

    # Configuration dictionary items are copied into the config file
    with open(config_path, "w+") as f:
        f.write("; MushroomPy Configuration\n")
        for _, (k, v) in enumerate(configdict.items()):
            f.write(f"{str(k)}={str(v)}\n")

    return

def parse_configfile():
    """Use this method to return to the caller the contents of the MushroomPy configuration file. 
    
    **Returns:**

    configdict
        dictionary, contains configuration keys and values as generated by :code:`update_configfile()`.
    
    **Example:**
    ::
        print(parse_configfile())
    
    Output:
    ::
        {'dataroot': 'D:/Documents/__UNI__/6013 FEEG Group Design Project/MPy-Data', ...}
    """
    # Find the path of the MushroomPy configuration file
    module_directory = os.path.dirname(__file__)
    config_path = os.path.join(module_directory, "config.ini")

    # If the config file does not exist, create it
    if os.path.isfile(config_path) is False:
        with open(config_path, "w+") as f:
            pass
    
    # Parse the config file into a dictionary
    configdict = {}
    with open(config_path, "r") as f:
        for line in f.readlines():
            if "=" in line:
                set_k, set_v = line[:-1].split("=")
                configdict[set_k] = set_v

    # Update config.ini
    update_configfile(configdict=configdict)

    return configdict

if __name__ == "__main__":
    
    # Automatically perform config.ini setup
    parse_configfile()
