# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
from twisted.protocols import amp
from twisted.application.service import Service
from twisted.internet.protocol import ServerFactory
from twisted.internet.endpoints import serverFromString
from twisted.internet import reactor

class FetchTicket(amp.Command):
    arguments = [('id', amp.Integer())]
    response = [('id', amp.Integer()),
                ('type', amp.Unicode()),
                ('time', amp.Integer()),
                ('component', amp.Unicode()),
                ('priority', amp.Unicode()),
                ('owner', amp.Unicode()),
                ('reporter', amp.Unicode()),
                ('cc', amp.Unicode()),
                ('status', amp.Unicode()),
                ('resolution', amp.Unicode()),
                ('summary', amp.Unicode()),
                ('description', amp.Unicode()),
                ('keywords', amp.Unicode()),
                ('changes', amp.AmpList([('time', amp.Integer()),
                                         ('author', amp.Unicode()),
                                         ('field', amp.Unicode()),
                                         ('oldvalue', amp.Unicode()),
                                         ('newvalue', amp.Unicode())]))]



class AMPFace(amp.BoxDispatcher, amp.CommandLocator):

    def __init__(self, store):
        amp.BoxDispatcher.__init__(self, self)
        self.store = store


    @FetchTicket.responder
    def fetchTicket(self, id):
        d = self.store.fetchTicket(id)
        return d



class AMPFactory(ServerFactory):
    def __init__(self, store):
        self.store = store


    def buildProtocol(self, addr):
        face = AMPFace(self.store)
        a = amp.AMP(boxReceiver=face, locator=face)
        a.factory = self
        return a



class AMPService(Service):

    def __init__(self, port, store):
        self.port = port
        self.store = store


    def startService(self):
        self.endpoint = serverFromString(reactor, self.port)
        self.endpoint.listen(AMPFactory(self.store))



class JSONRPCService(Service):
    pass



class JSONRPCFace(object):
    pass

@@ -1287,35 +1287,23 @@
 ass 
-AMPFace(amp.BoxDispatcher, 
+FrackResponder(
 amp.
@@ -1319,17 +1319,16 @@
 cator):%0A
-%0A
     def 
@@ -1354,55 +1354,8 @@
 e):%0A
-        amp.BoxDispatcher.__init__(self, self)%0A
     
@@ -1636,33 +1636,89 @@
     
-face = AMPFace(self.store
+responder = FrackResponder(self.store)%0A        disp = amp.BoxDispatcher(responder
 )%0A  
@@ -1747,20 +1747,20 @@
 eceiver=
-face
+disp
 , locato
@@ -1765,12 +1765,17 @@
 tor=
-face
+responder
 )%0A  

split commands from AMP protocol bits