from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import matplotlib.pyplot as plt
import matplotlib
import datetime
from TransactionBook.model.Filter import Filter
from datetime import datetime
from kivy.uix.popup import Popup
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.uix.checkbox import CheckBox
from kivy.core.window import Window


class MultiSelectPopUp(Popup):
    pHint_x = NumericProperty(0.7)
    pHint_y = NumericProperty(0.7)
    pHint = ReferenceListProperty(pHint_x, pHint_y)

    def __init__(self, title, option_list, option_init=None, callback=None, multiselect=True, **kwargs):
        super().__init__(**kwargs)
        self.title = title
        self.callback = callback
        self.main_layout = AnchorLayout()
        if option_init is None:
            option_init = [True] * len(option_list)

        self.grid = GridLayout(cols=1)
        self.opt_boxes = []
        self.labels = []
        for i, opt in enumerate(option_list):
            box = BoxLayout(orientation='horizontal')
            check_box = CheckBox(active=option_init[i])
            if not multiselect:
                check_box.group = "Single_Select_Only_Group"
            label = Label(text=str(opt))
            self.opt_boxes.append(check_box)
            self.labels.append(label)
            box.add_widget(check_box)
            box.add_widget(label)
            self.grid.add_widget(box)
        cancel_button = Button(text="Cancel")
        cancel_button.bind(on_press=self.cancel_callback)
        ok_button = Button(text="Ok")
        ok_button.bind(on_press=self.ok_callback)
        box = BoxLayout(orientation='horizontal')
        box.add_widget(cancel_button)
        box.add_widget(ok_button)
        self.grid.add_widget(box)

        self.main_layout.add_widget(self.grid)

        self.content = self.main_layout
        self.size_hint = self.pHint
        Window.release_all_keyboards()
        self.open()

    def ok_callback(self, _):
        selection = []
        for i, check_box in enumerate(self.opt_boxes):
            if check_box.active:
                selection.append(self.labels[i].text)
        self.callback(selection)
        self.dismiss()

    def cancel_callback(self, _):
        self.dismiss()


if __name__ == "__main__":
    from kivy.base import runTouchApp

    def cb(list_of_selection):
        print(list_of_selection)
    c = MultiSelectPopUp(title="Test", option_list=["Item1", "Item2", "Item3"], callback=cb, option_init=[True, False, True])
    runTouchApp(c)
[(20, 'kivy.properties.NumericProperty', 'NumericProperty', False, 'from kivy.properties import NumericProperty, ReferenceListProperty\n'), (21, 'kivy.properties.NumericProperty', 'NumericProperty', False, 'from kivy.properties import NumericProperty, ReferenceListProperty\n'), (22, 'kivy.properties.ReferenceListProperty', 'ReferenceListProperty', False, 'from kivy.properties import NumericProperty, ReferenceListProperty\n'), (80, 'kivy.base.runTouchApp', 'runTouchApp', False, 'from kivy.base import runTouchApp\n'), (28, 'kivy.uix.anchorlayout.AnchorLayout', 'AnchorLayout', False, 'from kivy.uix.anchorlayout import AnchorLayout\n'), (32, 'kivy.uix.gridlayout.GridLayout', 'GridLayout', False, 'from kivy.uix.gridlayout import GridLayout\n'), (46, 'kivy.uix.button.Button', 'Button', False, 'from kivy.uix.button import Button\n'), (48, 'kivy.uix.button.Button', 'Button', False, 'from kivy.uix.button import Button\n'), (50, 'kivy.uix.boxlayout.BoxLayout', 'BoxLayout', False, 'from kivy.uix.boxlayout import BoxLayout\n'), (59, 'kivy.core.window.Window.release_all_keyboards', 'Window.release_all_keyboards', False, 'from kivy.core.window import Window\n'), (36, 'kivy.uix.boxlayout.BoxLayout', 'BoxLayout', False, 'from kivy.uix.boxlayout import BoxLayout\n'), (37, 'kivy.uix.checkbox.CheckBox', 'CheckBox', False, 'from kivy.uix.checkbox import CheckBox\n')]