""" Helper view for Thonny developers
"""

from thonny.tktextext import TextFrame
from thonny.globals import get_workbench

class EventsView(TextFrame):
    def __init__(self, master):
        TextFrame.__init__(self, master)
        #self.text.config(wrap=tk.WORD)
        get_workbench().bind("ShowView", self._log_event, True)
        get_workbench().bind("HideView", self._log_event, True)
        get_workbench().bind("ToplevelResult", self._log_event, True)
        get_workbench().bind("DebuggerProgress", self._log_event, True)
        get_workbench().bind("ProgramOutput", self._log_event, True)
        get_workbench().bind("InputRequest", self._log_event, True)
    
    
    def _log_event(self, event):
        self.text.insert("end", event.sequence + "\n")
        for name in dir(event):
            if name not in ["sequence", "setdefault", "update"] and not name.startswith("_"):
                self.text.insert("end", "    " + name + ": " + repr(getattr(event, name))[:100] + "\n")
        
        if event.sequence == "DebuggerProgress":
            frame = event.stack[-1]
            self.text.insert("end", "    " + "event" + ": " + frame.last_event + "\n") 
            self.text.insert("end", "    " + "focus" + ": " + str(frame.last_event_focus) + "\n") 
            self.text.insert("end", "    " + "args" + ": " + str(frame.last_event_args) + "\n") 

        self.text.see("end")

def load_plugin():
    if get_workbench().get_option("debug_mode"):
        get_workbench().add_view(EventsView, "Events", "se")