# Path: multi_input_box.py
# class MultiInputBox(Popup):
#     ok_text = StringProperty('OK')
#     cancel_text = StringProperty('Cancel')
# 
#     def __init__(self, **kwargs):
#         self.window = Window
#         super(MultiInputBox, self).__init__(**kwargs)
#         self.content = self.ids["content"]
#         self.contentButtons = self.ids["contentButtons"]
#         self.wl = []
# 
#     def _dismiss(self):
#         self.dismiss()
# 
#     def open(self):
#         super(MultiInputBox, self).open()
# 
#     def _ok(self):
#         if self.optionCallBack is not None:
#             opts = {}
#             for x in self.wl:
#                 opts[x[0]] = x[1].text
#             self.optionCallBack(opts)
#         self.dismiss()
# 
#     def on_open(self):
#         if self.wl:
#             self.wl[0][1].focus = True
# 
#     def setOptions(self, options, callBack):
#         self.optionCallBack = callBack
#         self.contentButtons.clear_widgets()
#         self.wl = []
#         for name in options:
#             self.contentButtons.add_widget(Label(text=name, size_hint_y=None, height='30dp', halign='right'))
#             tw = TextInput(multiline=False, write_tab=False, use_bubble=False, use_handles=False)
#             self.contentButtons.add_widget(tw)
#             self.wl.append((name, tw))


from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import mainthread
from multi_input_box import MultiInputBox

class ConfigEditor(Screen):

    @mainthread
    def _add_line(self, line):
        if not line.lstrip().startswith("#"):
            t = line.split()
            if len(t) >= 2:
                self.rv.data.append({'k': t[0], 'v': t[1]})
            elif t[0] == 'ok':
                # add dummy lines at end so we can edit the last few lines without keyboard covering them
                for i in range(10):
                    self.rv.data.append({'k': '', 'v': ''})

                self.app.comms.redirect_incoming(None)

    def open(self):
        self.rv.data = []
        self.manager.current = 'config_editor'
        self.app = App.get_running_app()
        # get config, parse and populate
        self.app.comms.redirect_incoming(self._add_line)
        # issue command
        self.app.comms.write('cat /sd/config\n')
        self.app.comms.write('\n')  # get an ok to indicate end of cat

    def insert(self, value):
        self.rv.data.insert(0, {'k': value, 'v': ''})

    def new_switch(self):

# Path: multi_input_box.py
# class MultiInputBox(Popup):
#     ok_text = StringProperty('OK')
#     cancel_text = StringProperty('Cancel')
# 
#     def __init__(self, **kwargs):
#         self.window = Window
#         super(MultiInputBox, self).__init__(**kwargs)
#         self.content = self.ids["content"]
#         self.contentButtons = self.ids["contentButtons"]
#         self.wl = []
# 
#     def _dismiss(self):
#         self.dismiss()
# 
#     def open(self):
#         super(MultiInputBox, self).open()
# 
#     def _ok(self):
#         if self.optionCallBack is not None:
#             opts = {}
#             for x in self.wl:
#                 opts[x[0]] = x[1].text
#             self.optionCallBack(opts)
#         self.dismiss()
# 
#     def on_open(self):
#         if self.wl:
#             self.wl[0][1].focus = True
# 
#     def setOptions(self, options, callBack):
#         self.optionCallBack = callBack
#         self.contentButtons.clear_widgets()
#         self.wl = []
#         for name in options:
#             self.contentButtons.add_widget(Label(text=name, size_hint_y=None, height='30dp', halign='right'))
#             tw = TextInput(multiline=False, write_tab=False, use_bubble=False, use_handles=False)
#             self.contentButtons.add_widget(tw)
#             self.wl.append((name, tw))


# Path: config_editor.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import mainthread
from multi_input_box import MultiInputBox


class ConfigEditor(Screen):

    @mainthread
    def _add_line(self, line):
        if not line.lstrip().startswith("#"):
            t = line.split()
            if len(t) >= 2:
                self.rv.data.append({'k': t[0], 'v': t[1]})
            elif t[0] == 'ok':
                # add dummy lines at end so we can edit the last few lines without keyboard covering them
                for i in range(10):
                    self.rv.data.append({'k': '', 'v': ''})

                self.app.comms.redirect_incoming(None)

    def open(self):
        self.rv.data = []
        self.manager.current = 'config_editor'
        self.app = App.get_running_app()
        # get config, parse and populate
        self.app.comms.redirect_incoming(self._add_line)
        # issue command
        self.app.comms.write('cat /sd/config\n')
        self.app.comms.write('\n')  # get an ok to indicate end of cat

    def insert(self, value):
        self.rv.data.insert(0, {'k': value, 'v': ''})

    def new_switch(self):

        o = MultiInputBox(title='Add Switch')