import re
import omni

from urllib.parse import unquote


class Utils:
    MDL_RE = re.compile("^.*\\.mdl?$", re.IGNORECASE)
    # References https://gitlab-master.nvidia.com/omniverse/rtxdev/kit/blob/d37f0906c58cb1a5d8591f9e47125b4154b19b88/rendering/source/plugins/common/UDIM.h#L22
    # for regex details to detect udim textures.
    UDIM_MARKER_RE = re.compile("^.*(<UDIM>|<UVTILE0>|<UVTILE1>).*")
    UDIM_GROUP_RE = re.compile("^(.*)(<UDIM>|<UVTILE0>|<UVTILE1>)(.*)")

    @staticmethod
    def normalize_path(path):
        path = omni.client.normalize_url(path)

        # Hard-decoding url currently since combine_urls will encode url
        path = unquote(path)

        return path.replace("\\", "/")

    @staticmethod
    def compute_absolute_path(base_path, path):
        # Handles old omni path
        if path.startswith("omni:"):
            path = path[5:]
        absolute_path = omni.client.combine_urls(base_path, path)

        return Utils.normalize_path(absolute_path)

    @staticmethod
    def is_material(path):
        if not path:
            return False

        path = Utils.remove_query_from_url(path)
        if Utils.MDL_RE.match(path):
            return True

        return False

    @staticmethod
    def is_local_path(path):
        if not path:
            return False

        client_url = omni.client.break_url(path)
        return client_url and client_url.is_raw

    @staticmethod
    def is_omniverse_path(path):
        return path and path.startswith("omniverse://")

    @staticmethod
    def remove_query_from_url(url):
        if not url:
            return url

        client_url = omni.client.break_url(url)
        url_without_query = omni.client.make_url(
            scheme=client_url.scheme,
            user=client_url.user,
            host=client_url.host,
            port=client_url.port,
            path=client_url.path,
            query=None,
            fragment=client_url.fragment,
        )
        url_without_query = url_without_query.replace("\\", "/")

        return url_without_query

    @staticmethod
    def is_udim_texture(path):
        if not path:
            return False

        path = Utils.remove_query_from_url(path)
        url = omni.client.break_url(path)
        if url and Utils.UDIM_MARKER_RE.match(url.path):
            return True

        return False

    @staticmethod
    def is_udim_wildcard_texture(path, udim_texture_path):
        if not path or not udim_texture_path:
            return False

        udim_path = Utils.remove_query_from_url(udim_texture_path)
        url = omni.client.break_url(udim_path)
        groups = Utils.UDIM_GROUP_RE.match(url.path)
        if not groups:
            return False

        base_path = groups[1]
        suffix_path = groups[3]
        wildcard_re = re.compile(re.escape(base_path) + "((\\d\\d\\d\\d)|(_u\\d*_v\\d*))" + suffix_path)
        path = Utils.remove_query_from_url(path)
        url = omni.client.break_url(path)
        if wildcard_re.match(url.path):
            return True

        return False

    @staticmethod
    def make_relative_path(relative_to, path):
        relative_path = omni.client.make_relative_url(relative_to, path)

        return Utils.normalize_path(relative_path)
