#
# M E S S A G E   T O K E N S
#
# TOKEN FORMAT (32 bits): 0xPGGXNNNN
#    P  - message priority
#    G  - message group
#    X  - not used
#    N  - message number
#

MSGPRT_HIGH = "F"
MSGPRT_MEDHIG = "A"
MSGPRT_MEDLOW = "5"
MSGPRT_LOW = "0"


def create_token(priority, message_grourp, number):
    # type: (str, str, str) -> int
    while len(number) < 4:
        number = "0" + number

    while len(message_grourp) < 2:
        message_grourp = "0" + message_grourp

    return int("0x" + priority + message_grourp + "0" + number, 16)


def get_message_number(token):
    # type: (int) -> int
    return token & 0xFFFF


def get_token_hexa_string(token):
    # type: (int) -> str
    return "0x" + format(token, 'x')


# SAT Module
TK_SAT_PROCESS_REQUEST = create_token(MSGPRT_HIGH, "30", "1")
TK_SAT_STATUS_REQUEST = create_token(MSGPRT_LOW, "30", "2")
TK_SAT_OPERATIONAL_STATUS_REQUEST = create_token(MSGPRT_LOW, "30", "3")
TK_SAT_CANCEL_SALE = create_token(MSGPRT_LOW, "30", "4")

# FiscalWrapper Module
TK_FISCALWRAPPER_PROCESS_REQUEST = create_token(MSGPRT_HIGH, "31", "1")
TK_FISCALWRAPPER_SEARCH_REQUEST = create_token(MSGPRT_LOW, "31", "2")
TK_FISCALWRAPPER_REPRINT = create_token(MSGPRT_HIGH, "31", "3")
TK_FISCALWRAPPER_SEFAZ_CONNECTIVITY = create_token(MSGPRT_HIGH, "31", "4")
TK_FISCALWRAPPER_SAT_OPERATIONAL_STATUS_REQUEST = create_token(MSGPRT_HIGH, "31", "5")
TK_FISCALWRAPPER_EXPORT_FISCAL_FILES = create_token(MSGPRT_LOW, "31", "6")
TK_FISCALWRAPPER_GET_NF_TYPE = create_token(MSGPRT_LOW, "31", "7")
TK_FISCALWRAPPER_SAT_PROCESS_PAYMENT = create_token(MSGPRT_LOW, "31", "8")
TK_FISCALWRAPPER_SAT_PAYMENT_STATUS = create_token(MSGPRT_LOW, "31", "9")
TK_FISCALWRAPPER_CANCEL_ORDER = create_token(MSGPRT_LOW, "31", "10")
TK_SAT_PROCESS_PAYMENT = create_token(MSGPRT_LOW, "31", "11")
TK_SAT_PAYMENT_STATUS = create_token(MSGPRT_LOW, "31", "12")
TK_SAT_CANCEL_ORDER = create_token(MSGPRT_LOW, "31", "13")
TK_FISCALWRAPPER_RE_SIGN_XML = create_token(MSGPRT_LOW, "31", "14")
TK_FISCALWRAPPER_SITUATION = create_token(MSGPRT_LOW, "31", "15")  # 51380245
TK_FISCALWRAPPER_GET_CST = create_token(MSGPRT_LOW, "31", "16")
TK_FISCALWRAPPER_GET_BASE_REDUCTION = create_token(MSGPRT_LOW, "31", "17")
TK_FISCALWRAPPER_GET_FISCAL_XML = create_token(MSGPRT_LOW, "31", "18")
TK_FISCALWRAPPER_GET_IBPT_TAX = create_token(MSGPRT_LOW, "31", "19")
TK_FISCALWRAPPER_GET_ORDERS_DATA = create_token(MSGPRT_LOW, "31", "20")  # 51380246
TK_FISCALWRAPPER_GET_LAST_ORDER_ID = create_token(MSGPRT_LOW, "31", "21")  # 51380247
TK_FISCALWRAPPER_PRINT_DONATION_COUPON = create_token(MSGPRT_LOW, "31", "22")
TK_FISCALWRAPPER_GET_CONTINGENCY_STATUS = create_token(MSGPRT_LOW, "31", "23")
TK_FISCALWRAPPER_CHANGE_CONTINGENCY_STATUS = create_token(MSGPRT_LOW, "31", "24")
TK_FISCALWRAPPER_GET_CERTIFICATE_EXPIRATION_DAYS = create_token(MSGPRT_LOW, "31", "25")
TK_FISCALWRAPPER_GET_TIP = create_token(MSGPRT_LOW, "31", "31")
TK_FISCALWRAPPER_SAT_STORE_STATUS = create_token(
    priority=MSGPRT_LOW,
    message_grourp="31",
    number="32",
)


