# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import logging
import time

LOG = logging.getLogger(__name__)


def __virtual__():
    return 'heatv1'


def _heat_call(fname, *args, **kwargs):
    return __salt__['heatv1.{}'.format(fname)](*args, **kwargs)


def _poll_for_complete(stack_name, cloud_name=None, action=None,
                       poll_period=5, timeout=60):
    if action:
        stop_status = ('{0}_FAILED'.format(action), '{0}_COMPLETE'.format(action))
        stop_check = lambda a: a in stop_status
    else:
        stop_check = lambda a: a.endswith('_COMPLETE') or a.endswith('_FAILED')
    timeout_sec = timeout * 60
    msg_template = '\n Stack %(name)s %(status)s \n'
    while True:
        stack = _heat_call('stack_show',
                           name=stack_name,
                           cloud_name=cloud_name)
        if not stack["result"]:
            if action == "DELETE" and stack['status_code'] == 404:
                stack_status = 'DELETE COMPLETE'
                msg = msg_template % dict(name=stack_name, status=stack_status)
                return 'DELETE_COMPLETE', msg
            raise Exception("request for stack failed")

        stack = stack["body"]["stack"]
        stack_status = stack["stack_status"]
        msg = msg_template % dict(
            name=stack_name, status=stack_status)
        if stop_check(stack_status):
            return stack_status, msg

        time.sleep(poll_period)
        timeout_sec -= poll_period
        if timeout_sec <= 0:
            stack_status = '{0}_FAILED'.format(action)
            msg = 'Timeout expired'
            return stack_status, msg


def stack_present(name, cloud_name, template=None,
                  environment=None, params=None, poll=5, rollback=False,
                  timeout=60, profile=None, **connection_args):
    LOG.debug('Deployed with(' +
              '{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} {9})'
              .format(name, cloud_name, template, environment, params,
                      poll, rollback, timeout, profile, connection_args))
    ret = {'name': None,
           'comment': '',
           'changes': {},
           'result': True}

    if not name:
        ret['result'] = False
        ret['comment'] = 'Name is not valid'
        return ret

    ret['name'] = name,

    existing_stack = _heat_call('stack_show', name=name,
                                cloud_name=cloud_name)

    if existing_stack['result']:
        _heat_call('stack_update', name=name,
                   template=template,
                   cloud_name=cloud_name,
                   environment=environment,
                   parameters=params,
                   disable_rollback=not rollback,
                   timeout=timeout)
        ret['changes']['comment'] = 'Updated stack'
        status, res = _poll_for_complete(stack_name=name,
                                         cloud_name=cloud_name,
                                         action="UPDATE", timeout=timeout)
        ret["result"] = status == "UPDATE_COMPLETE"
        ret['comment'] = res
    else:
        _heat_call('stack_create',
                   name=name,
                   template=template,
                   cloud_name=cloud_name,
                   environment=environment,
                   parameters=params,
                   disable_rollback=not rollback,
                   timeout=timeout)
        status, res = _poll_for_complete(stack_name=name,
                                         cloud_name=cloud_name,
                                         action="CREATE", timeout=timeout)
        ret["result"] = status == "CREATE_COMPLETE"
        ret['comment'] = res
    ret['changes']['stack_name'] = name
    return ret


def stack_absent(name, cloud_name, poll=5, timeout=60):
    LOG.debug('Absent with(' +
              '{0}, {1}, {2})'.format(name, poll, cloud_name))
    ret = {'name': None,
           'comment': '',
           'changes': {},
           'result': True}
    if not name:
        ret['result'] = False
        ret['comment'] = 'Name is not valid'
        return ret

    ret['name'] = name,

    existing_stack = _heat_call('stack_show',
                                name=name, cloud_name=cloud_name)

    if not existing_stack['result']:
        ret['result'] = True
        ret['comment'] = 'Stack does not exist'
        return ret

    _heat_call('stack_delete', name=name, cloud_name=cloud_name)
    status, comment = _poll_for_complete(stack_name=name,
                                         cloud_name=cloud_name,
                                         action="DELETE", timeout=timeout)
    ret['result'] = status == "DELETE_COMPLETE"
    ret['comment'] = comment
    ret['changes']['stack_name'] = name
    return ret
