from yaml import safe_load
import json
from heatv1 import common
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode

HEAT_ROOT = "/srv/heat/env"

TEMPLATE_PATH = "template"
ENV_PATH = "env"


def _read_env_file(name):
    path = "/".join([
        HEAT_ROOT,
        ENV_PATH,
        name])

    return _read_file(path)


def _read_template_file(name):
    path = "/".join([
        HEAT_ROOT,
        TEMPLATE_PATH,
        name])

    return _read_file(path)


def _read_file(full_path):
    with open(full_path, 'r') as f:
        data = safe_load(f)
        return json.dumps(data, default=str)


def _read_additional_file(path):
    full_path = "/".join([
        HEAT_ROOT,
        path])
    with open(full_path) as f:
        return str(f.read())


@common.send("get")
def stack_list(**kwargs):
    url = "/stacks?{}".format(urlencode(kwargs))
    return url, {}


@common.get_by_name_or_uuid(stack_list, 'stacks')
@common.send("get")
def stack_show(stack_id, **kwargs):
    stack_name = kwargs.get("name")
    url = "/stacks/{stack_name}/{stack_id}".format(
        stack_name=stack_name, stack_id=stack_id)
    return url, {}


@common.get_by_name_or_uuid(stack_list, 'stacks')
@common.send("delete")
def stack_delete(stack_id, **kwargs):
    stack_name = kwargs.get("name")
    url = "/stacks/{stack_name}/{stack_id}".format(stack_name=stack_name,
                                                   stack_id=stack_id)
    return url, {}


@common.send("post")
def stack_create(name, template=None, environment=None, environment_files=None,
                 files=None, parameters=None, template_url=None,
                 timeout_mins=5, disable_rollback=True, **kwargs):
    url = "/stacks"
    request = {'stack_name': name,
               'timeout_mins': timeout_mins,
               'disable_rollback': disable_rollback}
    if environment:
        request["environment"] = environment
    file_items = {}
    if environment_files:
        env_names = []
        env_files = {}
        for f_name in environment_files:
            data = _read_env_file(f_name)
            env_files[f_name] = data
            env_names.append(f_name)
        file_items.update(env_files)
        request["environment_files"] = env_names
    if files:
        for f_name, path in files.items():
            file_items.update((f_name, _read_additional_file(path)))
    if file_items:
        request["files"] = file_items
    if parameters:
        request["parameters"] = parameters
    if template:
        template_file = _read_template_file(template)
        request["template"] = template_file
    if template_url:
        request["template_url"] = template_url
    # Validate the template and get back the params.

    return url, {"json": request}


@common.get_by_name_or_uuid(stack_list, 'stacks')
@common.send("put")
def stack_update(stack_id, template=None, environment=None,
                 environment_files=None, files=None, parameters=None,
                 template_url=None, timeout_mins=5, disable_rollback=True,
                 clear_parameters=None, **kwargs):
    stack_name = kwargs.get("name")
    url = "/stacks/{stack_name}/{stack_id}".format(
        stack_name=stack_name, stack_id=stack_id
    )
    request = {'stack_name': stack_name,
               'timeout_mins': timeout_mins,
               'disable_rollback': disable_rollback}
    if environment:
        request["environment"] = environment
    file_items = {}
    if environment_files:
        env_names = []
        env_files = {}
        for f_name in environment_files:
            data = _read_env_file(f_name)
            env_files[f_name] = data
            env_names.append(f_name)
        file_items.update(env_files)
        request["environment_files"] = env_names
    if files:
        for f_name, path in files.items():
            file_items.update((f_name, _read_additional_file(path)))
    if file_items:
        request["files"] = file_items
    if parameters:
        request["parameters"] = parameters
    if template:
        template_file = _read_template_file(template)
        request["template"] = template_file
    if template_url:
        request["template_url"] = template_url
    if clear_parameters:
        request["clear_parameters"] = clear_parameters
    return url, {"json": request}
