"""
Minimal GUI setup for displaying an unexpected error to the user.
"""
import wx


if "gettext" not in globals():
    try:
        from clarabot_nano.common.i18n import install_default_l10n

        install_default_l10n()
    except Exception:

        def gettext(s):
            return s


class ErrorFrame(wx.Frame):
    def __init__(self, error_message):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Clarabot Nano - Core Error")
        self.SetSizeHints(320, 320)
        self.SetSize(600, 400)

        panel = wx.Panel(self)

        layout = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(panel, wx.ID_ANY, gettext("Clarabot Nano encountered an error."))
        layout.Add(label, 0, wx.ALL | wx.EXPAND, 8)
        err = wx.TextCtrl(panel, wx.ID_ANY, style=wx.TE_MULTILINE | wx.TE_READONLY)

        preamble = gettext(
            "There is no automatic error reporting in Nano for privacy reasons.\n"
            "If you need help in resolving the problem, have a look at our documentation or submit a support ticket."
        )
        err.SetValue(f"{preamble}\n\n{error_message}")
        layout.Add(err, 1, wx.ALL | wx.EXPAND, 8)

        panel.SetSizer(layout)


class ErrorApp(wx.App):
    @classmethod
    def display(cls, error):
        return cls(error).OnRun()

    def __init__(self, error):
        self._error = str(error)
        wx.App.__init__(self, False)
        self.SetAppName("clarabot-nano-error")
        self.SetAppDisplayName("Clarabot Nano - Core Error")

    def OnInit(self):
        w = ErrorFrame(self._error)
        w.Show()
        self.SetTopWindow(w)
        return True
