from bcad.binterpreter.singleton import Singleton
from logging import info, warning
import logging

logging.getLogger(__name__)



class EVEnum(object):
    main_start = "main_start"
    init_display = "init_display"

class EventProcessor(object):
    ee = EVEnum()
    event_list = []
    selected_elements = []
    selected_path = None
    selected_tool_operation = None
    left_press_start = None
    pointer_position = None
    shift_pressed = False
    ctrl_pressed = False
    started = False

    def __init__(self):
        Singleton.ee = self.ee
        Singleton.ep = self
        Singleton.should_redraw = False
        self.started = False
        self.events = {
            self.ee.main_start: [self.main_start],
            self.ee.init_display: [self.init_display],
        }

    def reset(self):
        self.selected_elements = []
        self.selected_path = None
        self.selected_tool_operation = None
        self.left_press_start = None

    def append_event_processor(self, event, proc):
        self.events[event].append(proc)

    def prepend_event_processor(self, event, proc):
        self.events[event].insert(0, proc)

    def set_event(self, event, proc_list):
        self.events[event] = proc_list

    def push_event(self, event, *args):
        self.event_list.append((event, args))

    def process(self):
        event_list = self.event_list[:]
        self.event_list = []
        for e, args in event_list:
            if e in self.events:
                for p in self.events[e]:
                    r = p(args)
                    if (not r):
                        break
            else:
                warning("  Unknown event:"+str(e)+" args: "+str(args))

    def main_start(self, args):
        if not self.started:
            self.started = True
            info("Main start")
            info(args)
            args = args[0]
            Singleton.root_file = args.file

            info("args: %s"%(str(args),))
            if args.output is not None:
                Singleton.scl.load_file()
        else:
            warning("Already started")

    def init_display(self, args):
        info("Init display")
        from bcad.binterpreter.scl_context import SCLDisplay
        Singleton.sd = SCLDisplay()


ee = EVEnum()
ep = EventProcessor()
