import re

from smartcard.util import toHexString


def checkStatusWords(sw1, sw2):
    # SW1, SW2 incomming as decimal. Key is string representation in HEX
    key = toHexString([sw1, sw2])

    # Description of Status Words
    status_words = {'90 00': 'Normal processing',
                    '62 81': 'Warning processing. Part of returned data may be corrupted',
                    '63 00': 'Authentication failed (Warning)',
                    '64 00': 'Execution error — State of non-volatile memory unchanged. Integrity error',
                    '65 00': 'Execution error — State of non-volatile memory changed',
                    '65 81': 'Execution error — State of non-volatile memory changed — Memory failure',
                    '66 88': 'Security error: wrong cryptographic checksum (during Secure Messaging) '
                             'or wrong certificate (during certificate verification) '
                             'or wrong cryptogram (during external authentication) '
                             'or wrong signature (during signature verification)',
                    '67 00': 'Wrong length (wrong Lc or Le)',
                    '68 82': 'Secure messaging not supported',
                    '68 83': 'Last command of the chain expected',
                    '69 00': 'Forbidden command (no response available in T=0)',
                    '69 82': 'Security status not satisfied.',
                    '69 83': 'Authentication method blocked.',
                    '69 85': 'Conditions of use not satisfied.',
                    '69 86': 'Command not allowed (no current EF).',
                    '69 87': 'Expected Secure Messaging Data Objects missing',
                    '69 88': 'Incorrect Secure Messaging Data Objects',
                    '6A 80': 'Incorrect parameters in data field',
                    '6A 82': 'File not found.',
                    '6A 86': 'Wrong parameters P1-P2.',
                    '6A 88': 'Referenced data not found.',
                    '6B 00': 'Wrong parameters (offset outside the EF).',
                    '6D 00': 'Instruction code not supported or invalid.',
                    '6E 00': 'Class not supported.',
                    '6F 00': 'Other checking errors'}

    try:
        value = status_words[key]

    except KeyError:
        # Dynamic Status Word
        if '61 ' in key:
            # 61 xx
            value = 'Normal processing. XX = number of response bytes available.'
        elif '63 C' in key:
            # 63 Cx
            value = 'Wrong CHV (PIN). Remaining attempts counter provided by ‘X’.'
        elif '6C ' in key:
            # 6C xx
            value = 'Wrong length, SW2 indicates the exact length. No data field is returned.'
        else:
            value = 'Unknown error'

    return value

print(checkStatusWords(144, 00))