import subprocess
from gi.repository import Nautilus, GObject

class ColumnExtension(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        try:
            # python2 version
            import urllib
            self._unquote_func = urllib.unquote
        except AttributeError:
            # python3 version
            import urllib.parse
            self._unquote_func = urllib.parse.unquote

    def menu_activate_cb(self, menu, file, opTodo):
        file_path = self._extract_file_path(file[0].get_uri())
        print "activate: " + file_path + " " + opTodo
        subprocess.Popen(["/opt/dikeIC/DikeIC", "-o", opTodo, "-f", file_path], cwd="/opt/dikeIC/").pid

    def _extract_file_path(self, uri):
        # strip 'file:///' prefix & urldecode path
        return self._unquote_func(uri[7:]).decode('utf8')

    def get_file_items(self, window, files):
        if len(files) != 1:
            return

        file = files[0]
        top_menu = Nautilus.MenuItem(
                name="DikeMenuExtension::Dike",
                label="DikeIC",
                tip="DikeIC"
            )

        submenuDike = Nautilus.Menu()
        top_menu.set_submenu(submenuDike)

        sub_menuFirmaItem = Nautilus.MenuItem(
            name="DikeMenuExtension::SMFirma",
            label="Firma",
            tip="Firma"
        )
        submenuDike.append_item(sub_menuFirmaItem)

        sub_menuFirmaRemotaItem = Nautilus.MenuItem(
            name="DikeMenuExtension::SMFirmaRemota",
            label="Firma Remota",
            tip="Firma Remota"
        )
        submenuDike.append_item(sub_menuFirmaRemotaItem)

        sub_menuVerificaItem = Nautilus.MenuItem(
            name="DikeMenuExtension::Verifica",
            label="Verifica",
            tip="Verifica"
        )
        submenuDike.append_item(sub_menuVerificaItem)

        subsubmenuFirma = Nautilus.Menu()
        sub_menuFirmaItem.set_submenu(subsubmenuFirma)

        subsub_menuFirmaItem = Nautilus.MenuItem(
            name="DikeMenuExtension::Firma",
            label="Firma",
            tip="Firma"
        )
        subsubmenuFirma.append_item(subsub_menuFirmaItem)

        subsub_menuFirmaMarcaItem = Nautilus.MenuItem(
            name="DikeMenuExtension::FirmaMarca",
            label="Firma e Marca",
            tip="Firma e Marca"
        )
        subsubmenuFirma.append_item(subsub_menuFirmaMarcaItem)

        subsubmenuFirmaRemota = Nautilus.Menu()
        sub_menuFirmaRemotaItem.set_submenu(subsubmenuFirmaRemota)

        subsub_menuFirmaRemotaItem = Nautilus.MenuItem(
            name="DikeMenuExtension::FirmaRemota",
            label="Firma Remota",
            tip="Firma Remota"
        )
        subsubmenuFirmaRemota.append_item(subsub_menuFirmaRemotaItem)

        subsub_menuFirmaRemotaMarcaItem = Nautilus.MenuItem(
                name="DikeMenuExtension::FirmaRemotaMarca",
                label="Firma Remota e Marca",
                tip="Firma Remota e Marca"
            )
        subsubmenuFirmaRemota.append_item(subsub_menuFirmaRemotaMarcaItem)

        subsub_menuFirmaItem.connect("activate", self.menu_activate_cb, files, "SH_F")
        subsub_menuFirmaMarcaItem.connect("activate", self.menu_activate_cb, files, "SH_FM")
        subsub_menuFirmaRemotaItem.connect("activate", self.menu_activate_cb, files, "SH_FR")
        subsub_menuFirmaRemotaMarcaItem.connect("activate", self.menu_activate_cb, files, "SH_FRM")
        sub_menuVerificaItem.connect("activate", self.menu_activate_cb, files, "SH_V")

        return top_menu,
