// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.entitySystem;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.terasology.assets.AssetFactory;
import org.terasology.assets.ResourceUrn;
import org.terasology.assets.management.AssetManager;
import org.terasology.assets.module.ModuleAwareAssetTypeManager;
import org.terasology.context.internal.ContextImpl;
import org.terasology.engine.module.ModuleManager;
import org.terasology.entitySystem.metadata.ComponentLibrary;
import org.terasology.entitySystem.metadata.EntitySystemLibrary;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.prefab.PrefabData;
import org.terasology.entitySystem.prefab.internal.PojoPrefab;
import org.terasology.entitySystem.prefab.internal.PojoPrefabManager;
import org.terasology.entitySystem.stubs.StringComponent;
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.persistence.typeHandling.TypeHandlerLibraryImpl;
import org.terasology.persistence.typeHandling.mathTypes.legacy.LegacyQuat4fTypeHandler;
import org.terasology.persistence.typeHandling.mathTypes.legacy.LegacyVector3fTypeHandler;
import org.terasology.registry.CoreRegistry;
import org.terasology.testUtil.ModuleManagerFactory;
import org.terasology.utilities.Assets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
 */
public class PojoPrefabManagerTest {

    public static final String PREFAB_NAME = "unittest:myprefab";
    private EntitySystemLibrary entitySystemLibrary;
    private ComponentLibrary componentLibrary;
    private PojoPrefabManager prefabManager;

    @BeforeEach
    public void setup() throws Exception {
        ContextImpl context = new ContextImpl();
        CoreRegistry.setContext(context);
        ModuleManager moduleManager = ModuleManagerFactory.create();

        Reflections reflections = new Reflections(getClass().getClassLoader());
        TypeHandlerLibrary lib = new TypeHandlerLibraryImpl(reflections);

        lib.addTypeHandler(Vector3f.class, new LegacyVector3fTypeHandler());
        lib.addTypeHandler(Quat4f.class, new LegacyQuat4fTypeHandler());

        entitySystemLibrary = new EntitySystemLibrary(context, lib);
        componentLibrary = entitySystemLibrary.getComponentLibrary();

        ModuleAwareAssetTypeManager assetTypeManager = new ModuleAwareAssetTypeManager();
        assetTypeManager.registerCoreAssetType(Prefab.class,
                (AssetFactory<Prefab, PrefabData>) PojoPrefab::new, "prefabs");

        assetTypeManager.switchEnvironment(moduleManager.getEnvironment());
        context.put(AssetManager.class, assetTypeManager.getAssetManager());

        prefabManager = new PojoPrefabManager(context);
    }

    @Test
    public void testRetrieveNonExistentPrefab() {
        assertNull(prefabManager.getPrefab(PREFAB_NAME));
    }

    @Test
    public void testRetrievePrefab() {
        PrefabData data = new PrefabData();
        data.addComponent(new StringComponent("Test"));
        Prefab prefab = Assets.generateAsset(new ResourceUrn(PREFAB_NAME), data, Prefab.class);
        Prefab ref = prefabManager.getPrefab(PREFAB_NAME);
        assertNotNull(ref);
        assertEquals(PREFAB_NAME, ref.getName());
    }


}

@@ -194,87 +194,8 @@
 st;%0A
-import org.reflections.Reflections;%0Aimport org.terasology.assets.AssetFactory;%0A
 impo
@@ -458,135 +458,8 @@
 er;%0A
-import org.terasology.entitySystem.metadata.ComponentLibrary;%0Aimport org.terasology.entitySystem.metadata.EntitySystemLibrary;%0A
 impo
@@ -753,408 +753,8 @@
 nt;%0A
-import org.terasology.math.geom.Quat4f;%0Aimport org.terasology.math.geom.Vector3f;%0Aimport org.terasology.persistence.typeHandling.TypeHandlerLibrary;%0Aimport org.terasology.persistence.typeHandling.TypeHandlerLibraryImpl;%0Aimport org.terasology.persistence.typeHandling.mathTypes.legacy.LegacyQuat4fTypeHandler;%0Aimport org.terasology.persistence.typeHandling.mathTypes.legacy.LegacyVector3fTypeHandler;%0A
 impo
@@ -1187,108 +1187,8 @@
 b%22;%0A
-    private EntitySystemLibrary entitySystemLibrary;%0A    private ComponentLibrary componentLibrary;%0A
     
@@ -1453,454 +1453,8 @@
 );%0A%0A
-        Reflections reflections = new Reflections(getClass().getClassLoader());%0A        TypeHandlerLibrary lib = new TypeHandlerLibraryImpl(reflections);%0A%0A        lib.addTypeHandler(Vector3f.class, new LegacyVector3fTypeHandler());%0A        lib.addTypeHandler(Quat4f.class, new LegacyQuat4fTypeHandler());%0A%0A        entitySystemLibrary = new EntitySystemLibrary(context, lib);%0A        componentLibrary = entitySystemLibrary.getComponentLibrary();%0A%0A
     
@@ -1603,59 +1603,8 @@
 ass,
-%0A                (AssetFactory%3CPrefab, PrefabData%3E)
  Poj
@@ -2132,24 +2132,8 @@
     
- Prefab prefab =
  Ass

migrate and simplify PojoPrefabManagerTest (#4458)