import socket

import wx

from clarabot_nano.control.wizard import SimpleWizardPage


class NamePage(SimpleWizardPage):
    def __init__(self, parent, name: str):
        SimpleWizardPage.__init__(
            self,
            parent,
            gettext(
                "This instance of Nano shall be named to help identification and remote management. "
                "This name will only be visible to the owner ever."
            ),
        )
        self._old = name

        self.name = wx.TextCtrl(self, wx.ID_ANY, name)
        self.name.SetHint(gettext("The Nano must have a non-empty name!"))
        self.layout.Add(self.name, 0, wx.EXPAND | wx.BOTTOM, 8)
        self.default = wx.Button(self, wx.ID_ANY, gettext("Use the name of the localhost"))
        self.layout.Add(self.default, 0, wx.EXPAND)

        self.Bind(wx.EVT_BUTTON, self._on_default, self.default)

    def _on_default(self, *args):
        self.name.SetValue(socket.gethostname())

    def get_new_config(self):
        return self._old, self.name.GetValue()
