
"""
Teletext Character Sets (G0).
Maps the specific code points (0x23, 0x24, 0x40, 0x5B-0x5E, 0x60, 0x7B-0x7E)
to Unicode characters based on the National Option (3 bits).
"""

# Default (English) - Option 000
ENGLISH = {
    0x23: '#', 0x24: '$', 0x40: '@', 
    0x5B: '[', 0x5C: '\\', 0x5D: ']', 0x5E: '^', 
    0x5F: '_', 0x60: '-', 
    0x7B: '{', 0x7C: '|', 0x7D: '}', 0x7E: '~'
}

# Swedish/Finnish/Hungarian - Option 010 (2)
SWEDISH_FINNISH = {
    0x23: '#', 0x24: '\u00A4', 0x40: '\u00C9', 
    0x5B: '\u00C4', 0x5C: '\u00D6', 0x5D: '\u00C5', 0x5E: '\u00DC', 
    0x5F: '_', 0x60: '\u00E9', 
    0x7B: '\u00E4', 0x7C: '\u00F6', 0x7D: '\u00E5', 0x7E: '\u00FC'
}

# German - Option 001 (1) 
GERMAN = {
    0x23: '#', 0x24: '$', 0x40: '§', 
    0x5B: 'Ä', 0x5C: 'Ö', 0x5D: 'Ü', 0x5E: '^', 
    0x5F: '_', 0x60: '`', 
    0x7B: 'ä', 0x7C: 'ö', 0x7D: 'ü', 0x7E: 'ß'
}

# Italian - Option 011 (3)
ITALIAN = {
    0x23: '£', 0x24: '$', 0x40: 'é', 
    0x5B: '°', 0x5C: 'ç', 0x5D: '→', 0x5E: '↑', 
    0x5F: '#', 0x60: 'ù', 
    0x7B: 'à', 0x7C: 'ò', 0x7D: 'è', 0x7E: 'ì'
}

# French - Option 100 (4)
FRENCH = {
    0x23: 'é', 0x24: 'ï', 0x40: 'à', 
    0x5B: 'ë', 0x5C: 'ê', 0x5D: 'ù', 0x5E: 'î', 
    0x5F: '#', 0x60: 'è', 
    0x7B: 'â', 0x7C: 'ô', 0x7D: 'û', 0x7E: 'ç'
}

# Portuguese/Spanish - Option 101 (5)
PORTUGUESE_SPANISH = {
    0x23: 'Ç', 0x24: '$', 0x40: '¡', 
    0x5B: 'á', 0x5C: 'é', 0x5D: 'í', 0x5E: 'ó', 
    0x5F: 'ú', 0x60: '¿', 
    0x7B: 'ü', 0x7C: 'ñ', 0x7D: 'è', 0x7E: 'à'
}

# Turkish - Option 110 (6)
TURKISH = {
    0x23: 'ğ', 0x24: 'Ğ', 0x40: 'İ', 
    0x5B: 'Ş', 0x5C: 'Ö', 0x5D: 'Ç', 0x5E: 'Ü', 
    0x5F: 'ğ', 0x60: 'ç', 
    0x7B: 'ş', 0x7C: 'ö', 0x7D: 'ü', 0x7E: 'ı'
}

# We can add more as needed.

SETS = [
    ENGLISH,            # 000
    GERMAN,             # 001
    SWEDISH_FINNISH,    # 010
    ITALIAN,            # 011
    FRENCH,             # 100
    PORTUGUESE_SPANISH, # 101
    TURKISH,            # 110
    ENGLISH,            # 111 (Romania placeholder)
]

def get_char(byte_val, subset_idx):
    if subset_idx < 0 or subset_idx >= len(SETS):
        subset_idx = 0
    
    mapping = SETS[subset_idx]
    
    # If byte is in mapping, return mapped char.
    # Else return ASCII equivalent (for basic chars)
    
    valid_byte = byte_val & 0x7F # Strip parity if present (though our packet data is 8-bit usually already stripping parity?)
    # Packet data we store is raw bytes. We should probably strip parity bit 7 before lookup.
    
    if valid_byte in mapping:
        return mapping[valid_byte]
        
    return chr(valid_byte)

import unicodedata

def get_byte_from_char(char, subset_idx):
    if len(char) != 1: return 0
    
    # Normalize input to NFC to match our map keys (if they are NFC, which python literals usually are)
    char = unicodedata.normalize('NFC', char)
    
    if subset_idx < 0 or subset_idx >= len(SETS):
        subset_idx = 0
        
    mapping = SETS[subset_idx]
    
    for code, mapped_char in mapping.items():
        if mapped_char == char:
            return code
            
    return ord(char)
