# Path: pyledger/client/repl.py
# def disconnect(*args, protocol=None):
# def contracts(*args, protocol=None):
# def api(*args, protocol=None):
# def broadcast(*args, protocol=None):
# def call(*args, protocol=None):
# def general_parser(line, protocol=None, instruction_dict=None,
#                    user_instruction_dict=None):
# 
# Path: pyledger/client/lib.py
# def handle_response(bin_response, callback=None):
#     response = PyledgerResponse()
#     response.ParseFromString(bin_response)
# 
#     if response.successful:
#         if callback:
#             response_data = pickle.loads(response.data)
#             print('Executing callback...')
#             callback(response_data)
#             return True, response_data
#         else:
#             return True, pickle.loads(response.data)
# 
#     else:
#         return False, response.data.decode('utf-8')


from autobahn.asyncio.websocket import WebSocketClientProtocol, \
    WebSocketClientFactory
from asyncio.streams import StreamWriter, FlowControlMixin
from pyledger.client.repl import parse
from pyledger.client.lib import handle_response
from uuid import uuid4
from pprint import pprint
import os
import sys
import asyncio

async def stdio(loop=None):
    if loop is None:
        loop = asyncio.get_event_loop()

    reader = asyncio.StreamReader()
    reader_protocol = asyncio.StreamReaderProtocol(reader)

    writer_transport, writer_protocol = await loop.connect_write_pipe(
        FlowControlMixin, os.fdopen(0, 'wb'))
    writer = StreamWriter(writer_transport, writer_protocol, None, loop)

    await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin)

    return reader, writer


async def async_input(message, protocol):
    if isinstance(message, str):
        message = message.encode('utf8')

    global reader, writer
    if (reader, writer) == (None, None):
        reader, writer = await stdio()

    writer.write(message)
    await writer.drain()

    line = await reader.readline()
    # This is where everything happens in the client side

# Path: pyledger/client/repl.py
# def disconnect(*args, protocol=None):
# def contracts(*args, protocol=None):
# def api(*args, protocol=None):
# def broadcast(*args, protocol=None):
# def call(*args, protocol=None):
# def general_parser(line, protocol=None, instruction_dict=None,
#                    user_instruction_dict=None):
# 
# Path: pyledger/client/lib.py
# def handle_response(bin_response, callback=None):
#     response = PyledgerResponse()
#     response.ParseFromString(bin_response)
# 
#     if response.successful:
#         if callback:
#             response_data = pickle.loads(response.data)
#             print('Executing callback...')
#             callback(response_data)
#             return True, response_data
#         else:
#             return True, pickle.loads(response.data)
# 
#     else:
#         return False, response.data.decode('utf-8')


# Path: pyledger/client/ws.py
from autobahn.asyncio.websocket import WebSocketClientProtocol, \
    WebSocketClientFactory
from asyncio.streams import StreamWriter, FlowControlMixin
from pyledger.client.repl import parse
from pyledger.client.lib import handle_response
from uuid import uuid4
from pprint import pprint
import os
import sys
import asyncio


async def stdio(loop=None):
    if loop is None:
        loop = asyncio.get_event_loop()

    reader = asyncio.StreamReader()
    reader_protocol = asyncio.StreamReaderProtocol(reader)

    writer_transport, writer_protocol = await loop.connect_write_pipe(
        FlowControlMixin, os.fdopen(0, 'wb'))
    writer = StreamWriter(writer_transport, writer_protocol, None, loop)

    await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin)

    return reader, writer


async def async_input(message, protocol):
    if isinstance(message, str):
        message = message.encode('utf8')

    global reader, writer
    if (reader, writer) == (None, None):
        reader, writer = await stdio()

    writer.write(message)
    await writer.drain()

    line = await reader.readline()
    # This is where everything happens in the client side

    return parse(line, protocol=protocol)