# --------------------------------------------------------------------------
# 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.
# --------------------------------------------------------------------------

import os
import time
from azure.cli.testsdk import ScenarioTest
from azure.cli.testsdk import ResourceGroupPreparer
from .preparers import VirtualNetworkPreparer
from .example_steps import step_ds_create
from .example_steps import step_ds_list
from .example_steps import step_ds_list2
from .example_steps import step_ds_update
from .example_steps import step_ds_show
from .example_steps import step_ds_delete
from .example_steps import step_ds_update_security_settings
from .example_steps import step_ds_update_notification_settings
from .. import (
    try_manual,
    raise_if,
    calc_coverage
)


TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..'))


# Env setup_scenario
@try_manual
def setup_scenario(test, rg_2, rg):
    test.kwargs.update({
        'nsg': 'aadds-nsg-test',
        'vn': 'vnet1',
        'vn_2': 'vnet2'
    })
    test.cmd('az network nsg create --name {nsg} --resource-group {rg}')
    test.cmd('az network nsg rule create -n AllowPSRemoting -g {rg} --nsg-name {nsg} --priority 301 '
             '--access Allow --protocol Tcp --direction Inbound --destination-port-ranges 5986 '
             '--source-address-prefixes AzureActiveDirectoryDomainServices')
    test.cmd('az network nsg rule create -n AllowRD -g {rg} --nsg-name {nsg} --priority 201 '
             '--access Allow --protocol Tcp --direction Inbound --destination-port-ranges 3389 '
             '--source-address-prefixes CorpNetSaw')
    test.cmd('az network vnet create -g {rg} -n {vn} --nsg {nsg} --subnet-name default '
             '--address-prefixes 10.0.0.0/16')
    test.cmd('az network vnet create -g {rg} -n {vn_2} --nsg {nsg} --subnet-name default '
             '--address-prefixes 10.1.0.0/16')


# Env cleanup_scenario
@try_manual
def cleanup_scenario(test, rg_2, rg):
    test.cmd('az network vnet delete -g {rg} -n {vn}')
    test.cmd('az network vnet delete -g {rg} -n {vn_2}')


# Testcase: Scenario
@try_manual
def call_scenario(test, rg_2, rg):
    setup_scenario(test, rg_2, rg)
    step_ds_create(test, rg_2, rg, checks=[
        test.check("domainName", "{myDomainService}", case_sensitive=False),
        test.check("replicaSets | length(@)", 1),
        test.check("domainSecuritySettings.ntlmV1", "Enabled", case_sensitive=False),
        test.check("domainSecuritySettings.syncNtlmPasswords", "Enabled", case_sensitive=False),
        test.check("domainSecuritySettings.tlsV1", "Enabled", case_sensitive=False),
        test.check("ldapsSettings.externalAccess", "Disabled", case_sensitive=False),
        test.check("ldapsSettings.ldaps", "Disabled", case_sensitive=False),
        test.check("notificationSettings.notifyDcAdmins", "Enabled", case_sensitive=False),
        test.check("notificationSettings.notifyGlobalAdmins", "Enabled", case_sensitive=False),
        test.check("name", "{myDomainService}", case_sensitive=False),
    ])
    step_ds_list(test, rg_2, rg, checks=[
        test.check('[?name==`{myDomainService}`] | length(@)', 1),
    ])
    step_ds_list2(test, rg_2, rg, checks=[
        test.check('[?name==`{myDomainService}`] | length(@)', 1),
    ])
    time.sleep(2400)
    # step_ds_update(test, rg_2, rg, checks=[
    #     test.check("domainName", "{myDomainService}", case_sensitive=False),
    #     test.check("replicaSets | length(@)", 2),
    #     test.check("domainSecuritySettings.ntlmV1", "Disabled", case_sensitive=False),
    #     test.check("domainSecuritySettings.syncNtlmPasswords", "Enabled", case_sensitive=False),
    #     test.check("domainSecuritySettings.tlsV1", "Enabled", case_sensitive=False),
    #     test.check("notificationSettings.notifyDcAdmins", "Enabled", case_sensitive=False),
    #     test.check("notificationSettings.notifyGlobalAdmins", "Enabled", case_sensitive=False),
    #     test.check("notificationSettings.additionalRecipients | length(@)", 2),
    #     test.check("name", "{myDomainService}", case_sensitive=False),
    # ])
    # time.sleep(600)
    step_ds_update_security_settings(test, rg_2, rg, checks=[
        test.check("domainSecuritySettings.ntlmV1", "Disabled", case_sensitive=False),
        test.check("domainSecuritySettings.syncNtlmPasswords", "Enabled", case_sensitive=False),
        test.check("name", "{myDomainService}", case_sensitive=False),
    ])
    time.sleep(600)
    step_ds_update_notification_settings(test, rg_2, rg, checks=[
        test.check("notificationSettings.additionalRecipients | length(@)", 2),
        test.check("name", "{myDomainService}", case_sensitive=False),
    ])
    step_ds_show(test, rg_2, rg, checks=[
        test.check("domainName", "{myDomainService}", case_sensitive=False),
        test.check("replicaSets | length(@)", 1),
        test.check("domainSecuritySettings.ntlmV1", "Disabled", case_sensitive=False),
        test.check("domainSecuritySettings.syncNtlmPasswords", "Enabled", case_sensitive=False),
        test.check("notificationSettings.notifyDcAdmins", "Enabled", case_sensitive=False),
        test.check("notificationSettings.notifyGlobalAdmins", "Enabled", case_sensitive=False),
        test.check("notificationSettings.additionalRecipients | length(@)", 2),
        test.check("name", "{myDomainService}", case_sensitive=False),
    ])
    step_ds_delete(test, rg_2, rg, checks=[])
    time.sleep(600)
    step_ds_list(test, rg_2, rg, checks=[
        test.check("[?name==`{myDomainService}`] | length(@)", 0),
    ])
    cleanup_scenario(test, rg_2, rg)


# Test class for Scenario
@try_manual
class AdScenarioTest(ScenarioTest):

    def __init__(self, *args, **kwargs):
        super(AdScenarioTest, self).__init__(*args, **kwargs)
        self.kwargs.update({
            'subscription_id': self.get_subscription_id()
        })

        self.kwargs.update({
            'myDomainService': 'TestDomainService.com',
        })

    @ResourceGroupPreparer(name_prefix='clitestad_TestNetworkResourceGroup'[:7], key='rg_2', parameter_name='rg_2')
    @ResourceGroupPreparer(name_prefix='clitestad_TestResourceGroup'[:7], key='rg', parameter_name='rg')
    def test_ad_Scenario(self, rg_2, rg):
        call_scenario(self, rg_2, rg)
        calc_coverage(__file__)
        raise_if()
