__author__ = "Christian Kongsgaard"
__license__ = "MIT"
__version__ = "0.0.1"

# -------------------------------------------------------------------------------------------------------------------- #
# IMPORTS

# Modules:


# RiBuild Modules:
from delphin_6_automation.simulation.nosql.db_templates import delphin_entry as delphin_db
from delphin_6_automation.simulation.nosql.db_templates import result_entry as result_db
from delphin_6_automation.simulation.database_interactions import delphin_interactions as delphin_interact

# -------------------------------------------------------------------------------------------------------------------- #
# DATABASE INTERACTIONS


def gather_material_list(delphin_id: str) -> list:
    """
    Gathers the material file names of Delphin file in the database
    :param delphin_id: database id
    :return: list of material file names
    """

    delphin_document = delphin_db.Delphin.objects(id=delphin_id).first()

    material_list = []
    for material_dict in delphin_document['dp6_file']['DelphinProject']['Materials']['MaterialReference']:
        material_list.append(material_dict['#text'].split('/')[-1])

    return material_list


def download_raw_result(result_id: str, download_path: str) -> bool:
    """
    Downloads a result entry from the database
    :param result_id: Database entry id
    :param download_path: Path where the result should be written
    :return: True
    """

    result_obj = result_db.Result.objects(id=result_id).first()

    delphin_interact.write_log_files(result_obj, download_path)
    delphin_interact.download_result_files(result_obj, download_path)

    return True


def queue_priorities(priority: str)-> int:
    """
    Generate a queue priority number
    :param priority: High, medium or low priority
    :return: Priority number
    """

    priority_list = [obj.queue_priority
                     for obj in delphin_db.Delphin.objects.order_by('queue_priority')]

    min_priority = min(priority_list)
    max_priority = max(priority_list)
    span = max_priority - min_priority

    if priority == 'high':
        priority_number = int(max_priority)

    elif priority == 'medium':
        priority_number = int(span * 0.5 + min_priority)

    elif priority == 'low':
        priority_number = int(span * 0.25 + min_priority)

    else:
        raise ValueError('priority has to be: high, medium or low. Value given was: ' + str(priority))

    return priority_number


def add_to_simulation_queue(delphin_file: str, priority: str)-> str:
    """
    Uploads and adds a Delphin project file to the simulation queue.
    :param delphin_file: Delphin 6 project file path
    :param priority: High, medium or low priority
    :return: Database entry id
    """

    priority_number = queue_priorities(priority)
    simulation_id = delphin_interact.upload_to_database(delphin_file, priority_number)

    return simulation_id


def is_simulation_finished(sim_id: str) -> bool:
    """
    Checks if a Delphin project entry is simulated or not.
    :param sim_id: Database entry to check
    :return: True if it is simulated otherwise returns False.
    """

    object_ = delphin_db.Delphin.objects(id=sim_id).first()

    if object_.simulated:
        return True
    else:
        return False


def gather_weather_list(delphin_id: str) -> list:
    """
    Gathers the weather files names of Delphin file in the database
    :param delphin_id: database id
    :return: list of weather file names
    """

    delphin_document = delphin_db.Delphin.objects(id=delphin_id).first()

    weather_list = []
    for weather_dict in delphin_document['dp6_file']['DelphinProject']['Conditions']['ClimateConditions']['ClimateCondition']:
        # TODO - Create genexpression instead
        weather_list.append((weather_dict['@name'],
                             weather_dict['@type'],
                             weather_dict['@kind'])
                            )

    return weather_list


def download_materials(material_list, path):
    return None


def download_weather(weather_list, path):
    return None

@@ -3596,26 +3596,17 @@
 list = %5B
-%5D%0A    for 
+(
 weather_
@@ -3613,114 +3613,18 @@
 dict
- in delphin_document%5B'dp6_file'%5D%5B'DelphinProject'%5D%5B'Conditions'%5D%5B'ClimateConditions'%5D%5B'ClimateCondition'%5D:
+%5B'@name'%5D,
 %0A   
@@ -3632,75 +3632,21 @@
     
-# TODO - Create genexpression instead%0A        weather_list.append((
+             
 weat
@@ -3656,32 +3656,24 @@
 _dict%5B'@
-nam
+typ
 e'%5D,%0A
-        
         
@@ -3692,39 +3692,39 @@
  weather_dict%5B'@
-type'%5D,
+kind'%5D)
 %0A               
@@ -3724,32 +3724,27 @@
             
-        
+for
  weather_dic
@@ -3748,49 +3748,171 @@
 dict
-%5B'@kind'%5D)%0A                            )%0A
+ in delphin_document%5B'dp6_file'%5D%5B'DelphinProject'%5D%5B'Conditions'%5D%0A                                                        %5B'ClimateConditions'%5D%5B'ClimateCondition'%5D%5D
 %0A   
@@ -3971,32 +3971,43 @@
 al_list, path):%0A
+    # TODO%0A
     return None%0A
@@ -4004,24 +4004,24 @@
 turn None%0A%0A%0A
-
 def download
@@ -4046,24 +4046,35 @@
 ist, path):%0A
+    # TODO%0A
     return N

update to change_weather_list