import codecs
from hashlib import sha256 as hasher

from tvm_valuetypes.cell import deserialize_boc, Cell


def seqno_extractor(result, data):
    data_cell = deserialize_boc(codecs.decode(codecs.encode(data["data"], 'utf-8'), 'base64'))
    seqno = int.from_bytes(data_cell.data.data[0:32].tobytes(), 'big')
    result['seqno'] = seqno


def v3_extractor(result, data):
    seqno_extractor(result, data)
    try:
        data_cell = deserialize_boc(codecs.decode(codecs.encode(data["data"], 'utf-8'), 'base64'))
        wallet_id = int.from_bytes(data_cell.data.data[32:64].tobytes(), 'big')
        result['wallet_id'] = wallet_id
    except Exception as e:
        print('Extracting wallet_id failed:', str(e))


def v3_builder(wallet_id: int, public_key: bytes):
    cell = Cell()
    cell.data.put_arbitrary_uint(0, 32)  # seqno
    cell.data.put_arbitrary_uint(int(wallet_id), 32)
    cell.data.put_arbitrary_uint(int.from_bytes(public_key, byteorder='big'), 256)
    return cell


def v4_builder(wallet_id: int, public_key: bytes):
    cell = v3_builder(wallet_id, public_key)
    cell.data.put_arbitrary_uint(0, 1)  # empty plugins dict
    return cell


def sha256(x):
    if not isinstance(x, bytes):
        x = codecs.encode(x, 'utf-8')
    h = hasher()
    h.update(x)
    return h.digest()


sources = {
    # Wallets
    'v1r1': "te6cckEBAQEARAAAhP8AIN2k8mCBAgDXGCDXCx/tRNDTH9P/0VESuvKhIvkBVBBE+RDyovgAAdMfMSDXSpbTB9QC+wDe0aTIyx/L/8ntVEH98Ik=",
    'v1r2': "te6cckEBAQEAUwAAov8AIN0gggFMl7qXMO1E0NcLH+Ck8mCBAgDXGCDXCx/tRNDTH9P/0VESuvKhIvkBVBBE+RDyovgAAdMfMSDXSpbTB9QC+wDe0aTIyx/L/8ntVNDieG8=",
    'v1r3': "te6cckEBAQEAXwAAuv8AIN0gggFMl7ohggEznLqxnHGw7UTQ0x/XC//jBOCk8mCBAgDXGCDXCx/tRNDTH9P/0VESuvKhIvkBVBBE+RDyovgAAdMfMSDXSpbTB9QC+wDe0aTIyx/L/8ntVLW4bkI=",
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YBA+wAGAIpQBIEBCPRZMO1E0IEBQNcgyAHPFvQAye1UAXKwjiOCEGRzdHKDHrFwgBhQBcsFUAPPFiP6AhPLassfyz/JgED7AJJfA+ICASAKCwBZvSQrb2omhAgKBrkPoCGEcNQICEekk30pkQzmkD6f+YN4EoAbeBAUiYcVnzGEAgFYDA0AEbjJftRNDXCx+AA9sp37UTQgQFA1yH0BDACyMoHy//J0AGBAQj0Cm+hMYAIBIA4PABmtznaiaEAga5Drhf/AABmvHfaiaEAQa5DrhY/AAG7SB/oA1NQi+QAFyMoHFcv/ydB3dIAYyMsFywIizxZQBfoCFMtrEszMyXP7AMhAFIEBCPRR8qcCAHCBAQjXGPoA0z/IVCBHgQEI9FHyp4IQbm90ZXB0gBjIywXLAlAGzxZQBPoCFMtqEssfyz/Jc/sAAgBsgQEI1xj6ANM/MFIkgQEI9Fnyp4IQZHN0cnB0gBjIywXLAlAFzxZQA/oCE8tqyx8Syz/Jc/sAAAr0AMntVGliJeU="
}

contracts = {
    sha256(sources[name]): {
        'type': name,
        'data_extractor': v3_extractor if name.find('v') == 0 else None,
        'data_builder': {
            'v1': None,
            'v2': None,
            'v3': v3_builder,
            'v4': v4_builder
        }[name[:2]],
    } for name in sources
}