import wx
from .util import create_config_file
from .common import BOS_TOOLBOX_LANGUAGES, BOS_TOOLBOX_CONFIG_FILE


class LanguageWindow(wx.Frame):
    def __init__(self, *args, **kw):
        super(LanguageWindow, self).__init__(*args, **kw)
        self.win_size = wx.Size((500, 80))
        self.boarder = 10
        self.title = 'Select language'
        self.languages = BOS_TOOLBOX_LANGUAGES
        self.language = 'english'
        self.InitUI()

    def InitUI(self):
        pnl = wx.Panel(self)

        self.cb = wx.ComboBox(
            pnl, value=self.language, choices=self.languages, style=wx.CB_READONLY
        )
        self.cb.Bind(wx.EVT_COMBOBOX, self.on_select)

        vbox = wx.BoxSizer(wx.VERTICAL)

        gs = wx.GridSizer(1, 2, 5, 5)

        self.button = wx.Button(pnl, label='Ok')
        self.button.Bind(wx.EVT_BUTTON, self.generate_json)

        gs.AddMany([(self.cb, 0, wx.EXPAND), (self.button, 0, wx.EXPAND)])
        vbox.Add(gs, flag=wx.EXPAND | wx.ALL, border=self.boarder)
        pnl.SetSizer(vbox)

        self.SetSize(self.win_size)
        self.SetTitle(self.title)
        self.SetMinSize(self.win_size)
        self.SetMaxSize(self.win_size)
        self.Centre()
        self.Show(True)

    def generate_json(self, e):
        if self.language is not None:
            create_config_file(self.language, BOS_TOOLBOX_CONFIG_FILE)
            self.Close(True)

    def on_select(self, e):
        self.language = e.GetString()
