"""Resource utility functions."""
from datetime import datetime

import pytz
import tzlocal

from resdk.constants import RESOLWE_DATETIME_FORMAT


def iterate_fields(fields, schema):
    """Recursively iterate over all DictField sub-fields.

    :param fields: Field instance (e.g. input)
    :type fields: dict
    :param schema: Schema instance (e.g. input_schema)
    :type schema: dict

    """
    schema_dict = {val["name"]: val for val in schema}
    for field_id, properties in fields.items():
        if "group" in schema_dict[field_id]:
            for _field_sch, _fields in iterate_fields(
                properties, schema_dict[field_id]["group"]
            ):
                yield (_field_sch, _fields)
        else:
            yield (schema_dict[field_id], fields)


def iterate_schema(fields, schema, path=None):
    """Recursively iterate over all schema sub-fields.

    :param fields: Field instance (e.g. input)
    :type fields: dict
    :param schema: Schema instance (e.g. input_schema)
    :type schema: dict
    :path schema: Field path
    :path schema: string

    """
    for field_schema in schema:
        name = field_schema["name"]
        if "group" in field_schema:
            for rvals in iterate_schema(
                fields[name] if name in fields else {},
                field_schema["group"],
                None if path is None else "{}.{}".format(path, name),
            ):
                yield rvals
        else:
            if path is None:
                yield (field_schema, fields)
            else:
                yield (field_schema, fields, "{}.{}".format(path, name))


def flatten_field(field, schema, path):
    """Reduce dicts of dicts to dot separated keys.

    :param field: Field instance (e.g. input)
    :type field: dict
    :param schema: Schema instance (e.g. input_schema)
    :type schema: dict
    :param path: Field path
    :type path: string
    :return: flattened instance
    :rtype: dictionary

    """
    flat = {}
    for field_schema, fields, current_path in iterate_schema(field, schema, path):
        name = field_schema["name"]
        typ = field_schema["type"]
        label = field_schema["label"]
        value = fields.get(name, None)
        flat[current_path] = {"name": name, "type": typ, "label": label, "value": value}

    return flat


def fill_spaces(word, desired_length):
    """Fill spaces at the end until word reaches desired length."""
    return str(word) + " " * (desired_length - len(word))


def _print_input_line(element_list, level):
    """Pretty print of input_schema."""
    spacing = 2

    for element in element_list:
        if "group" in element:
            print(
                "{}- {} - {}".format("    " * level, element["name"], element["label"])
            )
            _print_input_line(element["group"], level + 1)
        else:
            max_name_len = max([len(elm["name"]) for elm in element_list])
            max_type_len = max(
                [
                    len(elm["type"]) or 0
                    for elm in [e for e in element_list if "group" not in e]
                ]
            )
            print(
                "{}- {} {} - {}".format(