from xml.etree import cElementTree as Etree

from model.log import LogEntry
from _EventProcessor import EventProcessor


def _get_log_entry_from_pos_event(data, event_type):
    event = Etree.fromstring(data)
    event_data = event[0] if "Event" in event.tag else event

    return LogEntry(
        pos_id=event_data.get("posId", event_data.get("posid")) or -1,
        order_id=event_data.get("orderId"),
        business_period=event_data.get("businessPeriod", event_data.get("period")),
        event_type=event_type,
        session_id=event_data.get("sessionId", event_data.get("sessionid")),
        event_data=Etree.tostring(event_data)
    )


class PosEventProcessor(EventProcessor):
    def parse_data(self, data, event_type):
        # type: (str, str) -> LogEntry

        return _get_log_entry_from_pos_event(data, event_type)
