#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
完全還原導航列到原始狀態（黑色主題，無自定義樣式）
"""
import os
import re
from pathlib import Path

def restore_original_navbar(file_path):
    """將導航列完全還原到原始狀態"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()

        original_content = content

        # 將任何變體改回原始的 navbar-dark bg-dark
        content = re.sub(
            r'<nav class="navbar navbar-expand-xl navbar-(dark|light) bg-(dark|white|primary)([^"]*)"',
            r'<nav class="navbar navbar-expand-xl navbar-dark bg-dark\3"',
            content
        )

        # 移除所有導航列相關的自定義樣式
        # 移除 /* Blue Navbar Theme */ 區塊
        content = re.sub(
            r'\s*/\* Blue Navbar Theme \*/.*?(?=\s*\.|\s*</style>)',
            '\n',
            content,
            flags=re.DOTALL
        )

        # 移除 /* Navbar Custom Styles */ 區塊
        content = re.sub(
            r'\s*/\* Navbar Custom Styles \*/.*?(?=\s*\.|\s*</style>)',
            '\n',
            content,
            flags=re.DOTALL
        )

        # 清理多餘的空行
        content = re.sub(r'\n{3,}', '\n\n', content)

        # 只有內容改變時才寫入
        if content != original_content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            return True
        return False

    except Exception as e:
        print(f"處理 {file_path} 時發生錯誤: {e}")
        return False

def main():
    """主程序"""
    base_dir = Path(__file__).parent
    html_files = list(base_dir.glob("*.html"))

    updated_count = 0

    print(f"找到 {len(html_files)} 個 HTML 文件")
    print("=" * 60)

    for html_file in html_files:
        if restore_original_navbar(html_file):
            print(f"✅ 已還原: {html_file.name}")
            updated_count += 1
        else:
            print(f"⏭️  跳過: {html_file.name}")

    print("=" * 60)
    print(f"✅ 完成！共還原 {updated_count} 個文件到原始狀態")

if __name__ == "__main__":
    main()
