import asyncio
import math
import unittest
import omni.kit.test
from omni.kit.tagging import get_tagging_instance


class TestTaggingPlugin(omni.kit.test.AsyncTestCase):
    async def test_tagging_plugin_load(self):
        tagging_interface = get_tagging_instance()
        self.assertTrue(tagging_interface is not None)

    async def test_tagging_validation(self):
        tagging_interface = get_tagging_instance()

        valid_tags = [
            "book",
            "book=10",
            "appearance.book",
            "appearance.book=10",
            "appearance.generated.book",
            "appearance.generated.book=10",
            "visual.appearance.generated.book",
            "visual.appearance.generated.book=10",
            ".appearance.generated.book",
            ".appearance.generated.book=10",
            ".generated.book",
            ".generated.book=10",
        ]

        invalid_tags = ["", ".", "a=b=c", "...", ".test", "test.", "test.=1", "test..test", "test..test=10"]

        for tag in valid_tags:
            self.assertTrue(tagging_interface.validate_tag(tag))

        for tag in invalid_tags:
            self.assertFalse(tagging_interface.validate_tag(tag))

    async def test_sorted_tags(self):

        expected_dict = [
            {"name": "binder", "tag_namespace": ".appearance.generated", "value": "50"},
            {"name": "notebook", "tag_namespace": "appearance", "value": ""},
            {"name": "book", "tag_namespace": "appearance", "value": ""},
            {"name": "book", "tag_namespace": ".appearance.generated", "value": "80"},
            {"name": "Lighter", "tag_namespace": ".ImageNet.generated", "value": "36"},
            {"name": "Wafer", "tag_namespace": ".ImageNet.generated", "value": "47"},
            {"name": "Knife", "tag_namespace": ".ImageNet.generated", "value": "2"},
            {"name": "Knife", "tag_namespace": ".ImageNet.excluded", "value": ""},
        ]

        sorted_tags = ["book", "notebook", "binder", "Wafer", "Lighter"]

        tagging_interface = get_tagging_instance()
        ordered_tags = tagging_interface.ordered_tag_list(expected_dict)
        self.assertEqual(ordered_tags, sorted_tags)
