from hazelcast.serialization.bits import *
from hazelcast.protocol.builtin import FixSizedTypesCodec
from hazelcast.protocol.client_message import OutboundMessage, REQUEST_HEADER_SIZE, create_initial_buffer, RESPONSE_HEADER_SIZE
from hazelcast.protocol.builtin import StringCodec
from hazelcast.protocol.builtin import DataCodec
from hazelcast.protocol.codec.custom.simple_entry_view_codec import SimpleEntryViewCodec
from hazelcast.protocol.builtin import CodecUtil

# hex: 0x011D00
_REQUEST_MESSAGE_TYPE = 72960
# hex: 0x011D01
_RESPONSE_MESSAGE_TYPE = 72961

_REQUEST_THREAD_ID_OFFSET = REQUEST_HEADER_SIZE
_REQUEST_INITIAL_FRAME_SIZE = _REQUEST_THREAD_ID_OFFSET + LONG_SIZE_IN_BYTES
_RESPONSE_MAX_IDLE_OFFSET = RESPONSE_HEADER_SIZE


def encode_request(name, key, thread_id):
    buf = create_initial_buffer(_REQUEST_INITIAL_FRAME_SIZE, _REQUEST_MESSAGE_TYPE)
    FixSizedTypesCodec.encode_long(buf, _REQUEST_THREAD_ID_OFFSET, thread_id)
    StringCodec.encode(buf, name)
    DataCodec.encode(buf, key, True)
    return OutboundMessage(buf, True)


def decode_response(msg):
    initial_frame = msg.next_frame()
    response = dict()
    response["max_idle"] = FixSizedTypesCodec.decode_long(initial_frame.buf, _RESPONSE_MAX_IDLE_OFFSET)
    response["response"] = CodecUtil.decode_nullable(msg, SimpleEntryViewCodec.decode)
    return response
