import inspect
import pathlib

import carb
import carb.settings
import carb.tokens

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

import omni.kit.compatibility_checker

class CompatibilityCheckerTest(omni.kit.test.AsyncTestCase):
    async def setUp(self):
        self._settings = carb.settings.acquire_settings_interface()
        self._app_window_factory = omni.appwindow.acquire_app_window_factory_interface()
        self._renderer = omni.kit.renderer.bind.acquire_renderer_interface()

    def __test_name(self) -> str:
        return f"{self.__module__}.{self.__class__.__name__}.{inspect.stack()[2][3]}"

    async def tearDown(self):
        self._renderer = None
        self._app_window_factory = None
        self._settings = None


    async def test_1_check_gpu(self):
        supported_gpus = [
            "*GeForce RTX ????*",
            "*Quadro RTX ????*",
            "*RTX ?????*",
            "*TITAN RTX*"
        ]

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["NVIDIA GeForce RTX 2060 OC"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["GeForce RTX 2080 Ti"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["NVIDIA GeForce RTX 3080"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["NVIDIA Quadro RTX A6000"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["Quadro RTX 8000"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["RTX A6000"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["TITAN RTX"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["NVIDIA GeForce GTX 1060 OC"], supported_gpus)
        self.assertTrue(gpu_good is not True)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["NVIDIA GeForce RTX 2060 OC", "Not NVIDIA not GeForce"], supported_gpus)
        self.assertTrue(gpu_good)

        gpu_good = omni.kit.compatibility_checker.check_supported_gpu(["Not NVIDIA not GeForce", "NVIDIA GeForce RTX 2060 OC"], supported_gpus)
        self.assertTrue(gpu_good)

    async def test_2_check_drivers_blacklist(self):
        driver_blacklist_ranges = [
            ["0.0", "100.0", "Minimum RTX req"],
            ["110.0", "111.0", "Bugs"],
            ["120.3", "120.5", "More bugs"],
        ]

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([95, 0], driver_blacklist_ranges)
        self.assertTrue(driver_good is not True)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([105, 0], driver_blacklist_ranges)
        self.assertTrue(driver_good)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([110, 5], driver_blacklist_ranges)
        self.assertTrue(driver_good is not True)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([115, 0], driver_blacklist_ranges)
        self.assertTrue(driver_good)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([120, 3], driver_blacklist_ranges)
        self.assertTrue(driver_good is not True)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([120, 4], driver_blacklist_ranges)
        self.assertTrue(driver_good is not True)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([120, 5], driver_blacklist_ranges)
        self.assertTrue(driver_good is True)

        driver_good, driver_blacklist_range = omni.kit.compatibility_checker.check_driver_version([130, 5], driver_blacklist_ranges)
        self.assertTrue(driver_good)
