import functools
import logging
import time


def __virtual__():
    return 'designatev2' if 'designatev2.zone_list' in __salt__ else False  # noqa


log = logging.getLogger(__name__)


def _designatev2_call(fname, *args, **kwargs):
    return __salt__['designatev2.{}'.format(fname)](*args, **kwargs)  # noqa


def _resource_present(resource, name, cloud_name, waiter=None, **kwargs):
    try:
        method_name = '{}_get_details'.format(resource)
        exact_resource = _designatev2_call(
            method_name, name, cloud_name=cloud_name
        )
    except Exception as e:
        if 'ResourceNotFound' in repr(e):
            try:
                method_name = '{}_create'.format(resource)
                resp = _designatev2_call(
                    method_name, name=name, cloud_name=cloud_name, **kwargs
                )
            except Exception as e:
                log.exception('Designate {0} create failed with {1}'.
                              format(resource, e))
                return _failed('create', name, resource)
            if waiter:
                if not waiter(name, cloud_name):
                    return _failed('create', name, resource)
                resp['status'] = 'ACTIVE'
            return _succeeded('create', name, resource, resp)
        elif 'MultipleResourcesFound' in repr(e):
            return _failed('find', name, resource)
        else:
            raise

    to_update = {}
    for key in kwargs:
        if key not in exact_resource or kwargs[key] != exact_resource[key]:
            to_update[key] = kwargs[key]
    if to_update:
        try:
            method_name = '{}_update'.format(resource)
            resp = _designatev2_call(
                method_name, name, cloud_name=cloud_name, **to_update
            )
        except Exception as e:
            log.exception('Designate {0} update failed with {1}'.format(resource, e))
            return _failed('update', name, resource)
        return _succeeded('update', name, resource, resp)
    return _succeeded('no_changes', name, resource)


def _resource_absent(resource, name, cloud_name, waiter=None):
    try:
        method_name = '{}_get_details'.format(resource)
        _designatev2_call(
            method_name, name, cloud_name=cloud_name
        )
    except Exception as e:
        if 'ResourceNotFound' in repr(e):
            return _succeeded('absent', name, resource)
        if 'MultipleResourcesFound' in repr(e):
            return _failed('find', name, resource)
    try:
        method_name = '{}_delete'.format(resource)
        _designatev2_call(
            method_name, name, cloud_name=cloud_name
        )
    except Exception as e:
        log.error('Designate delete {0} failed with {1}'.format(resource, e))
        return _failed('delete', name, resource)
    if waiter:
        if not waiter(name, cloud_name):
            return _failed('delete', name, resource)
    return _succeeded('delete', name, resource)


def _poll_for_zone(zone_name, cloud_name, checker, retry=5, timeout=2):
    while retry:
        zones = _designatev2_call('zone_list', name=zone_name,
                                  cloud_name=cloud_name)['zones']
        if checker(zones):
            return True
        time.sleep(timeout)
        retry -= 1
    return False


def zone_present(name, cloud_name, email, **kwargs):
    kwargs.update({'email': email})
    return _resource_present(
        'zone', name, cloud_name,
        functools.partial(
            _poll_for_zone,
            checker=lambda zones: len(zones) and
                                  zones[0]['status'] == 'ACTIVE',
            **kwargs.pop('waiter_config', {})),
        **kwargs)


def zone_absent(name, cloud_name, **kwargs):
    return _resource_absent(
        'zone', name, cloud_name,
        waiter=functools.partial(
            _poll_for_zone, checker=lambda zones: not len(zones),
            **kwargs.pop('waiter_config', {})))


def _succeeded(op, name, resource, changes=None):
    msg_map = {
        'create': '{0} {1} created',
        'delete': '{0} {1} removed',
        'update': '{0} {1} updated',
        'no_changes': '{0} {1} is in desired state',
        'absent': '{0} {1} not present',
        'resources_moved': '{1} resources were moved from {0}',
    }
    changes_dict = {
        'name': name,
        'result': True,
        'comment': msg_map[op].format(resource, name),
        'changes': changes or {},
    }
    return changes_dict


def _failed(op, name, resource):
    msg_map = {
        'create': '{0} {1} failed to create',
        'delete': '{0} {1} failed to delete',
        'update': '{0} {1} failed to update',
        'find': '{0} {1} found multiple {0}',
        'resources_moved': 'failed to move {1} from {0}',
    }
    changes_dict = {
        'name': name,
        'result': False,
        'comment': msg_map[op].format(resource, name),
        'changes': {},
    }
    return changes_dict
