import os
from typing import cast

# pylint: disable=C0111
from qutebrowser.config.config import ConfigContainer  # noqa: F401
from qutebrowser.config.configfiles import ConfigAPI  # noqa: F401

config: ConfigAPI = cast(ConfigAPI, config)  # noqa: F821 pylint: disable=E0602,C0103
c: ConfigContainer = cast(ConfigContainer, c)  # noqa: F821 pylint: disable=E0602,C0103

# Autogenerated config.py
# Documentation:
#   qute://help/configuring.html
#   qute://help/settings.html

# load additional settings configured via autoconfig.yml
_ = config.load_autoconfig()

c.content.javascript.log_message.excludes = {
    "userscript:_qute_stylesheet": [
        "*Refused to apply inline style because it violates the following Content Security Policy directive: *"
    ],
    "userscript:_qute_js": ["*TrustedHTML*"],
}

term = os.getenv("TERMINAL", "xterm")

c.completion.web_history.max_items = 1000
c.hints.uppercase = True
c.editor.command = [
    term,
    "start",
    "--always-new-process",
    "nvim",
    "-f",
    "{file}",
    "-c",
    "normal {line}G{column0}l",
]

# change filepicker
c.fileselect.handler = "external"
picker = [
    term,
    "start",
    "--class",
    "float",
    "vifm",
    "--choose-files",
    "{}",
]
c.fileselect.single_file.command = picker
c.fileselect.multiple_files.command = picker

c.downloads.location.directory = os.getenv("XDG_DOWNLOAD_DIR", "~/downloads")
c.downloads.location.prompt = False

config.source("alias.py")
config.source("maps.py")
config.source("content.py")
config.source("searchengines.py")
config.source("redirects.py")

# Tab-Bar
# have tab bar on the right, not on the top
c.tabs.background = True
c.tabs.title.format = "{index} {audio}{perc}{current_title}"
c.tabs.position = "right"
c.tabs.width = "15%"
c.tabs.show = "multiple"
c.tabs.show_switching_delay = 2000
c.statusbar.show = "always"

c.colors.webpage.bg = "#555555"

# Prevents *all* tabs from being loaded on restore, only loads on activating them
c.session.lazy_restore = True

# for code_select.py userscript
# Allows copying code sections to clipboard easily
c.hints.selectors["code"] = [
    # Selects all code tags whose direct parent is not a pre tag
    ":not(pre) > code",
    "pre",
]

# give the browser nice theme colors
state_dir = os.environ.get("XDG_STATE_HOME", f"{os.environ['HOME']}/.local/state")
colorscheme = f"{state_dir}/qutebrowser/colorscheme.py"
if os.path.isfile(colorscheme):
    config.source(colorscheme)

c.url.start_pages = "https://start.duckduckgo.com/html"
