import os
import xlrd

# List of bytes to replace (hex format)
original_bytes = [
    b'\xD4', b'\xC8', b'\xD1', b'\xC2', b'\xD3', b'\xC0', b'\xCF', b'\xD0', b'\xD8', b'\xCE',
    b'\xCB', b'\xC4', b'\xDC', b'\xD2', b'\xD9', b'\xC7', b'\xC9', b'\xCA', b'\xDB', b'\xC5',
    b'\xC3', b'\xCC', b'\xD6', b'\xD7', b'\xCD', b'\xDF', b'\x2E', b'\xDE', b'\xA8', b'\xD5',
    b'\xC1', b'\xC6', b'\xDA', b'\xDD'
]

replacement_bytes = [
    b'\x41', b'\x42', b'\x43', b'\x44', b'\x45', b'\x46', b'\x47', b'\x48', b'\x49', b'\x4A',
    b'\x4B', b'\x4C', b'\x4D', b'\x4E', b'\x4F', b'\x50', b'\x51', b'\x52', b'\x53', b'\x54',
    b'\x55', b'\x56', b'\x57', b'\x58', b'\x59', b'\x5A',  # A-Z
    b'\x3E',  # >
    b'\x2E',  # .
    b'\x40',  # @
    b'\x5B',  # [
    b'\x5C',  # \
    b'\x5D',  # ]
    b'\x5E',  # ^
    b'\x5F'   # _
]

# Additional bytes for lowercase letters
lowercase_original_bytes = [
    b'\xF4', b'\xE8', b'\xF1', b'\xE2', b'\xF3', b'\xE0', b'\xEF', b'\xF0', b'\xF8', b'\xEE',
    b'\xEB', b'\xE4', b'\xFC', b'\xF2', b'\xF9', b'\xE7', b'\xE9', b'\xEA', b'\xFB', b'\xE5',
    b'\xE3', b'\xEC', b'\xF6', b'\xF7', b'\xED', b'\xFF', b'\x2E', b'\xFE', b'\xB8', b'\xF5',
    b'\xE1', b'\xE6', b'\xFA', b'\xFD'
]

# Lowercase replacements (same as uppercase)
lowercase_replacement_bytes = [
    b'\x41', b'\x42', b'\x43', b'\x44', b'\x45', b'\x46', b'\x47', b'\x48', b'\x49', b'\x4A',
    b'\x4B', b'\x4C', b'\x4D', b'\x4E', b'\x4F', b'\x50', b'\x51', b'\x52', b'\x53', b'\x54',
    b'\x55', b'\x56', b'\x57', b'\x58', b'\x59', b'\x5A',  # A-Z
    b'\x3E',  # >
    b'\x2E',  # .
    b'\x40',  # @
    b'\x5B',  # [
    b'\x5C',  # \
    b'\x5D',  # ]
    b'\x5E',  # ^
    b'\x5F'   # _
]

# Combine all bytes
all_original_bytes = original_bytes + lowercase_original_bytes
all_replacement_bytes = replacement_bytes + lowercase_replacement_bytes

# Replacement dictionary
replace_dict = {orig: repl for orig, repl in zip(all_original_bytes, all_replacement_bytes)}

# Replace bytes in RUS strings
def replace_bytes_in_rus(rus, replace_dict):
    for orig, repl in replace_dict.items():
        rus = rus.replace(orig, repl)
    return rus

# Read Excel file
rb = xlrd.open_workbook('MM.xls', formatting_info=True)
sheet = rb.sheet_by_index(0)

for rownum in range(sheet.nrows):
    print(rownum)
    row = sheet.row_values(rownum)

    eng = row[0].encode('cp1251')
    rus = row[1].encode('cp1251')
    fname = row[2]

    print(eng + b" ->  " + rus)

    # Replace bytes in RUS before writing
    modified_rus = replace_bytes_in_rus(rus, replace_dict)
    eng = eng.replace(b"[NULL]", b"\x00")

    with open("text/" + fname, 'rb') as fo:
        text = fo.read()

    # Replace newline marker
    text = text.replace(b"\n", b"\\n")

    # Replace ENG with modified RUS
    text = text.replace(eng, modified_rus)

    text = text.replace(b"[NULL]", b"\x00")
    text = text.replace(b"\x0a", b"\x0d")

    with open("text/" + fname, 'wb') as ft:
        ft.write(text)

    with open("text/" + fname, 'rb') as fo:
        text = fo.read()

    with open("text/" + fname, 'wb') as ft:
        text = text.replace(b"\\n", b"\n")
        ft.write(text)