#-------------------------------------------------------------------------------
# Name:        Event Factory
# Purpose:     Contains factories for creating appropriate events.
#
# Author:      tetrisd
#-------------------------------------------------------------------------------

from . import event as evt
import re
import abc

LOGINOK = "LOGINOK"
LOGINBAD = "LOGINBAD"

class EventFactory():

    factory = None
    prefix = None
    
    @staticmethod
    def getInstance():
        pass

    def setMessage(self, message):
        self.message = message
        return self

    def getMessage(self):
        return self.message

    @abc.abstractclassmethod
    def makeEvent(self):
        pass

    @abc.abstractclassmethod
    def handles(self, message):
        pass
    

class LoginEventFactory(EventFactory):

    prefix = "LOG"

    @staticmethod
    def getInstance():
        if (LoginEventFactory.factory is None):
            LoginEventFactory.factory = LoginEventFactory()
        return LoginEventFactory.factory

    def makeEvent(self):
        messages = self.message.split(':')
        success = messages[0]

        if (success == LOGINOK):
            event = evt.LoginEvent(messages[1], True)
        else:
            event = evt.LoginEvent(None, False)
        return event

    def handles(self, message):
        return message.startswith(self.prefix)

class MessageEventFactory(EventFactory):

    prefix = "MS"

    @staticmethod
    def getInstance():
        if (MessageEventFactory.factory is None):
            MessageEventFactory.factory = MessageEventFactory()
        return MessageEventFactory.factory

    def makeEvent(self):
        typeM = Messages.getMessageType(self.message)
        event = None
        parsedMessage = self.message.split(":")

        if (typeM == Messages.MSG_TYPE_PRIV):
            event = evt.MessagePrivateEvent(parsedMessage[2], parsedMessage[3])
        elif (typeM == Messages.MSG_TYPE_CMD):
            f = re.findall(Messages.MSG_PRIV_CMD, self.message)
            args = f[0][4].split(" ")
            args.pop(0)
            event = evt.MessagePrivateCommandEvent(f[0][0], f[0][3].lower(), args)
        elif (typeM == Messages.MSG_TYPE_ARENA):
            event = evt.MessageArenaEvent(self.message)
        elif (typeM == Messages.MSG_TYPE_MONEY_GIVE):
            f = re.findall(Messages.MSG_ARENA_MONEY, self.message)
            event = evt.MessageArenaMoneyEvent(f[0][0], int(f[0][1]), f[0][3])
        return event

    def handles(self, message):
        return message.startswith(self.prefix)

class Messages:
    MSG_PRIV = "MSG:PRIV"
    MSG_ARENA = "MSG:ARENA"
    MSG_PRIV_CMD = "^MSG:PRIV:(.+?):( *?|)!( *?)([A-z]+)( +.*)?$"
    MSG_ARENA_MONEY = "^MSG:ARENA:Player (.+?) gave you \\$(.+?)\\.( Message: \"(.+?)\")?$"
    MSG_TYPE_UNKNOWN, MSG_TYPE_PRIV, MSG_TYPE_CMD, MSG_TYPE_ARENA, MSG_TYPE_MONEY_GIVE = range(5)

    @staticmethod
    def getMessageType(message):
        typeM = Messages.MSG_TYPE_UNKNOWN
        if (message.startswith(Messages.MSG_PRIV)):
            if (re.match(Messages.MSG_PRIV_CMD, message)):
                typeM = Messages.MSG_TYPE_CMD
            else:
                typeM = Messages.MSG_TYPE_PRIV
        elif (message.startswith(Messages.MSG_ARENA)):
            if (re.match(Messages.MSG_ARENA_MONEY, message)):
                typeM = Messages.MSG_TYPE_MONEY_GIVE
            else:
                typeM = Messages.MSG_TYPE_ARENA
        return typeM