# SiTEF Module
TK_SITEF_ADD_PAYMENT = create_token(MSGPRT_HIGH, "32", "1")
TK_SITEF_CANCEL_PAYMENT = create_token(MSGPRT_LOW, "32", "2")
TK_SITEF_FINISH_PAYMENT = create_token(MSGPRT_LOW, "32", "3")
TK_SITEF_CONNECTIVITY_TEST = create_token(MSGPRT_LOW, "32", "4")
TK_SITEF_AVAILABLE = create_token(MSGPRT_LOW, "32", "5")
TK_SITEF_CONFIRM_FINISH = create_token(MSGPRT_LOW, "32", "6")
TK_SITEF_NOT_ALL_PAYMENTS_REFUNDED = create_token(MSGPRT_LOW, "32", "1001")
TK_SITEF_REFUND_CANCELED = create_token(MSGPRT_LOW, "32", "1002")
TK_SITEF_GET_DOC = create_token(MSGPRT_LOW, "32", "7")
TK_SITEF_CANCEL_PAYMENT_ORDER = create_token(MSGPRT_LOW, "32", "8")

# FingerPrint Reader
TK_FPR_ENROLL_USER = create_token(MSGPRT_LOW, "33", "1")
TK_FPR_IDENTIFY_USER = create_token(MSGPRT_LOW, "33", "2")
TK_FPR_AUTHORIZE_USER = create_token(MSGPRT_LOW, "33", "3")
TK_FPR_OK = create_token(MSGPRT_LOW, "33", "4")
TK_FPR_RE_ENROLL_USER = create_token(MSGPRT_LOW, "33", "5")
TK_FPR_TIMEOUT = create_token(MSGPRT_LOW, "33", "6")
TK_FPR_FINGER_PRINT_ALREADY_REGISTERED = create_token(MSGPRT_LOW, "33", "7")

# KDSMonitor Module
TK_KDSMONITOR_STATUS_REQUEST = create_token(MSGPRT_LOW, "34", "1")

# Maintenance Module
TK_MAINTENANCE_TERMINATE_REQUEST = create_token(MSGPRT_LOW, "35", "1")
TK_MAINTENANCE_RESTART_REQUEST = create_token(MSGPRT_LOW, "35", "2")
TK_MAINTENANCE_RECREATE_FILES_XML = create_token(MSGPRT_LOW, "35", "3")  # token 55574531
TK_MAINTENANCE_RECREATE_FILE_NUMBER = create_token(MSGPRT_LOW, "35", "4")
TK_MAINTENANCE_CLEAN_AUDIT_LOG = create_token(MSGPRT_LOW, "35", "5")
TK_MAINTENANCE_DATABASE_CLEANUP_STATUS = create_token(MSGPRT_LOW, "35", "6")

# Chat Controller
TK_CHAT_SEND_MESSAGE = create_token(MSGPRT_LOW, "36", "1")
TK_CHAT_GET_UPDATES = create_token(MSGPRT_LOW, "36", "2")
TK_CHAT_MARK_UPDATES_RECEIVED = create_token(MSGPRT_LOW, "36", "3")
TK_CHAT_GET_LAST_MESSAGES = create_token(MSGPRT_LOW, "36", "4")
TK_CHAT_INVALID_REQUEST = create_token(MSGPRT_LOW, "36", "100")

