# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=line-too-long
# pylint: disable=too-many-lines
# pylint: disable=too-many-locals

from azure.cli.core.util import sdk_no_wait


def ad_ds_create(client,
                 resource_group_name,
                 domain_service_name,
                 domain_name,
                 replica_sets,
                 tags=None,
                 domain_configuration_type=None,
                 sku=None,
                 filtered_sync=None,
                 notify_global_admins=None,
                 notify_dc_admins=None,
                 additional_recipients=None,
                 ntlm_v1=None,
                 tls_v1=None,
                 sync_ntlm_passwords=None,
                 sync_kerberos_passwords=None,
                 sync_on_prem_passwords=None,
                 settings=None,
                 resource_forest=None,
                 ldaps=None,
                 pfx_certificate=None,
                 pfx_certificate_password=None,
                 external_access=None,
                 no_wait=False):
    domain_service = {}
    domain_service['tags'] = tags
    domain_service['location'] = replica_sets[0].get('location')
    domain_service['domain_name'] = domain_name
    domain_service['replica_sets'] = replica_sets
    domain_service['domain_configuration_type'] = domain_configuration_type
    domain_service['sku'] = sku
    domain_service['filtered_sync'] = filtered_sync

    if any([notify_global_admins, notify_dc_admins, additional_recipients]):
        domain_service['notification_settings'] = {}
        domain_service['notification_settings']['notify_global_admins'] = notify_global_admins
        domain_service['notification_settings']['notify_dc_admins'] = notify_dc_admins
        domain_service['notification_settings']['additional_recipients'] = additional_recipients

    if any([ntlm_v1, tls_v1, sync_ntlm_passwords, sync_kerberos_passwords, sync_on_prem_passwords]):
        domain_service['domain_security_settings'] = {}
        domain_service['domain_security_settings']['ntlm_v1'] = ntlm_v1
        domain_service['domain_security_settings']['tls_v1'] = tls_v1
        domain_service['domain_security_settings']['sync_ntlm_passwords'] = sync_ntlm_passwords
        domain_service['domain_security_settings']['sync_kerberos_passwords'] = sync_kerberos_passwords
        domain_service['domain_security_settings']['sync_on_prem_passwords'] = sync_on_prem_passwords

    if any([settings, resource_forest]):
        domain_service['resource_forest_settings'] = {}
        domain_service['resource_forest_settings']['settings'] = settings
        domain_service['resource_forest_settings']['resource_forest'] = resource_forest

    if any([ldaps, pfx_certificate, pfx_certificate_password, external_access]):
        domain_service['ldaps_settings'] = {}
        domain_service['ldaps_settings']['ldaps'] = ldaps
        domain_service['ldaps_settings']['pfx_certificate'] = pfx_certificate
        domain_service['ldaps_settings']['pfx_certificate_password'] = pfx_certificate_password
        domain_service['ldaps_settings']['external_access'] = external_access

    return sdk_no_wait(no_wait,
                       client.begin_create_or_update,
                       resource_group_name=resource_group_name,
                       domain_service_name=domain_service_name,
                       domain_service=domain_service)


