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

"""
awsscripter.template

This module implements a Template class, which stores a CloudFormation template
and implements methods for uploading it to S3.
"""

import imp
import logging
import os
import sys
import threading

import botocore
import jinja2

from awsscripter.common.exceptions import TemplateawsscripterHandlerError
from awsscripter.common.exceptions import UnsupportedTemplateFileTypeError


class Template(object):
    """
    Template represents an AWS CloudFormation template. It is responsible for
    loading, storing, and optionally uploading, local templates for use by the
    CloudFormation service.

    :param path: The absolute path to the file which stores the template.
    :type path: str
    :param awsscripter_user_data: A dictionary of arbitrary data to be passed to \
        a handler function in an external Python script.
    :type awsscripter_user_data: dict
    """

    _boto_s3_lock = threading.Lock()

    def __init__(
        self, path, awsscripter_user_data, connection_manager=None, s3_details=None
    ):
        self.logger = logging.getLogger(__name__)

        self.path = path
        self.awsscripter_user_data = awsscripter_user_data
        self.connection_manager = connection_manager
        self.s3_details = s3_details

        self.name = os.path.basename(path).split(".")[0]
        self._body = None

    def __repr__(self):
        return (
            "awsscripter.template.Template(name='{0}', path='{1}', "
            "awsscripter_user_data={2}, s3_details={3})".format(
                self.name, self.path, self.awsscripter_user_data, self.s3_details
            )
        )

    @property
    def body(self):
        """
        Represents body of the CloudFormation template.

        :returns: The body of the CloudFormation template.
        :rtype: str
        """
        if self._body is None:
            file_extension = os.path.splitext(self.path)[1]

            if file_extension in {".json", ".yaml"}:
                with open(self.path) as template_file:
                    self._body = template_file.read()
            elif file_extension == ".j2":
                self._body = self._render_jinja_template(
                    os.path.dirname(self.path),
                    os.path.basename(self.path),
                    {"awsscripter_user_data": self.awsscripter_user_data}
                )
            elif file_extension == ".py":
                self._body = self._call_awsscripter_handler()

            else:
                raise UnsupportedTemplateFileTypeError(
                    "Template has file extension %s. Only .py, .yaml, "
                    ".json and .j2 are supported.",
                    os.path.splitext(self.path)[1]
                )
        return self._body

    def _call_awsscripter_handler(self):
        """
        Calls the function `awsscripter_handler` within templates that are python
        scripts.

        :returns: The string returned from awsscripter_handler in the template.
        :rtype: str
        :raises: IOError
        :raises: TemplateawsscripterHandlerError
        """
        # Get relative path as list between current working directory and where
        # the template is
        # NB: this is a horrible hack...
        relpath = os.path.relpath(self.path, os.getcwd()).split(os.path.sep)
        relpaths_to_add = [
            os.path.sep.join(relpath[:i+1])
            for i in range(len(relpath[:-1]))
        ]
        # Add any directory between the current working directory and where
        # the template is to the python path
        for directory in relpaths_to_add:
            sys.path.append(os.path.join(os.getcwd(), directory))
        self.logger.debug(
            "%s - Getting CloudFormation from %s", self.name, self.path
        )

        if not os.path.isfile(self.path):
            raise IOError("No such file or directory: '%s'", self.path)

        module = imp.load_source(self.name, self.path)

        try:
            body = module.awsscripter_handler(self.awsscripter_user_data)
        except AttributeError as e:
            if 'awsscripter_handler' in str(e):
                raise TemplateawsscripterHandlerError(
                    "The template does not have the required "
                    "'awsscripter_handler(awsscripter_user_data)' function."
                )
            else:
                raise e
        for directory in relpaths_to_add:
            sys.path.remove(os.path.join(os.getcwd(), directory))
        return body

    def upload_to_s3(self):
        """
        Uploads the template to ``bucket_name`` and returns its URL.

        The template is uploaded with the ``bucket_key``.

        :returns: The URL of the template object in S3.
        :rtype: str
        :raises: botocore.exceptions.ClientError

        """
        self.logger.debug("%s - Uploading template to S3...", self.name)

        with self._boto_s3_lock:
            if not self._bucket_exists():
                self._create_bucket()

        # Remove any leading or trailing slashes the user may have added.
        bucket_name = self.s3_details["bucket_name"]
        bucket_key = self.s3_details["bucket_key"]

        self.logger.debug(
            "%s - Uploading template to: 's3://%s/%s'",
            self.name, bucket_name, bucket_key
        )
        self.connection_manager.call(
            service="s3",
            command="put_object",
            kwargs={
                "Bucket": bucket_name,
                "Key": bucket_key,
                "Body": self.body,
                "ServerSideEncryption": "AES256"
            }
        )

        url = "https://{0}.s3.amazonaws.com/{1}".format(
            bucket_name, bucket_key
        )

        self.logger.debug("%s - Template URL: '%s'", self.name, url)

        return url

    def _bucket_exists(self):
        """
        Checks if the bucket ``bucket_name`` exists.

        :returns: Boolean whether the bucket exists
        :rtype: bool
        :raises: botocore.exception.ClientError

        """
        bucket_name = self.s3_details["bucket_name"]
        self.logger.debug(
            "%s - Attempting to find template bucket '%s'",
            self.name, bucket_name
        )
        try:
            self.connection_manager.call(
                service="s3",
                command="head_bucket",
                kwargs={"Bucket": bucket_name}
            )
        except botocore.exceptions.ClientError as exp:
            if exp.response["Error"]["Message"] == "Not Found":
                self.logger.debug(
                    "%s - %s bucket not found.", self.name, bucket_name
                )
                return False
            else:
                raise
        self.logger.debug(
            "%s - Found template bucket '%s'", self.name, bucket_name
        )
        return True

    def _create_bucket(self):
        """
        Create the s3 bucket ``bucket_name``.

        :raises: botocore.exception.ClientError

        """
        bucket_name = self.s3_details["bucket_name"]

        self.logger.debug(
            "%s - Creating new bucket '%s'", self.name, bucket_name
        )

        if self.connection_manager.region == "us-east-1":
            self.connection_manager.call(
                service="s3",
                command="create_bucket",
                kwargs={"Bucket": bucket_name}
            )
        else:
            self.connection_manager.call(
                service="s3",
                command="create_bucket",
                kwargs={
                    "Bucket": bucket_name,
                    "CreateBucketConfiguration": {
                        "LocationConstraint": self.connection_manager.region
                    }
                }
            )

    def get_boto_call_parameter(self):
        """
        Returns the CloudFormation template location.

        Uploads the template to S3 and returns the object's URL, or returns
        the template itself.

        :returns: The boto call parameter for the template.
        :rtype: dict
        """
        if self.s3_details:
            url = self.upload_to_s3()
            return {"TemplateURL": url}
        else:
            return {"TemplateBody": self.body}

    def validate(self):
        """
        Validates the stack's CloudFormation template.

        Raises an error if the template is invalid.

        :returns: Information about the template.
        :rtype: dict
        :raises: botocore.exceptions.ClientError
        """
        self.logger.debug("%s - Validating template", self.name)
        response = self.connection_manager.call(
            service="cloudformation",
            command="validate_template",
            kwargs=self.get_boto_call_parameter()
        )
        self.logger.debug(
            "%s - Validate template response: %s", self.name, response
        )
        return response

    @staticmethod
    def _render_jinja_template(template_dir, filename, jinja_vars):
        """
        Renders a jinja template.

        awsscripter supports passing awsscripter_user_data to JSON and YAML
        CloudFormation templates using Jinja2 templating.

        :param template_dir: The directory containing the template.
        :type template_dir: str
        :param filename: The name of the template file.
        :type filename: str
        :param jinja_vars: Dict of variables to render into the template.
        :type jinja_vars: dict
        :returns: The body of the CloudFormation template.
        :rtype: string
        """
        logger = logging.getLogger(__name__)
        logger.debug("%s Rendering CloudFormation template", filename)
        env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(template_dir),
            undefined=jinja2.StrictUndefined
        )
        template = env.get_template(filename)
        body = template.render(**jinja_vars)
        return body
