import six
from c7n_azure.actions.tagging import Tag, AutoTagUser, RemoveTag, TagTrim, TagDelayedAction
from c7n_azure.actions.delete import DeleteAction
from c7n_azure.filters import (MetricFilter, TagActionFilter,
                               DiagnosticSettingsFilter, PolicyCompliantFilter)
from c7n_azure.provider import resources
from c7n_azure.query import QueryResourceManager, QueryMeta, ChildResourceManager, TypeInfo, \
    ChildTypeInfo, TypeMeta
from c7n_azure.utils import ResourceIdParser

from c7n.utils import local_session


@six.add_metaclass(TypeMeta)
class ArmTypeInfo(TypeInfo):
    # api client construction information for ARM resources
    id = 'id'
    name = 'name'
    diagnostic_settings_enabled = True
    default_report_fields = (
        'name',
        'location',
        'resourceGroup'
    )


@resources.register('armresource')
@six.add_metaclass(QueryMeta)
class ArmResourceManager(QueryResourceManager):

    class resource_type(ArmTypeInfo):
        service = 'azure.mgmt.resource'
        client = 'ResourceManagementClient'
        enum_spec = ('resources', 'list', None)

    def augment(self, resources):
        for resource in resources:
            if 'id' in resource:
                resource['resourceGroup'] = ResourceIdParser.get_resource_group(resource['id'])
        return resources

    def get_resources(self, resource_ids):
        resource_client = self.get_client('azure.mgmt.resource.ResourceManagementClient')
        session = local_session(self.session_factory)
        data = [
            resource_client.resources.get_by_id(rid, session.resource_api_version(rid))
            for rid in resource_ids
        ]
        return [r.serialize(True) for r in data]

    @staticmethod
    def register_arm_specific(registry, _):
        for resource in registry.keys():
            klass = registry.get(resource)
            if issubclass(klass, ArmResourceManager):
                klass.action_registry.register('tag', Tag)
                klass.action_registry.register('untag', RemoveTag)
                klass.action_registry.register('auto-tag-user', AutoTagUser)
                klass.action_registry.register('tag-trim', TagTrim)
                klass.filter_registry.register('metric', MetricFilter)
                klass.filter_registry.register('marked-for-op', TagActionFilter)
                klass.action_registry.register('mark-for-op', TagDelayedAction)
                klass.filter_registry.register('policy-compliant', PolicyCompliantFilter)

                if resource != 'resourcegroup':
                    klass.action_registry.register('delete', DeleteAction)

                if hasattr(klass.resource_type, 'diagnostic_settings_enabled') \
                        and klass.resource_type.diagnostic_settings_enabled:
                    klass.filter_registry.register('diagnostic-settings', DiagnosticSettingsFilter)


@six.add_metaclass(QueryMeta)
class ChildArmResourceManager(ChildResourceManager, ArmResourceManager):

    class resource_type(ChildTypeInfo, ArmTypeInfo):
        pass


resources.subscribe(resources.EVENT_FINAL, ArmResourceManager.register_arm_specific)
