#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Ia letter_ru.html (rusesc) si injecteaza un chunk de Unicode Tags dupa FIECARE cuvant.
Rezultat: letter_ru_words.html
"""

import os

INPUT  = "letter_ru.html"          # letterul rusesc original
OUTPUT = "letter_ru_words.html"    # iese cu Tags dupa fiecare cuvant

TAG = 0xE0000

# Textul care se va imprastia cuvant cu cuvant
HIDDEN = "ATENTIE-STUDENTI-TEST-PHISHING-EDUCATIONAL-AI-GASIT-CODURI-ASCUNSE-BRAVO"


def to_tags(text: str) -> str:
    out = []
    for ch in text:
        c = ord(ch)
        if 0x20 <= c <= 0x7E:
            out.append(chr(TAG + c))
    return "".join(out)


def inject_after_words(html: str, hidden: str) -> str:
    chunks = [hidden[i:i+6] for i in range(0, len(hidden), 6)]
    chunk_idx = 0
    result = []
    i = 0

    while i < len(html):
        ch = html[i]

        # skip HTML tags
        if ch == '<':
            end = html.find('>', i)
            if end == -1:
                result.append(html[i:])
                break
            result.append(html[i:end+1])
            i = end + 1
            continue

        # un cuvant = litera/cifra ASCII urmata de altele
        if ch.isalnum() and ord(ch) < 128:
            j = i
            while j < len(html) and html[j].isalnum() and ord(html[j]) < 128:
                j += 1
            result.append(html[i:j])
            result.append(to_tags(chunks[chunk_idx % len(chunks)]))
            chunk_idx += 1
            i = j
            continue

        result.append(ch)
        i += 1

    return "".join(result)


def main():
    if not os.path.isfile(INPUT):
        print(f"[!] Nu gasesc {INPUT}. Pune letterul rusesc acolo.")
        return

    with open(INPUT, "r", encoding="utf-8") as f:
        html = f.read()

    result = inject_after_words(html, HIDDEN)

    with open(OUTPUT, "w", encoding="utf-8") as f:
        f.write(result)

    total = sum(1 for ch in result if TAG <= ord(ch) <= TAG + 0x7F)
    print(f"[✓] Scris: {OUTPUT}")
    print(f"[✓] Tags injectate: {total}")
    print(f"[✓] Bytes: {os.path.getsize(OUTPUT)}")


if __name__ == "__main__":
    main()
