from typing import NamedTuple
import re

from . import util

Icon = NamedTuple("Icon", [("code", str), ("color", str)])

default = Icon("\uf15b", color="#ffffff")


def filetype_icon(filetype: str):
    return icon_map.get(file_icon_map.get(filetype, ""))


def iconize(string: str, colored: bool = False) -> str:
    """Replaces icon-strings with the actual escape codes
    Expects the icon-strings to either be surrounded by colons (::)
    or bars (||). Colons mean to replace it with just the icon escape code.
    Bars mean to replace it with the icon, plus the longer name

    :param string: string to substitute icon-strings
    :param colored: whether ot not to return a
        pango-span with the accompanying icon color

    `iconize(':py:') -> '\\uf81f'`
    `iconize('|py|') -> '\\uf81f python'`
    """
    icon_regex = re.compile(r":([\w-]*):")
    filetypes = icon_regex.findall(string)
    replaces = string

    for filetype in filetypes:
        icon = filetype_icon(filetype)
        if icon:
            formatted = (
                util.pango_span(icon.code, color=icon.color) if colored else icon.code
            )
        else:
            formatted = filetype

        replaces = re.sub(f":{filetype}:", formatted + " ", replaces)

    long_name_regex = re.compile(r"\|([\w-]*)\|")
    filetypes = long_name_regex.findall(string)

    for filetype in filetypes:
        icon = filetype_icon(filetype)
        long_name = file_icon_map.get(filetype)
        if icon and long_name:
            formatted = (
                (util.pango_span(icon.code, color=icon.color) if colored else icon.code)
                + " "
                + long_name
            )
        else:
            formatted = filetype

        replaces = re.sub(fr"\|{filetype}\|", formatted, replaces)

    return replaces


