import json
import re
import os

def parse_dart_map(filepath):
    translations = {}
    if not os.path.exists(filepath):
        return translations
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Matches maps of form: "key": "value" or 'key': 'value'
    # Handles escapes and multi-line content matched on single lines
    pattern = re.compile(r'^\s*([\'"])(.*?)\1\s*:\s*([\'"])(.*?)\3\s*,?\s*$', re.MULTILINE)
    matches = pattern.findall(content)
    for match in matches:
        key = match[1]
        val = match[3]
        # Unescape simple sequences (single and double quotes)
        key = key.replace('\\"', '"').replace("\\'", "'")
        val = val.replace('\\"', '"').replace("\\'", "'")
        translations[key] = val
        
    return translations

def to_dart_string(s):
    # Escape characters to be safe inside double quotes in Dart source code
    result = []
    for char in s:
        if char == '\\':
            result.append('\\\\')
        elif char == '"':
            result.append('\\"')
        elif char == '$':
            result.append('\\$')
        elif char == '\n':
            result.append('\\n')
        elif char == '\r':
            result.append('\\r')
        elif char == '\t':
            result.append('\\t')
        else:
            result.append(char)
    return "".join(result)

# Load current Arabic files
ar_eg = parse_dart_map("siro_driver/lib/controller/local/ar_eg.dart")
ar_jo = parse_dart_map("siro_driver/lib/controller/local/ar_jo.dart")
ar_sy = parse_dart_map("siro_driver/lib/controller/local/ar_sy.dart")

# Load JSON data
with open("siro_driver_translations_data.json", "r", encoding="utf-8") as f:
    json_data = json.load(f)

existing_syrian = json_data.get('existing_syrian', {})
missing_keys = json_data.get('missing_keys', [])

# Load legacy non-Arabic languages
with open("scratch/legacy_extracted_languages.json", "r", encoding="utf-8") as f:
    legacy_extracted = json.load(f)

# Compute master keys
master_keys = set(ar_sy.keys())
master_keys.update(ar_jo.keys())
master_keys.update(ar_eg.keys())
master_keys.update(existing_syrian.keys())
master_keys.update(missing_keys)

master_keys = sorted(list(master_keys))
print(f"Master keys count: {len(master_keys)}")

# Let's build translation maps
aligned_maps = {
    'ar-EG': {},
    'ar-JO': {},
    'ar-SY': {},
    'en': {},
    'de': {},
    'el': {},
    'es': {},
    'fa': {},
    'fr': {},
    'hi': {},
    'it': {},
    'ru': {},
    'tr': {},
    'ur': {},
    'zh': {}
}

for key in master_keys:
    # 1. English is always the key itself
    aligned_maps['en'][key] = key

    # 2. Syrian Arabic (ar-SY)
    val_sy = ar_sy.get(key)
    if not val_sy:
        val_sy = existing_syrian.get(key)
    if not val_sy:
        val_sy = ar_jo.get(key)
    if not val_sy:
        val_sy = ar_eg.get(key)
    if not val_sy:
        val_sy = key  # fallback to key
    aligned_maps['ar-SY'][key] = val_sy

    # 3. Jordanian Arabic (ar-JO)
    val_jo = ar_jo.get(key)
    if not val_jo:
        val_jo = val_sy  # fallback to Syrian
    aligned_maps['ar-JO'][key] = val_jo

    # 4. Egyptian Arabic (ar-EG)
    val_eg = ar_eg.get(key)
    if not val_eg:
        val_eg = val_jo  # fallback to Jordanian/Syrian
    aligned_maps['ar-EG'][key] = val_eg

    # 5. Non-Arabic languages
    for lang in ['de', 'el', 'es', 'fa', 'fr', 'hi', 'it', 'ru', 'tr', 'ur', 'zh']:
        val_lang = legacy_extracted.get(lang, {}).get(key)
        if not val_lang:
            val_lang = key  # fallback to English
        aligned_maps[lang][key] = val_lang

# Directories and file writes
output_dir = "siro_driver/lib/controller/local"

def write_dart_file(filename, map_name, data_map):
    filepath = os.path.join(output_dir, filename)
    with open(filepath, 'w', encoding='utf-8') as f:
        # Write final Map<String, String> map_name = { ... };
        f.write(f"final Map<String, String> {map_name} = {{\n")
        for k, v in data_map.items():
            k_escaped = to_dart_string(k)
            v_escaped = to_dart_string(v)
            f.write(f'  "{k_escaped}": "{v_escaped}",\n')
        f.write("};\n")
    print(f"Wrote {filepath} with {len(data_map)} keys.")

# Write Arabic dialects
write_dart_file("ar_eg.dart", "ar_eg", aligned_maps['ar-EG'])
write_dart_file("ar_jo.dart", "ar_jo", aligned_maps['ar-JO'])
write_dart_file("ar_sy.dart", "ar_sy", aligned_maps['ar-SY'])

# Write English
write_dart_file("en.dart", "en", aligned_maps['en'])

# Write non-Arabic languages
for lang in ['de', 'el', 'es', 'fa', 'fr', 'hi', 'it', 'ru', 'tr', 'ur', 'zh']:
    write_dart_file(f"{lang}.dart", lang, aligned_maps[lang])

print("Alignment and file writing complete.")
