import os
import re
import argparse
import sys

missing = []
for lib in ["chardet"]:
    try:
        __import__(lib)
    except ImportError:
        missing.append(lib)
if missing:
    print(f"Error: Missing libraries:\n{', '.join(missing)}")
    print(f"  pip install chardet")
    input("\nPress ENTER to terminate>>\n")
    sys.exit(1)

import chardet


def terminate():
    input(f"\nPress ENTER to terminate>>\n")
    exit()
    
def detect_encoding(file_path):
    """ファイルの文字コードを判定して開く"""
    with open(file_path, 'rb') as f:
        raw_data = f.read()
    result = chardet.detect(raw_data)
    return result['encoding']

def load_replace_dict(ini_path):
    """replace.iniを読み込んで辞書を作成（コメント行無視、空白保持）"""
    replace_dict = {}
    if not ini_path or not os.path.isfile(ini_path):
        if ini_path:
            print(f"  [skip] File not found: {ini_path}")
        return replace_dict

    try:
        with open(ini_path, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.rstrip('\n')
                if line.startswith('#') or '=' not in line:
                    continue

                match = re.match(r"""^(['"].+?['"]|[^=]+?)=(.*)$""", line)
                if not match:
                    continue

                raw_key, val = match.groups()
                key = raw_key[1:-1] if (raw_key.startswith("'") and raw_key.endswith("'")) or (raw_key.startswith('"') and raw_key.endswith('"')) else raw_key.strip()
                replace_dict[key] = val  # valの前後の空白は保持
    except Exception as e:
        print(f"  [skip] Failed to read {ini_path}: {e}")
    return replace_dict

def apply_replacements(text, replace_list):
    """テキストに対して正規表現による置換を適用"""
    for pattern, replacement in replace_list:
        try:
            print(f"pattern: {pattern} => {replacement}")
            text = re.sub(pattern, replacement, text, flags=re.IGNORECASE | re.MULTILINE)
        except Exception as e:
            print(f"re.sub error for [{pattern}]: {e}")
            terminate()

    return text

def generate_output_path(input_path):
    """output_fileが指定されていない場合の自動生成"""
    base, ext = os.path.splitext(input_path)
    return f"{base}-converted{ext}"

def main():
    parser = argparse.ArgumentParser(description='読み方補正プログラム（正規表現対応）')
    parser.add_argument('--input_file', "-i", default='input.md', help='入力テキストファイルのパス（デフォルト: input.md）') 
    parser.add_argument('--output_file', "-o", default=None, help='出力ファイルのパス（指定がない場合は自動生成）') 
    parser.add_argument('--replace_file', "-r", default='replace.ini', help='default replace.iniのパス（デフォルト: replace.ini）') 
    parser.add_argument('--replace_file2', "-s", default='', help='user replace.iniのパス（デフォルト: ""）')
    args = parser.parse_args()

    input_path = args.input_file
    output_path = args.output_file or generate_output_path(input_path)

    print(f"\nRead input file from [{input_path}]")
    encoding = detect_encoding(input_path)
    print(f"  encoding:", encoding)

    text = None
    try:
        with open(input_path, 'r', encoding=encoding) as f:
            text = f.read()
    except Exception as e:
        print(f"  Failed with encoding={encoding}: {e}")
        print("  Retrying with utf-8...")
        with open(input_path, 'r', encoding="utf-8") as f:
            text = f.read()

    # 置換辞書の読み込み
    print(f"\nLoading replacement dictionaries...")
    dict2 = load_replace_dict(args.replace_file2)  # ユーザー定義
    dict1 = load_replace_dict(args.replace_file)   # デフォルト

    # dict2 に存在するキーは dict1 から除外（ユーザー定義が優先）
    dict1_filtered = {k: v for k, v in dict1.items() if k not in dict2}

    # 表示
    print("Replacement list (replace_file2 first, then replace_file):")
    for pattern, replacement in dict2.items():
        print(f"  [user] {pattern}: [{replacement}]")
    for pattern, replacement in dict1_filtered.items():
        print(f"  [default] {pattern}: [{replacement}]")

    # 置換処理: dict2 → dict1_filtered の順で適用
    print("\nReplacing...")
    replaced_text = apply_replacements(text, list(dict2.items()))
    replaced_text = apply_replacements(replaced_text, list(dict1_filtered.items()))

    # 出力
    print(f"\nWrite to [{output_path}]")
    with open(output_path, 'w', encoding='utf-8') as f:
        f.write(replaced_text)

if __name__ == '__main__':
    main()
    terminate()