# Remote Order
TK_REMOTE_ORDER_GET_STORED_ORDERS = create_token(MSGPRT_LOW, "37", "1")
TK_REMOTE_ORDER_SEND_ORDER_TO_PRODUCTION = create_token(MSGPRT_LOW, "37", "2")
TK_REMOTE_ORDER_GET_STORE = create_token(MSGPRT_LOW, "37", "3")
TK_REMOTE_ORDER_CLOSE_STORE = create_token(MSGPRT_LOW, "37", "4")
TK_REMOTE_ORDER_OPEN_STORE = create_token(MSGPRT_LOW, "37", "5")
TK_REMOTE_ORDER_INVALID_ORDER_ID = create_token(MSGPRT_LOW, "37", "100")
TK_REMOTE_STORE_ALREADY_OPEN = create_token(MSGPRT_LOW, "37", "101")
TK_REMOTE_STORE_ALREADY_CLOSED = create_token(MSGPRT_LOW, "37", "102")
TK_REMOTE_ORDER_REPRINT = create_token(MSGPRT_LOW, "37", "104")
TK_REMOTE_ORDER_GET_AVAILABLE_PRODUCTS = create_token(MSGPRT_LOW, "37", "201")
TK_REMOTE_ORDER_GET_UNAVAILABLE_PRODUCTS = create_token(MSGPRT_LOW, "37", "202")
TK_REMOTE_ORDER_UPDATE_AVAILABLE_PRODUCTS = create_token(MSGPRT_LOW, "37", "203")
TK_REMOTE_ORDER_ERROR = create_token(MSGPRT_LOW, "37", "204")
TK_REMOTE_ORDER_CHECK_RUPTURA_DIFF_ITEMS = create_token(MSGPRT_LOW, "37", "205")
TK_REMOTE_ORDER_VOID_REMOTE_ORDER = create_token(MSGPRT_LOW, "37", "206")
TK_REMOTE_ORDER_CREATE_AND_PRODUCE = create_token(MSGPRT_LOW, "37", "207")
TK_REMOTE_ORDER_CREATE = create_token(MSGPRT_LOW, "37", "208")
TK_REMOTE_ORDER_GET_VOIDED_ORDERS = create_token(MSGPRT_LOW, "37", "209")
TK_REMOTE_ORDER_GET_POS_ID = create_token(MSGPRT_LOW, "37", "210")
TK_REMOTE_ORDER_GET_CONFIRMED_ORDERS = create_token(MSGPRT_LOW, "37", "211")
TK_REMOTE_ORDER_GET_RUPTURED_ITEMS = create_token(MSGPRT_LOW, "37", "213")
TK_REMOTE_ORDER_GET_STORE_STATUS = create_token(MSGPRT_LOW, "37", "214")
TK_REMOTE_ORDER_CHECK_IF_ORDER_EXISTS = create_token(MSGPRT_LOW, "37", "215")
TK_REMOTE_ORDER_REPRINT_DELIVERY = create_token(MSGPRT_LOW, "37", "216")
TK_REMOTE_ORDER_GET_LOGISTIC_PARTNERS = create_token(MSGPRT_LOW, "37", "217")
TK_REMOTE_ORDER_SEARCH_LOGISTIC = create_token(MSGPRT_LOW, "37", "218")
TK_REMOTE_ORDER_SEND_ORDER_TO_LOGISTIC = create_token(MSGPRT_LOW, "37", "219")
TK_REMOTE_ORDER_CANCEL_LOGISTIC = create_token(MSGPRT_LOW, "37", "220")
TK_REMOTE_ORDER_SAVE_ORDER = create_token(MSGPRT_LOW, "37", "221")
TK_REMOTE_ORDER_GET_DELIVERY_FEE_CODE = create_token(MSGPRT_LOW, "37", "222")
TK_REMOTE_ORDER_CONFIRM_DELIVERY_PAYMENT = create_token(MSGPRT_LOW, "37", "223")
TK_REMOTE_ORDER_INFORM_ORDER_READY = create_token(MSGPRT_LOW, "37", "225")
TK_REMOTE_ORDER_GET_DELIVERY_BRANDS = create_token(MSGPRT_LOW, "37", "226")


# Daily Goals
TK_DAILY_GOALS_UPDATE_GOALS = create_token(MSGPRT_LOW, "38", "1")
TK_DAILY_GOALS_UPDATE_TOTAL_SOLD = create_token(MSGPRT_LOW, "38", "2")
TK_DAILY_GOALS_TERMINATE_REQUEST = create_token(MSGPRT_LOW, "38", "3")
TK_DAILY_GOALS_GET_SALES_CHALLENGE = create_token(MSGPRT_LOW, "38", "4")
TK_DAILY_GOALS_INSERT_SALES_CHALLENGE = create_token(MSGPRT_LOW, "38", "5")
TK_DAILY_GOALS_DELETE_SALES_CHALLENGE = create_token(MSGPRT_LOW, "38", "6")
TK_DAILY_GOALS_UPDATE_SALES_CHALLENGE = create_token(MSGPRT_LOW, "38", "7")

# Pickup Display Module
TK_PICKUPDISPLAY_ADD_READY_TO_PICK_ORDER = create_token(MSGPRT_LOW, "39", "1")
TK_PICKUPDISPLAY_ADD_PAID_ORDER = create_token(MSGPRT_LOW, "39", "2")
TK_PICKUPDISPLAY_ADD_PICKED_UP_ORDER = create_token(MSGPRT_LOW, "39", "3")
TK_PICKUPDISPLAY_UPDATE_ORDER = create_token(MSGPRT_LOW, "39", "4")
TK_PICKUPDISPLAY_SEND_CACHED_ORDERS = create_token(MSGPRT_LOW, "39", "5")