#-------------------------------------------------------------------------------
# Name:        Event Factory
# Purpose:     Contains factories for creating appropriate events.
#
# Author:      tetrisd
#-------------------------------------------------------------------------------

from . import event as evt
import re
import abc

LOGINOK = "LOGINOK"
LOGINBAD = "LOGINBAD"

class EventFactory():

    factory = None
    prefix = None
    
    @staticmethod
    def getInstance():
        pass

    def setMessage(self, message):
        self.message = message
        return self

    def getMessage(self):
        return self.message

    @abc.abstractclassmethod
    def makeEvent(self):
        pass

    @abc.abstractclassmethod
    def handles(self, message):
        pass
    

class LoginEventFactory(EventFactory):

    prefix = "LOG"

    @staticmethod
    def getInstance():
        if (LoginEventFactory.factory is None):
            LoginEventFactory.factory = LoginEventFactory()
        return LoginEventFactory.factory

    def makeEvent(self):
        messages = self.message.split(':')
        success = messages[0]

        if (success == LOGINOK):
            event = evt.LoginEvent(messages[1], True)
        else:
            event = evt.LoginEvent(None, False)
        return event

    def handles(self, message):
        return message.startswith(self.prefix)

class MessageEventFactory(EventFactory):

    prefix = "MS"

    @staticmethod
    def getInstance():
        if (MessageEventFactory.factory is None):
            MessageEventFactory.factory = MessageEventFactory()
        return MessageEventFactory.factory

    def makeEvent(self):
        typeM = Messages.getMessageType(self.message)
        event = None
        parsedMessage = self.message.split(":")

        if (typeM == Messages.MSG_TYPE_PRIV):
            event = evt.MessagePrivateEvent(parsedMessage[2], parsedMessage[3])
        elif (typeM == Messages.MSG_TYPE_CMD):
            f = re.findall(Messages.MSG_PRIV_CMD, self.message)
            args = f[0][4].split(" ")
            args.pop(0)
            event = evt.MessagePrivateCommandEvent(f[0][0], f[0][3].lower(), args)
        elif (typeM == Messages.MSG_TYPE_ARENA):
            event = evt.MessageArenaEvent(self.message)
        elif (typeM == Messages.MSG_TYPE_MONEY_GIVE):
            f = re.findall(Messages.MSG_ARENA_MONEY, self.message)
            event = evt.MessageArenaMoneyEvent(f[0][0], int(f[0][1]), f[0][3])
        return event

    def handles(self, message):
        return message.startswith(self.prefix)

class Messages:
    MSG_PRIV = "MSG:PRIV"
    MSG_ARENA = "MSG:ARENA"
    MSG_PRIV_CMD = "^MSG:PRIV:(.+?):( *?|)!( *?)([A-z]+)( +.*)?$"
    MSG_ARENA_MONEY = "^MSG:ARENA:Player (.+?) gave you \\$(.+?)\\.( Message: \"(.+?)\")?$"
    MSG_TYPE_UNKNOWN, MSG_TYPE_PRIV, MSG_TYPE_CMD, MSG_TYPE_ARENA, MSG_TYPE_MONEY_GIVE = range(5)

    @staticmethod
    def getMessageType(message):
        typeM = Messages.MSG_TYPE_UNKNOWN
        if (message.startswith(Messages.MSG_PRIV)):
            if (re.match(Messages.MSG_PRIV_CMD, message)):
                typeM = Messages.MSG_TYPE_CMD
            else:
                typeM = Messages.MSG_TYPE_PRIV
        elif (message.startswith(Messages.MSG_ARENA)):
            if (re.match(Messages.MSG_ARENA_MONEY, message)):
                typeM = Messages.MSG_TYPE_MONEY_GIVE
            else:
                typeM = Messages.MSG_TYPE_ARENA
        return typeM

#------------------------------------------------------------------------------- # Name:        Event Factory # Purpose:     Contains factories for creating appropriate events. # # Author:      tetrisd #-------------------------------------------------------------------------------