# -*- coding: utf-8 -*-

import os.path

from outwiker.api.gui.defines import TOOLBAR_PLUGINS
from outwiker.api.pages.wiki.defines import MENU_WIKI_COMMANDS, PAGE_TYPE_STRING
from outwiker.api.gui.actions import ActionsGUIController, ActionGUIInfo, ButtonInfo
from outwiker.api.gui.defines import PREF_PANEL_PLUGINS

from .i18n import get_
from .preferencepanel import PreferencePanel
from .actions import InsertSourceAction


class Controller:
    """
    This class is responsible for the main plugin interface functionality
    """

    def __init__(self, plugin, application):
        """
        plugin - Plugin owner (instance of PluginSource class)
        application - instance of Application class
        """
        self._plugin = plugin
        self._application = application

        self._GUIController = ActionsGUIController(
            self._application,
            PAGE_TYPE_STRING,
        )

    def initialize(self):
        """
        Controller initialization on plugin activation.
        Subscribe to required events
        """
        global _
        _ = get_()

        self._application.onWikiParserPrepare += self.__onWikiParserPrepare
        self._application.onPreferencesDialogCreate += self.__onPreferencesDialogCreate
        self._initialize_guicontroller()

    def _get_image_full_path(self, fname):
        return os.path.join(self._plugin.pluginPath, "images", fname)

    def _initialize_guicontroller(self):
        action_gui_info = [
            ActionGUIInfo(
                InsertSourceAction(self._application),
                MENU_WIKI_COMMANDS,
                ButtonInfo(TOOLBAR_PLUGINS, self._get_image_full_path("source.svg")),
            ),
        ]

        if self._application.mainWindow is not None:
            self._GUIController.initialize(action_gui_info)

    def destroy(self):
        """
        Called when the plugin is disabled
        """
        self._application.onWikiParserPrepare -= self.__onWikiParserPrepare
        self._application.onPreferencesDialogCreate -= self.__onPreferencesDialogCreate
        self._destroy_guicontroller()

    def _destroy_guicontroller(self):
        if self._application.mainWindow is not None:
            self._GUIController.destroy()

    def __onWikiParserPrepare(self, parser):
        """
        Called before wiki text parsing. Adding (:source:) command
        """
        from .commandsource import CommandSource

        parser.addCommand(CommandSource(parser, self._application.config))

    def __onPreferencesDialogCreate(self, dialog):
        """
        Adding preferences page
        """
        prefPanel = PreferencePanel(dialog.treeBook, self._application.config)
        dialog.addPage(
            prefPanel,
            _("Source"),
            parent_page_tag=PREF_PANEL_PLUGINS,
            icon_fname=self._get_image_full_path("source.svg"),
        )
