import tkinter
from constants import GPA_MAPPING
import tkinter as tk
import tkinter.ttk as ttk
from pprint import pprint

# widgets.py - Implements a custom GUI widget 'ModuleWidget'
# TODO: Remove all 'pass' statements and complete the implementation based on class description

class ModuleWidget:
    """A supplier class for displaying a module and returning the GPA value.
    """
    def __init__(self, master, command):
        self.__module = None
        self.__command = command
        self.__gpa = -1

        self.setup_ui(master)
        self.set_module(None)

    def set_module(self, module = None):
        if (module != None):
            # TODO: When module is provided:
            #       - Update UI to display module information
            #       - Enable option menu

            self.__var_name.set(module.get_name())
            self.__var_credit.set(module.get_credit())
            self.__ent_name['state'] = 'readonly'
            self.__ent_credit['state'] = 'readonly'
            self.__om_grade['state'] = 'readonly'

        else:
            # - Clear entry content
            # - Disable option menu
            self.__var_name.set('')
            self.__var_credit.set('')
            self.__ent_name['state'] = 'disabled'
            self.__ent_credit['state'] = 'disabled'
            self.__om_grade['state'] = 'disabled'

        # Finally, update self.__module
        self.__module = module

    def get_module(self):
        return self.__module

    def get_frame(self):
        return self.__frame

    def get_gpa(self):
        return self.__gpa

    def setup_ui(self, master):
        # Variables

        # Contianer
        self.__frame = tkinter.Frame(master)
        # pprint(self)

        # Module name
        tkinter.Label(self.__frame, text = "Module", ).grid(row=0, column=0, padx=(0, 4), sticky=tkinter.W)
        self.__var_name = tkinter.StringVar(value="")
        self.__ent_name = ttk.Entry(self.__frame, textvariable=self.__var_name)
        self.__ent_name.config(width=50)
        self.__ent_name.grid(row=0, column=1, padx=(4, 4),  sticky=tkinter.E+tkinter.W)

        # Module credit
        tkinter.Label(self.__frame, text = "Credit", ).grid(row=0, column=2, padx=(0, 4), sticky=tkinter.W)
        self.__var_credit = tkinter.StringVar(value="")
        self.__ent_credit = ttk.Entry(self.__frame, textvariable=self.__var_credit)
        self.__ent_credit['state'] = 'disabled'
        self.__ent_credit.config(width=5)
        self.__ent_credit.grid(row=0, column=3, padx=(4, 4),  sticky=tkinter.E+tkinter.W)

        # Grade
        tkinter.Label(self.__frame, text = "Grade", ).grid(row=0, column=4, padx=(0, 4), sticky=tkinter.W)
        self.__var_grade = tk.StringVar()
        self.__om_grade = ttk.Combobox(self.__frame, textvariable=self.__var_grade)
        self.__om_grade.set('Please select')
        self.__om_grade['values'] = [gpa for gpa in GPA_MAPPING.keys()]
        self.__om_grade['state'] = 'disabled'
        self.__om_grade.config(width=15)
        self.__om_grade.grid(row=0, column=5, padx=(0, 4), sticky=tkinter.W)
        self.__om_grade.bind('<<ComboboxSelected>>', self.on_grade_changed)

    def on_grade_changed(self, _):
        # Get GPA based on combo box value
        letter_grade = list(GPA_MAPPING.keys())[self.__om_grade.current()]
        num_grade = GPA_MAPPING[letter_grade]
        self.__gpa = num_grade
        self.__command()
