import base64
import logging

import msgpack

logger = logging.getLogger(__name__)

loadargs = {'use_list': False, 'raw': False}
if msgpack.version < (1, 0, 0):
    loadargs['encoding'] = 'utf-8'
else:
    loadargs['strict_map_key'] = False

VSIG = b'MSGVIV'.ljust(8, b'\x00')


def vivEventsAppendFile(filename, events):
    with open(filename, 'ab') as f:
        for event in events:
            if event[0] == 20:
                mape = base64.b64encode(event[1][3])
                event = (event[0], (event[1][0], event[1][1], event[1][2], mape))
            msgpack.pack(event, f, use_bin_type=False)


def saveWorkspaceChanges(vw, filename):
    events = vw.exportWorkspaceChanges()
    vivEventsAppendFile(filename, events)


def vivEventsToFile(filename, events):
    with open(filename, 'wb') as f:
        msgpack.pack(VSIG, f, use_bin_type=False)
        for event in events:
            if event[0] == 20:
                mape = base64.b64encode(event[1][3])
                event = (event[0], (event[1][0], event[1][1], event[1][2], mape))
            msgpack.pack(event, f, use_bin_type=False)


def saveWorkspace(vw, filename):
    events = vw.exportWorkspace()
    vivEventsToFile(filename, events)


def vivEventsFromFile(filename):
    events = []
    with open(filename, 'rb') as f:
        unpacker = msgpack.Unpacker(f, **loadargs)
        siggy = next(unpacker)
        if siggy.encode('utf-8') != VSIG:
            logger.warning('Invalid file signature of %s', str(siggy))
            return
        for event in unpacker:
            if event[0] == 20:
                mape = base64.b64decode(event[1][3])
                event = (event[0], (event[1][0], event[1][1], event[1][2], mape))
            events.append(event)
    return events


def loadWorkspace(vw, filename):
    events = vivEventsFromFile(filename)
    vw.importWorkspace(events)

[((6, 9, 6, 36), 'logging.getLogger', 'logging.getLogger', ({(6, 27, 6, 35): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((33, 8, 33, 49), 'msgpack.pack', 'msgpack.pack', (), '', False, 'import msgpack\n'), ((49, 19, 49, 50), 'msgpack.Unpacker', 'msgpack.Unpacker', ({(49, 36, 49, 37): 'f'}, {}), '(f, **loadargs)', False, 'import msgpack\n'), ((23, 12, 23, 54), 'msgpack.pack', 'msgpack.pack', (), '', False, 'import msgpack\n'), ((38, 12, 38, 54), 'msgpack.pack', 'msgpack.pack', (), '', False, 'import msgpack\n'), ((21, 23, 21, 52), 'base64.b64encode', 'base64.b64encode', ({(21, 40, 21, 51): 'event[1][3]'}, {}), '(event[1][3])', False, 'import base64\n'), ((36, 23, 36, 52), 'base64.b64encode', 'base64.b64encode', ({(36, 40, 36, 51): 'event[1][3]'}, {}), '(event[1][3])', False, 'import base64\n'), ((56, 23, 56, 52), 'base64.b64decode', 'base64.b64decode', ({(56, 40, 56, 51): 'event[1][3]'}, {}), '(event[1][3])', False, 'import base64\n')]