from datetime import datetime
from xml.etree import cElementTree as Etree

from interactor.event import EventInteractor
from model.log import LogEntry
from processor.event import EventProcessor


def _get_log_entry_from_pos_event(
        data,           # type: str
        event_type,     # type: str
):
    event = Etree.fromstring(data)
    event_data = event[0] if "Event" in event.tag else event
    created_at = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

    return LogEntry(
            pos_id=event_data.get("posId", event_data.get("posid")) or -1,
            business_period=event_data.get("businessPeriod", datetime.utcnow().strftime("%Y%m%d")),
            event_type=event_type,
            created_at_gmt=created_at,
            event_data=data
    )


class GenericEventProcessor(
        EventProcessor  # type: EventInteractor
):

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

        return _get_log_entry_from_pos_event(data, event_type)