# Custom Production
TK_PROD_ENABLEVIEW = create_token(MSGPRT_LOW, "0C", "100")
TK_PROD_DISABLEVIEW = create_token(MSGPRT_LOW, "0C", "101")
TK_PROD_PURGE = create_token(MSGPRT_LOW, "0C", "103")
TK_PROD_GET_ORDER_MAX_PREP_TIME = create_token(MSGPRT_LOW, "C", "3")
TK_PROD_GET_PRINTERS_INFO = create_token(MSGPRT_LOW, "C", "201")
TK_PROD_GET_COURSES = create_token(MSGPRT_LOW, "C", "202")

TK_PROD_ORDER_PICKLIST = create_token(MSGPRT_LOW, "C", "301")

# BK Office uploader
TK_BKOFFICEUPLOADER_UPDATE_ORDERPICTURE = create_token(MSGPRT_LOW, "40", "1")
TK_BKOFFICEUPLOADER_SEND_SANGRIA = create_token(MSGPRT_LOW, "40", "2")
TK_BKOFFICEUPLOADER_SEND_BKOFFICE_ORDER = create_token(MSGPRT_LOW, "40", "3")

# Sapiens uploader
TK_SAPIENS_GENERATE_SALES = create_token(MSGPRT_LOW, "41", "1")
TK_SAPIENS_GENERATE_REDUCTIONZ = create_token(MSGPRT_LOW, "41", "2")
TK_SAPIENS_GENERATE_SALE_CENTER = create_token(MSGPRT_LOW, "41", "3")
TK_SAPIENS_UPLOAD = create_token(MSGPRT_LOW, "41", "4")
TK_SAPIENS_GENERATE_ALL_FILES_PER_PERIOD = create_token(MSGPRT_LOW, "41", "5")
TK_SAPIENS_PERFORM_BACKUP = create_token(MSGPRT_LOW, "41", "6")
TK_SAPIENS_PERFORM_FISCAL_VALIDATION = create_token(MSGPRT_LOW, "41", "7")

# Ruptura messages
TK_RUPTURA_GET_ENABLED = create_token(MSGPRT_LOW, "42", "3")
TK_RUPTURA_GET_DISABLED = create_token(MSGPRT_LOW, "42", "4")
TK_RUPTURA_GET_ENABLED_PRODUCTS = create_token(MSGPRT_LOW, "42", "5")
TK_RUPTURA_GET_DISABLED_PRODUCTS = create_token(MSGPRT_LOW, "42", "6")
TK_RUPTURA_IS_PROCESSED = create_token(MSGPRT_LOW, "42", "7")
TK_RUPTURA_UPDATE_ITEMS = create_token(MSGPRT_LOW, "42", "8")
TK_RUPTURA_CHECK_ITEMS = create_token(MSGPRT_LOW, "42", "9")
TK_RUPTURA_GET_CLEAN_TIME_WINDOW = create_token(MSGPRT_LOW, "42", "10")
TK_RUPTURA_CLEAN_WITH_CATALOG_UPDATE = create_token(MSGPRT_LOW, "42", "11")
TK_RUPTURA_GET_DAILY_DISHES = create_token(MSGPRT_LOW, "42", "12")
TK_RUPTURA_UPDATE_DAILY_DISHES = create_token(MSGPRT_LOW, "42", "13")

# Blacklist
TK_BLACKLIST_CHECK_STRING = create_token(MSGPRT_LOW, "44", "0")
TK_BLACKLIST_FILTER_STRING = create_token(MSGPRT_LOW, "44", "1")

# PafEcfListener
TK_VERIFY_PAF_ECF_LISTENER = create_token(MSGPRT_LOW, "45", "1")
TK_POS_FUNCTION_CHANGED = create_token(MSGPRT_LOW, "45", "2")

# Pos Updater
TK_POS_UPDATER_PERFORM_CATALOG_UPDATE = create_token(MSGPRT_LOW, "95", "1")
TK_POS_UPDATER_PERFORM_MEDIA_UPDATE = create_token(MSGPRT_LOW, "95", "2")
TK_POS_UPDATER_PERFORM_USER_UPDATE = create_token(MSGPRT_LOW, "95", "3")
TK_POS_GET_CATALOG_VERSION_TO_APPLY = create_token(MSGPRT_LOW, "95", "4")
TK_POS_UPDATER_PERFORM_LOADER_UPDATE = create_token(MSGPRT_LOW, "95", "5")
TK_POS_UPDATER_GET_VERSIONS = create_token(MSGPRT_LOW, "95", "6")
TK_POS_UPDATER_PERFORM_BINARIES_UPDATE = create_token(MSGPRT_LOW, "95", "7")
TK_POS_HAS_BINARY_UPDATE_TO_APPLY = create_token(MSGPRT_LOW, "95", "8")
TK_POS_UPDATER_PERFORM_CHROME_UPDATE = create_token(MSGPRT_LOW, "95", "9")

