# -*- coding: utf-8 -*-
'''
Microsoft certificate management via the Pki PowerShell module.

:platform:      Windows

.. versionadded:: Carbon
'''

# Import python libs
from __future__ import absolute_import

_DEFAULT_CONTEXT = 'LocalMachine'
_DEFAULT_FORMAT = 'cer'
_DEFAULT_STORE = 'My'


def __virtual__():
    '''
    Load only on minions that have the win_pki module.
    '''
    if 'win_pki.get_stores' in __salt__:
        return True
    return False


def import_cert(name, cert_format=_DEFAULT_FORMAT, context=_DEFAULT_CONTEXT, store=_DEFAULT_STORE,
                exportable=True, password='', saltenv='base'):
    '''
    Import the certificate file into the given certificate store.

    :param str name: The path of the certificate file to import.
    :param str cert_format: The certificate format. Specify 'cer' for X.509, or 'pfx' for PKCS #12.
    :param str context: The name of the certificate store location context.
    :param str store: The name of the certificate store.
    :param bool exportable: Mark the certificate as exportable. Only applicable to pfx format.
    :param str password: The password of the certificate. Only applicable to pfx format.
    :param str saltenv: The environment the file resides in.
    '''
    ret = {'name': name,
           'changes': dict(),
           'comment': str(),
           'result': None}

    store_path = r'Cert:\{0}\{1}'.format(context, store)

    cached_source_path = __salt__['cp.cache_file'](name, saltenv)
    current_certs = __salt__['win_pki.get_certs'](context=context, store=store)
    cert_props = __salt__['win_pki.get_cert_file'](name=cached_source_path)

    if cert_props['thumbprint'] in current_certs:
        ret['comment'] = ("Certificate '{0}' already contained in store:"
                          ' {1}').format(cert_props['thumbprint'], store_path)
        ret['result'] = True
    elif __opts__['test']:
        ret['comment'] = ("Certificate '{0}' will be imported into store:"
                          ' {1}').format(cert_props['thumbprint'], store_path)
        ret['changes'] = {'old': None,
                          'new': cert_props['thumbprint']}
    else:
        ret['changes'] = {'old': None,
                          'new': cert_props['thumbprint']}
        ret['result'] = __salt__['win_pki.import_cert'](name=name, cert_format=cert_format,
                                                        context=context, store=store,
                                                        exportable=exportable, password=password,
                                                        saltenv=saltenv)
        if ret['result']:
            ret['comment'] = ("Certificate '{0}' imported into store:"
                              ' {1}').format(cert_props['thumbprint'], store_path)
        else:
            ret['comment'] = ("Certificate '{0}' unable to be imported into store:"
                              ' {1}').format(cert_props['thumbprint'], store_path)
    return ret


def remove_cert(name, thumbprint, context=_DEFAULT_CONTEXT, store=_DEFAULT_STORE):
    '''
    Remove the certificate from the given certificate store.

    :param str thumbprint: The thumbprint value of the target certificate.
    :param str context: The name of the certificate store location context.
    :param str store: The name of the certificate store.
    '''
    ret = {'name': name,
           'changes': dict(),
           'comment': str(),
           'result': None}

    store_path = r'Cert:\{0}\{1}'.format(context, store)
    current_certs = __salt__['win_pki.get_certs'](context=context, store=store)

    if thumbprint not in current_certs:
        ret['comment'] = "Certificate '{0}' already removed from store: {1}".format(thumbprint,
                                                                                    store_path)
        ret['result'] = True
    elif __opts__['test']:
        ret['comment'] = "Certificate '{0}' will be removed from store: {1}".format(thumbprint,
                                                                                    store_path)
        ret['changes'] = {'old': thumbprint,
                          'new': None}
    else:
        ret['changes'] = {'old': thumbprint,
                          'new': None}
        ret['result'] = __salt__['win_pki.remove_cert'](thumbprint=thumbprint, context=context,
                                                        store=store)
        if ret['result']:
            ret['comment'] = "Certificate '{0}' removed from store: {1}".format(thumbprint, store_path)
        else:
            ret['comment'] = "Certificate '{0}' unable to be removed from store: {1}".format(thumbprint,
                                                                                             store_path)
    return ret

@@ -1233,16 +1233,641 @@
 ides in.
+%0A%0A    Example of usage with only the required arguments:%0A%0A    .. code-block:: yaml%0A%0A        site0-cert-imported:%0A            win_pki.import_cert:%0A                - name: salt://win/webserver/certs/site0.cer%0A%0A    Example of usage specifying all available arguments:%0A%0A    .. code-block:: yaml%0A%0A        site0-cert-imported:%0A            win_pki.import_cert:%0A                - name: salt://win/webserver/certs/site0.pfx%0A                - cert_format: pfx%0A                - context: LocalMachine%0A                - store: My%0A                - exportable: True%0A                - password: TestPassword%0A                - saltenv: base
 %0A    '''
@@ -3962,24 +3962,524 @@
 icate store.
+%0A%0A    Example of usage with only the required arguments:%0A%0A    .. code-block:: yaml%0A%0A        site0-cert-removed:%0A            win_pki.remove_cert:%0A                - thumbprint: 9988776655443322111000AAABBBCCCDDDEEEFFF%0A%0A    Example of usage specifying all available arguments:%0A%0A    .. code-block:: yaml%0A%0A        site0-cert-removed:%0A            win_pki.remove_cert:%0A                - thumbprint: 9988776655443322111000AAABBBCCCDDDEEEFFF%0A                - context: LocalMachine%0A                - store: My
 %0A    '''%0A   

Add example yaml to win_pki state