from typing import List

from wx.adv import EVT_WIZARD_PAGE_CHANGING

from clarabot_nano.control.configure.basic.admin_password import AdminPasswordPage
from clarabot_nano.control.configure.basic.finish import FinishPage
from clarabot_nano.control.configure.basic.index_ignore_mimes import IndexIgnoreMimesPage
from clarabot_nano.control.configure.basic.index_language import IndexLanguagePage
from clarabot_nano.control.configure.basic.intro import IntroPage
from clarabot_nano.control.configure.basic.name import NamePage
from clarabot_nano.control.nano.base import ConfigUpdateNanoRequestProgressStep
from clarabot_nano.control.wizard import SimpleWizard
from clarabot_nano.indexer.utility import get_index_supported_languages


class SimpleConfigurationWizard(SimpleWizard):
    def __init__(self, parent, config):
        SimpleWizard.__init__(self, parent)

        self.intro_page = IntroPage(self)
        self.name_page = NamePage(self, config["remote"]["name"])
        self.password_page = AdminPasswordPage(self, bool(config["remote"].get("admin_password")))
        self.index_language_page = IndexLanguagePage(
            self, list(get_index_supported_languages()), config["remote"]["indexed_languages"]
        )
        self.index_ignore_mimes_page = IndexIgnoreMimesPage(self, config["remote"]["indexer_ignored_mimes"])
        self.finish_page = FinishPage(self)

        self.Bind(EVT_WIZARD_PAGE_CHANGING, self._on_page_changing, self)

    def _on_page_changing(self, event):
        if event.GetDirection():
            if event.GetPage() is self.name_page:
                old_name, new_name = self.name_page.get_new_config()
                if not new_name:
                    event.Veto()
            elif event.GetPage() is self.password_page:
                has_password, new_password = self.password_page.get_new_config()
                if not new_password:
                    event.Veto()

    def config_update_steps(self) -> List[ConfigUpdateNanoRequestProgressStep]:
        steps = []
        old, new = self.name_page.get_new_config()
        if old != new:
            steps.append(ConfigUpdateNanoRequestProgressStep(gettext("Updating name"), "name", name=new))
        _, new = self.password_page.get_new_config()
        if isinstance(new, str):
            steps.append(
                ConfigUpdateNanoRequestProgressStep(
                    gettext("Updating admin password"), "admin_password", admin_password=new
                )
            )
        old, new = self.index_language_page.get_new_config()
        if old != new:
            steps.append(
                ConfigUpdateNanoRequestProgressStep(
                    gettext("Updating indexed languages"), "indexed_languages", indexed_languages=new
                )
            )
        old, new = self.index_ignore_mimes_page.get_new_config()
        if old != new:
            steps.append(
                ConfigUpdateNanoRequestProgressStep(
                    gettext("Updating indexing ignored mimetypes"), "indexer_ignored_mimes", indexer_ignored_mimes=new
                )
            )
        return steps
