import omni.kit.app
import omni.kit.test

import os
from pathlib import Path


class TestExtAPI(omni.kit.test.AsyncTestCase):
    async def test_ext_helpers(self):
        manager = omni.kit.app.get_app().get_extension_manager()

        # Enable ext id
        my_id = manager.get_enabled_extension_id("omni.kit.core.tests")
        self.assertEqual(my_id.split("-")[0], "omni.kit.core.tests")

        # Ext name
        self.assertEqual(omni.ext.get_extension_name(my_id), "omni.kit.core.tests")
        self.assertEqual(omni.ext.get_extension_name("omni.kit.some_ext_id"), "omni.kit.some_ext_id")
        self.assertEqual(omni.ext.get_extension_name("omni.kit.some_ext_id-1.0.0"), "omni.kit.some_ext_id")
        self.assertEqual(omni.ext.get_extension_name("omni.kit.some_ext_id-variant-1.0.0"), "omni.kit.some_ext_id-variant")

        # Ext id from module
        self.assertEqual(manager.get_extension_id_by_module(__name__), my_id)
        self.assertEqual(manager.get_extension_id_by_module("omni.kit.core.tests"), my_id)
        self.assertEqual(manager.get_extension_id_by_module("omni.kit.core.tests.test_base"), my_id)
        self.assertEqual(manager.get_extension_id_by_module("omni.kit.core"), None)

        # Ext path
        root = Path(os.path.dirname(__file__)).parent.parent.parent.parent
        self.assertEqual(root, Path(manager.get_extension_path(my_id)))
        self.assertEqual(root, Path(manager.get_extension_path_by_module(__name__)))
        self.assertEqual(root, Path(manager.get_extension_path_by_module("omni.kit.core.tests")))
        self.assertEqual(root, Path(manager.get_extension_path_by_module("omni.kit.core.tests.test_base")))
        self.assertEqual(manager.get_extension_path_by_module("omni.kit.core"), None)


    async def test_solve_extensions(self):
        manager = omni.kit.app.get_app().get_extension_manager()

        expected_sequence = ["omni.kit.actions.core", "omni.kit.commands"]

        # pass by name
        result, exts, err = manager.solve_extensions(["omni.kit.commands"], add_enabled=False, return_only_disabled=False)
        self.assertTrue(result)
        self.assertListEqual([e["name"] for e in exts], expected_sequence)
        self.assertEqual(err, "")

        commands_ext_id = exts[1]["id"]

        # pass by id
        result, exts, err = manager.solve_extensions([commands_ext_id], add_enabled=False, return_only_disabled=False)
        self.assertTrue(result)
        self.assertListEqual([e["name"] for e in exts], expected_sequence)
        self.assertEqual(err, "")


        # pass both
        result, exts, err = manager.solve_extensions(expected_sequence, add_enabled=False, return_only_disabled=False)
        self.assertTrue(result)
        self.assertListEqual([e["name"] for e in exts], expected_sequence)
        self.assertEqual(err, "")

        # pass wrong version
        result, exts, err = manager.solve_extensions(["omni.kit.actions.core-55.5.5", "omni.kit.commands"], add_enabled=False, return_only_disabled=False)
        self.assertFalse(result)
        self.assertTrue(len(err) > 0)

        # do not return enabled
        result, exts, err = manager.solve_extensions(["omni.kit.core.tests"], add_enabled=False, return_only_disabled=True)
        self.assertTrue(result)
        self.assertTrue(len(exts) == 0)
