from ahc.Ahc import ComponentModel, Event, framers, EventTypes
from enum import Enum
import queue


class GenericMacEventTypes(Enum):
    HANDLEMACFRAME = "handlemacframe"


class GenericMac(ComponentModel):

    def __init__(self, componentname, componentinstancenumber):
        super().__init__(componentname, componentinstancenumber)
        self.framequeue = queue.Queue()
        self.ahcuhd = framers.get_ahcuhd_by_id(self.componentinstancenumber)
        print("I am Generic MAC my uhd instance id is ", self.ahcuhd.componentinstancenumber)
        self.eventhandlers[GenericMacEventTypes.HANDLEMACFRAME] = self.on_handlemacframe
    
    def on_init(self, eventobj: Event):
        self.send_self(Event(self, GenericMacEventTypes.HANDLEMACFRAME, None))  # Continuously trigger handle_frame
        # print("Initialized", self.componentname, ":", self.componentinstancenumber)

    def on_handlemacframe(self, eventobj: Event): 
        # print("will handle frame from on_handlemacframe")     
        self.handle_frame()
        # self.send_self(Event(self, GenericMacEventTypes.HANDLEMACFRAME, None)) #Continuously trigger handle_frame
        # HANDLEMACFRAME event will be generated by the inheriting component to facilitate delay
    
    def on_message_from_bottom(self, eventobj: Event):
        # print(f"I am {self.componentname}, eventcontent={eventobj.eventcontent}\n")
        evt = Event(self, EventTypes.MFRB, eventobj.eventcontent)
        self.send_up(evt)
    
    def on_message_from_top(self, eventobj: Event):
        # print(f"I am {self.componentname}, eventcontent={eventobj.eventcontent}\n")
        # put message in queue and try accessing the channel
        self.framequeue.put_nowait(eventobj)
        #print("Mac put the frame in queueu", eventobj.eventcontent.payload)
