#!/usr/bin/env python

import glob
import sys
sys.path.append('gen-py')

from thrift           import Thrift
from bank             import OnlineBanking, AccountInformation, MoneyTransfer

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol  import TBinaryProtocol
from thrift.server    import TServer

import thriftpy2
from thriftpy2.rpc              import make_client
from thriftpy2.protocol.binary  import TBinaryProtocolFactory
from thriftpy2.transport.framed import TFramedTransportFactory
import thrift_connector.connection_pool as connection_pool
bank_thrift = thriftpy2.load("bank.thrift", module_name="bank_thrift")

SERVER_PORT = ('0.0.0.0', 9090)

class OnlineBankingHandler:
    def __init__(self):
        self.log = {}
        self.AccountInformationClient = connection_pool.ClientPool(
          bank_thrift.AccountInformation,
          'account-information', 9090,
          connection_class=connection_pool.ThriftPyCyClient
        )
        self.MoneyTransferClient = connection_pool.ClientPool(
          bank_thrift.MoneyTransfer,
          'money-transfer', 9090,
          connection_class=connection_pool.ThriftPyCyClient
        )

    def ping(self):
        print('ping()')

    def getBalance(self, accountNumber):
      balance = self.AccountInformationClient.getBalance(accountNumber)
      return balance

    def transferMoney(self, fromAccount, toAccount, amount):
      ack = self.MoneyTransferClient.transferMoney(fromAccount, toAccount, amount)
      return ack

if __name__ == '__main__':
    handler = OnlineBankingHandler()
    processor = OnlineBanking.Processor(handler)
    transport = TSocket.TServerSocket(host=SERVER_PORT[0], port=SERVER_PORT[1])
    tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)

    print('[' + SERVER_PORT[0] + ':' + str(SERVER_PORT[1]) + '] Starting the OnlineBankingServer...')
    server.serve()
    print('[' + SERVER_PORT[0] + ':' + str(SERVER_PORT[1]) + '] OnlineBankingServer done.')