def ad_ds_update(client,
                 resource_group_name,
                 domain_service_name,
                 tags=None,
                 replica_sets=None,
                 domain_configuration_type=None,
                 sku=None,
                 filtered_sync=None,
                 notify_global_admins=None,
                 notify_dc_admins=None,
                 additional_recipients=None,
                 ntlm_v1=None,
                 tls_v1=None,
                 sync_ntlm_passwords=None,
                 sync_kerberos_passwords=None,
                 sync_on_prem_passwords=None,
                 settings=None,
                 resource_forest=None,
                 ldaps=None,
                 pfx_certificate=None,
                 pfx_certificate_password=None,
                 external_access=None,
                 no_wait=False):
    domain_service = client.get(resource_group_name=resource_group_name, domain_service_name=domain_service_name)
    new_domain_service = {}
    if tags is not None:
        new_domain_service['tags'] = tags
    if replica_sets is not None:
        new_domain_service['replica_sets'] = replica_sets
    if domain_configuration_type is not None:
        new_domain_service['domain_configuration_type'] = domain_configuration_type
    if sku is not None:
        new_domain_service['sku'] = sku
    if filtered_sync is not None:
        new_domain_service['filtered_sync'] = filtered_sync

    if any([notify_global_admins, notify_dc_admins, additional_recipients]):
        notification_settings = domain_service.notification_settings
        new_domain_service['notification_settings'] = {}
        new_domain_service['notification_settings']['notify_global_admins'] = \
            _get_domain_service_property_value(notification_settings, 'notify_global_admins', None) if notify_global_admins is None else notify_global_admins
        new_domain_service['notification_settings']['notify_dc_admins'] = \
            _get_domain_service_property_value(notification_settings, 'notify_dc_admins', None) if notify_dc_admins is None else notify_dc_admins
        new_domain_service['notification_settings']['additional_recipients'] = \
            _get_domain_service_property_value(notification_settings, 'additional_recipients', None) if additional_recipients is None else additional_recipients

    if any([ntlm_v1, tls_v1, sync_ntlm_passwords, sync_kerberos_passwords, sync_on_prem_passwords]):
        domain_security_settings = domain_service.domain_security_settings
        new_domain_service['domain_security_settings'] = {}
        new_domain_service['domain_security_settings']['ntlm_v1'] = \
            _get_domain_service_property_value(domain_security_settings, 'ntlm_v1', 'Enabled') if ntlm_v1 is None else ntlm_v1
        new_domain_service['domain_security_settings']['tls_v1'] = \
            _get_domain_service_property_value(domain_security_settings, 'tls_v1', 'Enabled') if tls_v1 is None else tls_v1
        new_domain_service['domain_security_settings']['sync_ntlm_passwords'] = \
            _get_domain_service_property_value(domain_security_settings, 'sync_ntlm_passwords', 'Enabled') if sync_ntlm_passwords is None else sync_ntlm_passwords
        new_domain_service['domain_security_settings']['sync_kerberos_passwords'] = \
            _get_domain_service_property_value(domain_security_settings, 'sync_kerberos_passwords', 'Enabled') if sync_kerberos_passwords is None else sync_kerberos_passwords
        new_domain_service['domain_security_settings']['sync_on_prem_passwords'] = \
            _get_domain_service_property_value(domain_security_settings, 'sync_on_prem_passwords', 'Enabled') if sync_on_prem_passwords is None else sync_on_prem_passwords

    if any([settings, resource_forest]):
        resource_forest_settings = domain_service.resource_forest_settings
        new_domain_service['resource_forest_settings'] = {}
        new_domain_service['resource_forest_settings']['settings'] = \
            _get_domain_service_property_value(resource_forest_settings, 'settings', None) if settings is None else settings
        new_domain_service['resource_forest_settings']['resource_forest'] = \
            _get_domain_service_property_value(resource_forest_settings, 'resource_forest', None) if resource_forest is None else resource_forest

    if any([ldaps, pfx_certificate, pfx_certificate_password, external_access]):
        ldaps_settings = domain_service.ldaps_settings
        new_domain_service['ldaps_settings'] = {}
        new_domain_service['ldaps_settings']['ldaps'] = \
            _get_domain_service_property_value(ldaps_settings, 'ldaps', 'Disabled') if ldaps is None else ldaps
        new_domain_service['ldaps_settings']['pfx_certificate'] = \
            _get_domain_service_property_value(ldaps_settings, 'pfx_certificate', None) if pfx_certificate is None else pfx_certificate
        new_domain_service['ldaps_settings']['pfx_certificate_password'] = \
            _get_domain_service_property_value(ldaps_settings, 'pfx_certificate_password', None) if pfx_certificate_password is None else pfx_certificate_password
        new_domain_service['ldaps_settings']['external_access'] = \
            _get_domain_service_property_value(ldaps_settings, 'external_access', 'Disabled') if external_access is None else external_access

    return sdk_no_wait(no_wait,
                       client.begin_update,
                       resource_group_name=resource_group_name,
                       domain_service_name=domain_service_name,
                       domain_service=new_domain_service)


def _get_domain_service_property_value(old_property, sub_property_name, default_value):
    if old_property is not None:
        return getattr(old_property, sub_property_name, default_value)
    return default_value
