from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver

import json

class SkeletonReceiver(LineReceiver):
  def __init__(self, model):
    self.model      = model
    self.lastLine   = ''
    self.midMessage = False
    self.messageLen = 0

  def connectionMade(self):
    print 'Skeleton connected'

  def connectionLost(self, reason):
    print 'Skeleton disconnected'

  def startSkeletonFrame(self, line):
    length  = int(line.split('L')[1])
    message = line.split('L')[2]

    # Got more to come. Hold on.
    if len(message) != length:
      self.midMessage = True
      self.messageLen = length
      self.lastLine   = message
    # It was a full message. Keep going.
    else:
      data = json.loads(message)
      self.model.setAll(data)

  def continueSkeletonFrame(self, line):
    parts = line.split('L')

    # Just an added chunk of an existing message. Hold on.
    if len(parts) is 1:
      self.lastLine = self.lastLine + line

    elif len(parts) is 3:
      message         = self.lastLine + parts[0]
      self.midMessage = False
      self.lastLine   = ''

      self.startSkeletonFrame('L' + parts[1] + 'L' + parts[2])

      data = json.loads(message)
      self.model.setAll(data)

  def dataReceived(self, line):
    # Apparently all that skeleton data is too much for one message
    if not self.midMessage:
      self.startSkeletonFrame(line.strip())
    else:
      self.continueSkeletonFrame(line.strip())

  def sendMessage(self, message):
    self.transport.write(message + '\n')

class SkeletonSocketFactory(Factory):
  def __init__(self, model):
    self.model = model

  def buildProtocol(self, addr):
    return SkeletonReceiver(self.model)

class SpeechReceiver(LineReceiver):
  def __init__(self, model):
    self.model = model

  def connectionMade(self):
    print 'Speech connected'

  def connectionLost(self, reason):
    print 'Speech disconnected'

  def dataReceived(self, line):
    data = json.loads(line.strip())
    self.model.addCommand(str(data['command']))

  def sendMessage(self, message):
    self.transport.write(message + '\n')

class SpeechSocketFactory(Factory):
  def __init__(self, model):
    self.model = model

  def buildProtocol(self, addr):
    return SpeechReceiver(self.model)

class HandsReceiver(LineReceiver):
  def __init__(self, model):
    self.model = model

  def connectionMade(self):
    print 'Hands connected'

  def connectionLost(self, reason):
    print 'Hands disconnected'

  def dataReceived(self, line):
    print line

  def sendMessage(self, message):
    self.transport.write(message + '\n')

class HandsSocketFactory(Factory):
  def __init__(self, model):
    self.model = model

  def buildProtocol(self, addr):
    return HandsReceiver(self.model)

class MyoReceiver(LineReceiver):
  def __init__(self, model):
    self.model = model

  def connectionMade(self):
    print 'Myo connected'

  def connectionLost(self, reason):
    print 'Myo disconnected'

  def dataReceived(self, line):
    data = json.loads(line.strip())
    self.model.setAll(data)
    print self.model.get('orientation')
    self.sendMessage("OK")

  def sendMessage(self, message):
    self.transport.write(message + '\n')

class MyoSocketFactory(Factory):
  def __init__(self, model):
    self.model   = model

  def buildProtocol(self, addr):
    return MyoReceiver(self.model)