# Discount
TK_DISCOUNT_APPLY = create_token(MSGPRT_HIGH, "43", "1")
TK_DISCOUNT_CLEAR = create_token(MSGPRT_HIGH, "43", "2")

# NDiscount
TK_NDISCOUNT_ADD_BENEFIT = create_token(MSGPRT_LOW, "112", "1")
TK_NDISCOUNT_REMOVE = create_token(MSGPRT_LOW, "112", "2")
TK_NDISCOUNT_GET_BENEFITS = create_token(MSGPRT_LOW, "112", "7")
TK_NDISCOUNT_ON_ITEM_SALE = create_token(MSGPRT_LOW, "112", "8")
TK_NDISCOUNT_ON_ORDER_TOTAL = create_token(MSGPRT_LOW, "112", "9")
TK_NDISCOUNT_ON_ORDER_REOPEN = create_token(MSGPRT_LOW, "112", "10")
TK_NDISCOUNT_ON_TABLE_TOTAL = create_token(MSGPRT_LOW, "112", "11")
TK_NDISCOUNT_ON_TABLE_REOPEN = create_token(MSGPRT_LOW, "112", "12")
TK_NDISCOUNT_ON_TABLE_OPEN = create_token(MSGPRT_LOW, "112", "13")
TK_NDISCOUNT_ON_TAB_OPEN = create_token(MSGPRT_LOW, "112", "14")

# Coupon
TK_NCOUPON_APPLY_COUPON = create_token(MSGPRT_LOW, "113", "1")
TK_NCOUPON_UNLOCK_COUPON = create_token(MSGPRT_LOW, "113", "2")
TK_NCOUPON_BURN_COUPON = create_token(MSGPRT_LOW, "113", "3")
TK_NCOUPON_GET_COUPON_INFO = create_token(MSGPRT_LOW, "113", "4")
TK_NCOUPON_TEST_CONNECTION = create_token(MSGPRT_LOW, "113", "5")

# Electronic Payment
TK_ELECTRONIC_PAYMENT_ADD_PAYMENT = create_token(MSGPRT_LOW, "E", "1")
TK_ELECTRONIC_PAYMENT_CANCEL_PAYMENT = create_token(MSGPRT_LOW, "E", "2")
TK_ELECTRONIC_PAYMENT_FINISH_PAYMENT = create_token(MSGPRT_LOW, "E", "3")
TK_ELECTRONIC_PAYMENT_CONNECTIVITY_TEST = create_token(MSGPRT_LOW, "E", "4")
TK_ELECTRONIC_PAYMENT_SEND_RESPONSE = create_token(MSGPRT_LOW, "E", "5")
TK_ELECTRONIC_PAYMENT_CANCEL_PAYMENT_ORDER = create_token(MSGPRT_LOW, "E", "6")
TK_ELECTRONIC_PAYMENT_CONFIRM_FINISH = create_token(MSGPRT_LOW, "E", "7")
TK_ELECTRONIC_PAYMENT_GET_ELECTRONIC_TYPES = create_token(MSGPRT_LOW, "E", "8")
TK_ELECTRONIC_PAYMENT_GET_DOCUMENT = create_token(MSGPRT_LOW, "E", "9")
TK_ELECTRONIC_PAYMENT_NOT_ALL_PAYMENTS_REFUNDED = create_token(MSGPRT_LOW, "E", "1001")
TK_ELECTRONIC_PAYMENT_REFUND_CANCELED = create_token(MSGPRT_LOW, "E", "1002")

# Nsale
TK_SLCTRL_OMGR_UPDATEORDERTENDERDETAIL=create_token(MSGPRT_LOW, "A", "32")

# Device Manager
TK_GET_USED_SERVICE = create_token(MSGPRT_LOW, "D", "1")

# Loyalty
TK_LOYALTY_GET_LOYALTY_ID = create_token(MSGPRT_LOW, "111", "1")
TK_LOYALTY_GET_AND_LOCK_VOUCHER = create_token(MSGPRT_LOW, "111", "2")
TK_LOYALTY_BURN_VOUCHER = create_token(MSGPRT_LOW, "111", "3")
TK_LOYALTY_UNLOCK_VOUCHER = create_token(MSGPRT_LOW, "111", "4")
TK_LOYALTY_IS_AVAILABLE = create_token(MSGPRT_LOW, "111", "FF")