# Uses Nerd Fonts Icons
# some are their actual icon, instead of the escape code, should fix
icon_map: dict[str, Icon] = {
    "html": Icon("\uf13b", color="#e44f39"),
    "markdown": Icon("\uf853", color="#42a5f5"),
    "css": Icon("\uf81b", color="#42a5f5"),
    "css-map": Icon("\ue749", color="#42a5f5"),
    "sass": Icon("\ue603", color="#ed507a"),
    "less": Icon("\ue60b", color="#277bd"),
    "json": Icon("\ue60b", color="#fbc13c"),
    "yaml": Icon("\ue60b", color="#f4443e"),
    "xml": Icon("\uf72d", color="#409945"),
    "project": Icon("\ue601", color="#409945"),
    "image": Icon("\uf71e", color="#30a69a"),
    "javascript": Icon("\ue74e", color="#ffca3d"),
    "javascript-map": Icon("\ue781", color="#ffca3d"),
    "test-jsx": Icon("\uf595", color="#23bcd4"),
    "test-js": Icon("\uf595", color="#ffca3d"),
    "react": Icon("\ue7ba", color="#23bcd4"),
    "settings": Icon("\uf013", color="#42a5f5"),
    "typescript": Icon("\ue628", color="#007acc"),
    "typescript-def": Icon("ﯤ", color="#388d1"),
    "test-ts": Icon("\uf595", color="#388d1"),
    "pdf": Icon("\uf724", color="#f4443e"),
    "table": Icon("\uf71a", color="#8bc34a"),
    "visualstudio": Icon("\ue70c", color="#ad63bc"),
    "database": Icon("\ue706", color="#ffca3d"),
    "mysql": Icon("\ue704", color="#15e86"),
    "postgresql": Icon("\ue76e", color="#31638c"),
    "sqlite": Icon("\ue7c4", color="#139542"),
    "csharp": Icon("\uf81a", color="#277bd"),
    "zip": Icon("\uf410", color="#afb42b"),
    "exe": Icon("\uf2d0", color="#e54d3a"),
    "java": Icon("\uf675", color="#f4443e"),
    "c": Icon("\ue61e", color="#277bd"),
    "cpp": Icon("\ufe61d", color="#277bd"),
    "go": Icon("\ue626", color="#20adc2"),
    "go-mod": Icon("\ue626", color="#ed507a"),
    "go-test": Icon("\ue626", color="#ffd54f"),
    "python": Icon("\uf81f", color="#34668f"),
    "python-misc": Icon("\uf820", color="#823d1c"),
    "url": Icon("\uf836", color="#42a5f5"),
    "console": Icon("\uf68c", color="#fa6f42"),
    "word": Icon("\uf72b", color="#1579b"),
    "certificate": Icon("\uf623", color="#f9593f"),
    "key": Icon("\uf805", color="#30a69a"),
    "font": Icon("\uf031", color="#f4443e"),
    "lib": Icon("\uf831", color="#8bc34a"),
    "ruby": Icon("\ue739", color="#e53d3a"),
    "crystal": Icon("\ue739", color="#333"),
    "gemfile": Icon("\ue21e", color="#e53d3a"),
    "fsharp": Icon("\ue7a7", color="#378bba"),
    "swift": Icon("ﯣ", color="#f95f3f"),
    "docker": Icon("\uf308", color="#187c9"),
    "powerpoint": Icon("\uf726", color="#d14733"),
    "video": Icon("\uf72a", color="#fd9a3e"),
    "virtual": Icon("\uf822", color="#39be5"),
    "email": Icon("\uf6ed", color="#42a5f5"),
    "audio": Icon("ﭵ", color="#ef5350"),
    "coffee": Icon("\uf675", color="#42a5f5"),
    "document": Icon("\uf718", color="#42a5f5"),
    "rust": Icon("\ue7a8", color="#fa6f42"),
    "raml": Icon("\ue60b", color="#42a5f5"),
    "xaml": Icon("ﭲ", color="#42a5f5"),
    "haskell": Icon("\ue61f", color="#fea83e"),
    "git": Icon("\ue702", color="#e54d3a"),
    "lua": Icon("\ue620", color="#42a5f5"),
    "clojure": Icon("\ue76a", color="#64dd17"),
    "groovy": Icon("\uf2a6", color="#29c6da"),
    "r": Icon("ﳒ", color="#1976d2"),
    "dart": Icon("\ue798", color="#57b6f0"),
    "mxml": Icon("\uf72d", color="#fea83e"),
    "assembly": Icon("\uf471", color="#fa6d3f"),
    "vue": Icon("\ufd42", color="#41b883"),
    "vue-config": Icon("\ufd42", color="#3a796e"),
    "lock": Icon("\uf83d", color="#ffd54f"),
    "handlebars": Icon("\ue60f", color="#fa6f42"),
    "perl": Icon("\ue769", color="#9575cd"),
    "elixir": Icon("\ue62d", color="#9575cd"),
    "erlang": Icon("\ue7b1", color="#f4443e"),
    "twig": Icon("\ue61c", color="#9bb92f"),
    "julia": Icon("\ue624", color="#86529f"),
    "elm": Icon("\ue62c", color="#60b5cc"),
    "smarty": Icon("\uf834", color="#ffcf3c"),
    "stylus": Icon("\ue600", color="#c0ca33"),
    "verilog": Icon("\ufb19", color="#fa6f42"),
    "robot": Icon("ﮧ", color="#f9593f"),
    "solidity": Icon("ﲹ", color="#388d1"),
    "yang": Icon("ﭾ", color="#42a5f5"),
    "vercel": Icon("\uf47e", color="#cfd8dc"),
    "applescript": Icon("\uf302", color="#78909c"),
    "cake": Icon("\uf5ea", color="#fa6f42"),
    "nim": Icon("\uf6a4", color="#ffca3d"),
    "todo": Icon("\uf058", color="#7cb342"),
    "nix": Icon("\uf313", color="#5075c1"),
    "http": Icon("\uf484", color="#42a5f5"),
    "webpack": Icon("ﰩ", color="#8ed6fb"),
    "ionic": Icon("\ue7a9", color="#4f8ff7"),
    "gulp": Icon("\ue763", color="#e53d3a"),
    "nodejs": Icon("\uf898", color="#8bc34a"),
    "npm": Icon("\ue71e", color="#cb3837"),
    "yarn": Icon("\uf61a", color="#2c8ebb"),
    "android": Icon("\uf531", color="#8bc34a"),
    "tune": Icon("ﭩ", color="#fbc13c"),
    "contributing": Icon("\uf64d", color="#ffca3d"),
    "readme": Icon("\uf7fb", color="#42a5f5"),
    "changelog": Icon("ﮦ", color="#8bc34a"),
    "credits": Icon("\uf75f", color="#9ccc65"),
    "authors": Icon("\uf0c0", color="#f4443e"),
    "favicon": Icon("\ue623", color="#ffd54f"),
    "karma": Icon("\ue622", color="#3cbeae"),
    "travis": Icon("\ue77e", color="#cb3a49"),
    "heroku": Icon("\ue607", color="#6963b9"),
    "gitlab": Icon("\uf296", color="#e24539"),
    "bower": Icon("\ue61a", color="#ef583c"),
    "conduct": Icon("\uf64b", color="#cddc39"),
    "jenkins": Icon("\ue767", color="#f0d6b7"),
    "code-climate": Icon("\uf7f4", color="#eeeeee"),
    "log": Icon("\uf719", color="#afb42b"),
    "ejs": Icon("\ue618", color="#ffca3d"),
    "grunt": Icon("\ue611", color="#fbaa3d"),
    "django": Icon("\ue71d", color="#43a047"),
    "makefile": Icon("\uf728", color="#ef5350"),
    "bitbucket": Icon("\uf171", color="#1f88e5"),
    "d": Icon("\ue7af", color="#f4443e"),
    "mdx": Icon("\uf853", color="#ffca3d"),
    "azure-pipelines": Icon("\uf427", color="#1465c0"),
    "azure": Icon("ﴃ", color="#1f88e5"),
    "razor": Icon("\uf564", color="#42a5f5"),
    "asciidoc": Icon("\uf718", color="#f4443e"),
    "edge": Icon("\uf564", color="#ef6f3c"),
    "scheme": Icon("ﬦ", color="#f4443e"),
    "3d": Icon("\ue79b", color="#28b6f6"),
    "svg": Icon("ﰟ", color="#ffb53e"),
    "vim": Icon("\ue62b", color="#43a047"),
    "moonscript": Icon("\uf186", color="#fbc13c"),
    "codeowners": Icon("\uf507", color="#afb42b"),
    "disc": Icon("\ue271", color="#b0bec5"),
    "fortran": Icon("F", color="#fa6f42"),
    "tcl": Icon("\ufbd1", color="#ef5350"),
    "liquid": Icon("\ue275", color="#28b6f6"),
    "prolog": Icon("\ue7a1", color="#ef5350"),
    "husky": Icon("\uf8e8", color="#e5e5e5"),
    "coconut": Icon("\uf5d2", color="#8d6e63"),
    "sketch": Icon("\uf6c7", color="#ffc23d"),
    "pawn": Icon("\ue261", color="#ef6f3c"),
    "commitlint": Icon("ﰖ", color="#2b9689"),
    "dhall": Icon("\uf448", color="#78909c"),
    "dune": Icon("\uf7f4", color="#f47f3d"),
    "shaderlab": Icon("ﮭ", color="#1976d2"),
    "command": Icon("גּ", color="#afbcc2"),
    "stryker": Icon("\uf05b", color="#ef5350"),
    "modernizr": Icon("\ue720", color="#ea4863"),
    "roadmap": Icon("ﭭ", color="#30a69a"),
    "debian": Icon("\uf306", color="#d33d4c"),
    "ubuntu": Icon("\uf31c", color="#d64935"),
    "arch": Icon("\uf303", color="#218eca"),
    "redhat": Icon("\uf316", color="#e73d3a"),
    "gentoo": Icon("\uf30d", color="#948dd3"),
    "linux": Icon("\ue712", color="#eecf37"),
    "raspberry-pi": Icon("\uf315", color="#d03c4c"),
    "manjaro": Icon("\uf312", color="#49b95a"),
    "opensuse": Icon("\uf314", color="#6fb424"),
    "fedora": Icon("\uf30a", color="#3467ac"),
    "freebsd": Icon("\uf30c", color="#af2c2a"),
    "centOS": Icon("\uf304", color="#9d5387"),
    "alpine": Icon("\uf300", color="#e577b"),
    "mint": Icon("\uf30f", color="#7dbe3a"),
    "routing": Icon("נּ", color="#43a047"),
    "laravel": Icon("\ue73f", color="#f85051"),
    "directory": Icon("\ue5ff", color="#ffffff"),
    "dir-config": Icon("\ue5fc", color="#20adc2"),
    "dir-controller": Icon("\ue5fc", color="#ffc23d"),
    "dir-git": Icon("\ue5fb", color="#fa6f42"),
    "dir-github": Icon("\ue5fd", color="#546e7a"),
    "dir-npm": Icon("\ue5fa", color="#cb3837"),
    "dir-include": Icon("\uf756", color="#39be5"),
    "dir-import": Icon("\uf756", color="#afb42b"),
    "dir-upload": Icon("\uf758", color="#fa6f42"),
    "dir-download": Icon("\uf74c", color="#4caf50"),
    "dir-secure": Icon("\uf74f", color="#f9a93c"),
    "dir-images": Icon("\uf74e", color="#2b9689"),
    "dir-environment": Icon("\uf74e", color="#66bb6a"),
    "hamburger": Icon("\uf85b", color="#ffffff"),
}

