from . import service
from heaserver.service.testcase.microservicetestcase import get_test_case_cls_default
from heaserver.service.testcase.expectedvalues import Action, Link
from heaserver.service.testcase.dockermongo import MockDockerMongoManager

from typing import Dict, List, Any
from heaserver.service.testcase import TEST_USER
from heaobject.volume import DEFAULT_FILE_SYSTEM
from heaobject.user import ALL_USERS
from heaobject.group import NONE_GROUP
from heaobject.root import Permission

fixtures: Dict[str, List[Dict[str, Any]]] = {
    service.MONGODB_COMPONENT_COLLECTION: [{
        'id': '666f6f2d6261722d71757578',
        'instance_id': 'heaobject.registry.Component^666f6f2d6261722d71757578',
        'created': '2022-05-17T00:00:00+00:00',
        'derived_by': None,
        'derived_from': ['foo', 'bar'],
        'description': 'Description of Reximus',
        'display_name': 'Reximus',
        'invites': [],
        'modified': '2022-05-17T00:00:00+00:00',
        'name': 'reximus',
        'owner': TEST_USER,
        'shares': [{
            'type': 'heaobject.root.ShareImpl',
            'invite': None,
            'user': ALL_USERS,
            'permissions': [Permission.COOWNER.name],
            'type_display_name': 'Share',
            'group': NONE_GROUP,
            'basis': 'USER'
        }],
        'user_shares': [{
            'type': 'heaobject.root.ShareImpl',
            'invite': None,
            'user': ALL_USERS,
            'permissions': [Permission.COOWNER.name],
            'type_display_name': 'Share',
            'group': NONE_GROUP,
            'basis': 'USER'
        }],
        'group_shares': [],
        'source': None,
        'source_detail': None,
        'type': 'heaobject.registry.Component',
        'base_url': 'http://localhost/foo',
        'external_base_url': None,
        'type_display_name': 'Registry Component',
        'resources': [{
                'type': 'heaobject.registry.Resource',
                'resource_type_name': 'heaobject.folder.Folder',
                'base_path': 'folders',
                'file_system_name': DEFAULT_FILE_SYSTEM,
                'file_system_type': 'heaobject.volume.MongoDBFileSystem',
                'resource_collection_type_display_name': 'heaobject.folder.Folder',
                'collection_accessor_users': [],
                'collection_accessor_groups': [],
                'creator_users': [],
                'creator_groups': [],
                'default_shares': [],
                'type_display_name': 'Resource',
                'manages_creators': False,
                'collection_mime_type': 'application/x.collection',
                'display_in_system_menu': False,
                'display_in_user_menu': False
            }],
        'super_admin_default_permissions': [Permission.VIEWER.name, Permission.EDITOR.name],
        'dynamic_permission_supported': False
    },
    {
        'id': '0123456789ab0123456789ab',
        'instance_id': 'heaobject.registry.Component^0123456789ab0123456789ab',
        'created': '2022-05-17T00:00:00+00:00',
        'derived_by': None,
        'derived_from': ['oof', 'rab'],
        'description': 'Description of Luximus',
        'display_name': 'Luximus',
        'invites': [],
        'modified': '2022-05-17T00:00:00+00:00',
        'name': 'luximus',
        'owner': TEST_USER,
        'source': None,
        'source_detail': None,
        'type': 'heaobject.registry.Component',
        'base_url': 'http://localhost/foo',
        'external_base_url': None,
        'type_display_name': 'Registry Component',
        'resources': [{
            'type': 'heaobject.registry.Resource',
            'resource_type_name': 'heaobject.folder.Folder',
            'base_path': 'folders',
            'file_system_name': DEFAULT_FILE_SYSTEM,
            'file_system_type': 'heaobject.volume.MongoDBFileSystem',
            'resource_collection_type_display_name': 'heaobject.folder.Folder',
            'collection_accessor_users': [],
            'collection_accessor_groups': [],
            'creator_users': [],
            'creator_groups': [],
            'default_shares': [],
            'type_display_name': 'Resource',
            'manages_creators': False,
            'collection_mime_type': 'application/x.collection',
            'display_in_system_menu': False,
            'display_in_user_menu': False
        }],
        'shares': [{
            'type': 'heaobject.root.ShareImpl',
            'invite': None,
            'user': ALL_USERS,
            'permissions': [Permission.COOWNER.name],
            'type_display_name': 'Share',
            'group': NONE_GROUP,
            'basis': 'USER'
        }],
        'user_shares': [{
            'type': 'heaobject.root.ShareImpl',
            'invite': None,
            'user': ALL_USERS,
            'permissions': [Permission.COOWNER.name],
            'type_display_name': 'Share',
            'group': NONE_GROUP,
            'basis': 'USER'
        }],
        'group_shares': [],
        'super_admin_default_permissions': [Permission.VIEWER.name, Permission.EDITOR.name],
        'dynamic_permission_supported': False
    }]}

content = {
    service.MONGODB_COMPONENT_COLLECTION: {
        '666f6f2d6261722d71757578': b'The quick brown fox jumps over the lazy dog'
    }
}

ComponentTestCase = get_test_case_cls_default(coll=service.MONGODB_COMPONENT_COLLECTION, fixtures=fixtures,
                                              duplicate_action_name='component-duplicate-form',
                                              db_manager_cls=MockDockerMongoManager, wstl_package=service.__package__,
                                              content=content, content_type='text/plain', put_content_status=204,
                                              href='http://localhost:8080/components/',
                                              get_actions=[Action(name='component-get-properties',
                                                                  rel=['hea-properties']),
                                                           Action(name='component-get-open-choices',
                                                                  url='http://localhost:8080/components/{id}/opener',
                                                                  rel=['hea-opener-choices']),
                                                           Action(name='component-duplicate',
                                                                  url='http://localhost:8080/components/{id}/duplicator',
                                                                  rel=['hea-duplicator'])],
                                              get_all_actions=[Action(name='component-get-properties',
                                                                      rel=['hea-properties']),
                                                               Action(name='component-get-open-choices',
                                                                      url='http://localhost:8080/components/{id}/opener',
                                                                      rel=['hea-opener-choices']),
                                                               Action(name='component-duplicate',
                                                                      url='http://localhost:8080/components/{id}/duplicator',
                                                                      rel=['hea-duplicator'])],
                                              expected_opener=Link(
                                                  url=f'http://localhost:8080/components/{fixtures[service.MONGODB_COMPONENT_COLLECTION][0]["id"]}/content',
                                                  rel=['hea-default', 'hea-opener', 'text/plain']), sub=TEST_USER)
