import asyncio
from xdg import DesktopEntry
from pathlib import Path
import subprocess
import logging
from gi.repository import Gio, GLib
import gettext
import izvor_utils as izvor

_ = gettext.gettext


logging.basicConfig(level=logging.DEBUG)


config_template = {
    "terminal": "gnome-terminal",
    "shell": "bash -c"
}


class Provider(izvor.Provider):
    def __init__(self, config: dict):
        self.config = config
        self.name = _("Terminal")
        self.icon = "utilities-terminal"
        self.description = _("Run search queries as commands in a terminal.")

    async def search(self, query: str):
        terminal_app = self.config["terminal"]
        shell = self.config["shell"]

        def execute():
            izvor.launch_command(f"{terminal_app} -- {shell} '{query}'", shell=True)

        yield {
            "name": _("Run"),
            "description": query,
            "image": ("logo", "utilities-terminal"),
            "execute": execute
        }