# Each value should match with a name
# in the above dictionary
file_icon_map: dict[str, str] = {
    "directory": "directory",
    "htm": "html",
    "html": "html",
    "xhtml": "html",
    "html_vm": "html",
    "asp": "html",
    "md": "markdown",
    "markdown": "markdown",
    "rst": "markdown",
    "css": "css",
    "scss": "sass",
    "sass": "sass",
    "less": "less",
    "json": "json",
    "tsbuildinfo": "json",
    "json5": "json",
    "jsonl": "json",
    "ndjson": "json",
    "yaml": "yaml",
    "yaml-tmlanguage": "yaml",
    "yml": "yaml",
    "xml": "xml",
    "plist": "xml",
    "xsd": "xml",
    "dtd": "xml",
    "xsl": "xml",
    "xslt": "xml",
    "resx": "xml",
    "iml": "xml",
    "xquery": "xml",
    "tmLanguage": "xml",
    "manifest": "xml",
    "project": "project",
    "png": "image",
    "jpeg": "image",
    "jpg": "image",
    "gif": "image",
    "ico": "image",
    "tif": "image",
    "tiff": "image",
    "psd": "image",
    "psb": "image",
    "ami": "image",
    "apx": "image",
    "bmp": "image",
    "bpg": "image",
    "brk": "image",
    "cur": "image",
    "dds": "image",
    "dng": "image",
    "exr": "image",
    "fpx": "image",
    "gbr": "image",
    "img": "image",
    "jbig2": "image",
    "jb2": "image",
    "jng": "image",
    "jxr": "image",
    "pbm": "image",
    "pgf": "image",
    "pic": "image",
    "raw": "image",
    "webp": "image",
    "eps": "image",
    "afphoto": "image",
    "ase": "image",
    "aseprite": "image",
    "clip": "image",
    "cpt": "image",
    "heif": "image",
    "heic": "image",
    "kra": "image",
    "mdp": "image",
    "ora": "image",
    "pdn": "image",
    "reb": "image",
    "sai": "image",
    "tga": "image",
    "xcf": "image",
    "js": "javascript",
    "esx": "javascript",
    "mj": "javascript",
    "jsx": "react",
    "tsx": "react",
    "ini": "settings",
    "dlc": "settings",
    "dll": "settings",
    "config": "settings",
    "conf": "settings",
    "properties": "settings",
    "prop": "settings",
    "settings": "settings",
    "option": "settings",
    "props": "settings",
    "toml": "settings",
    "prefs": "settings",
    "dotsettings": "settings",
    "cfg": "settings",
    "ts": "typescript",
    "pdf": "pdf",
    "xlsx": "table",
    "xls": "table",
    "csv": "table",
    "tsv": "table",
    "csproj": "visualstudio",
    "ruleset": "visualstudio",
    "sln": "visualstudio",
    "suo": "visualstudio",
    "vb": "visualstudio",
    "vbs": "visualstudio",
    "vcxitems": "visualstudio",
    "vcxproj": "visualstudio",
    "pdb": "database",
    "sql": "mysql",
    "pks": "database",
    "pkb": "database",
    "accdb": "database",
    "mdb": "database",
    "sqlite": "sqlite",
    "sqlite3": "sqlite",
    "pgsql": "postgresql",
    "postgres": "postgresql",
    "psql": "postgresql",
    "cs": "csharp",
    "csx": "csharp",
    "zip": "zip",
    "tar": "zip",
    "gz": "zip",
    "xz": "zip",
    "br": "zip",
    "bzip2": "zip",
    "gzip": "zip",
    "brotli": "zip",
    "7z": "zip",
    "rar": "zip",
    "tgz": "zip",
    "exe": "exe",
    "msi": "exe",
    "java": "java",
    "jar": "java",
    "jsp": "java",
    "c": "c",
    "m": "c",
    "i": "c",
    "mi": "c",
    "cc": "cpp",
    "cpp": "cpp",
    "cxx": "cpp",
    "c++": "cpp",
    "cp": "cpp",
    "mm": "cpp",
    "mii": "cpp",
    "ii": "cpp",
    "go": "go",
    "py": "python",
    "pyc": "python-misc",
    "whl": "python-misc",
    "url": "url",
    "sh": "console",
    "ksh": "console",
    "csh": "console",
    "tcsh": "console",
    "zsh": "console",
    "bash": "console",
    "bat": "console",
    "cmd": "console",
    "awk": "console",
    "fish": "console",
    "doc": "word",
    "docx": "word",
    "rtf": "word",
    "cer": "certificate",
    "cert": "certificate",
    "crt": "certificate",
    "pub": "key",
    "key": "key",
    "pem": "key",
    "asc": "key",
    "gpg": "key",
    "woff": "font",
    "woff2": "font",
    "ttf": "font",
    "eot": "font",
    "suit": "font",
    "otf": "font",
    "bmap": "font",
    "fnt": "font",
    "odttf": "font",
    "ttc": "font",
    "font": "font",
    "fonts": "font",
    "sui": "font",
    "ntf": "font",
    "mrf": "font",
    "lib": "lib",
    "bib": "lib",
    "rb": "ruby",
    "erb": "ruby",
    "fs": "fsharp",
    "fsx": "fsharp",
    "fsi": "fsharp",
    "fsproj": "fsharp",
    "swift": "swift",
    "dockerignore": "docker",
    "dockerfile": "docker",
    "pptx": "powerpoint",
    "ppt": "powerpoint",
    "pptm": "powerpoint",
    "potx": "powerpoint",
    "potm": "powerpoint",
    "ppsx": "powerpoint",
    "ppsm": "powerpoint",
    "pps": "powerpoint",
    "ppam": "powerpoint",
    "ppa": "powerpoint",
    "webm": "video",
    "mkv": "video",
    "flv": "video",
    "vob": "video",
    "ogv": "video",
    "ogg": "video",
    "gifv": "video",
    "avi": "video",
    "mov": "video",
    "qt": "video",
    "wmv": "video",
    "yuv": "video",
    "rm": "video",
    "rmvb": "video",
    "mp4": "video",
    "m4v": "video",
    "mpg": "video",
    "mp2": "video",
    "mpeg": "video",
    "mpe": "video",
    "mpv": "video",
    "m2v": "video",
    "vdi": "virtual",
    "vbox": "virtual",
    "vbox-prev": "virtual",
    "ics": "email",
    "mp3": "audio",
    "flac": "audio",
    "m4a": "audio",
    "wma": "audio",
    "aiff": "audio",
    "coffee": "coffee",
    "cson": "coffee",
    "iced": "coffee",
    "txt": "document",
    "rs": "rust",
    "raml": "raml",
    "xaml": "xaml",
    "hs": "haskell",
    "patch": "git",
    "lua": "lua",
    "clj": "clojure",
    "cljs": "clojure",
    "cljc": "clojure",
    "groovy": "groovy",
    "r": "r",
    "rmd": "r",
    "dart": "dart",
    "mxml": "mxml",
    "asm": "assembly",
    "a51": "assembly",
    "inc": "assembly",
    "nasm": "assembly",
    "s": "assembly",
    "ms": "assembly",
    "agc": "assembly",
    "ags": "assembly",
    "aea": "assembly",
    "argus": "assembly",
    "mitigus": "assembly",
    "binsource": "assembly",
    "vue": "vue",
    "lock": "lock",
    "hbs": "handlebars",
    "mustache": "handlebars",
    "pm": "perl",
    "raku": "perl",
    "ex": "elixir",
    "exs": "elixir",
    "eex": "elixir",
    "leex": "elixir",
    "erl": "erlang",
    "twig": "twig",
    "jl": "julia",
    "elm": "elm",
    "tpl": "smarty",
    "styl": "stylus",
    "v": "verilog",
    "vhd": "verilog",
    "sv": "verilog",
    "svh": "verilog",
    "robot": "robot",
    "sol": "solidity",
    "yang": "yang",
    "cr": "crystal",
}
