# --------------------------------------------------------------------------------------------
# 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 aaz-dev-tools
# --------------------------------------------------------------------------------------------

import sys
import datetime
import time
from azure.cli.testsdk import *


class ChangeAnalysisScenario(LiveScenarioTest):
    @ResourceGroupPreparer(name_prefix="cli_test_change_analysis_")
    def test_change_analysis_list(self, resource_group):
        print(resource_group)
        start_time = datetime.datetime.utcnow()
        str_start_time = start_time.strftime('%Y-%m-%dT%H:%M:%S')

        self.kwargs.update({
            'vnet1': 'vnet1',
            'vnet2': 'vnet2',
            'subnet1': 'subnet1',
            'location': 'centralus',
            'rg': resource_group,
            'start_time': str_start_time,
        })
        print(str_start_time)

        # prepare for some resoruce changes
        self.cmd('network vnet create -g {rg} -n {vnet1} --subnet-name {subnet1} -l {location}')
        self.cmd('network vnet create -g {rg} -n {vnet2} --subnet-name {subnet1} -l {location}')
        
        # wait for up to 5 minutes to ensure changes can get to ARG
        changes = {}
        for x in range(10):
            time.sleep(30)
            end_time = datetime.datetime.utcnow()
            str_end_time = end_time.strftime('%Y-%m-%dT%H:%M:%S')
            print(str_end_time)
            self.kwargs.update({
                'end_time': str_end_time,
            })
            # list change for subscription
            changes = self.cmd('change-analysis list --start-time {start_time} --end-time {end_time}').get_output_in_json()
            print(changes)
            if len(changes) > 0:
                break

        assert len(changes) > 0
        self.assertIsNotNone(changes[0]['id'])
        property_changes = changes[0]['properties']['propertyChanges']
        resource_id = changes[0]['properties']['resourceId']
        assert len(property_changes) > 0

        # list changes for given resoruce group
        changes = self.cmd('change-analysis list --start-time {start_time} --end-time {end_time} -g {rg}').get_output_in_json()
        print(changes)
        assert len(changes) > 0
        for change in changes:
            self.assertEqual(change['resourceGroup'], resource_group)

        # list changes for not existed group, should not get any change
        changes = self.cmd('change-analysis list --start-time {start_time} --end-time {end_time} -g foo').get_output_in_json()
        assert len(changes) == 0

        # list changes by resource id
        changes = self.cmd('change-analysis list-by-resource --start-time {start_time} --end-time {end_time} --r ' + resource_id).get_output_in_json()
        assert len(changes) > 0
        for change in changes:
            self.assertEqual(change['properties']['resourceId'], resource_id)
