#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修復所有 HTML 文件的導航列顏色，從黑色改為淺色主題
"""
import os
import re
from pathlib import Path

def fix_navbar_color(file_path):
    """將導航列從 navbar-dark bg-dark 改為 navbar-light + 自定義背景"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()

        original_content = content

        # 替換導航列的類別
        # 從 navbar-dark bg-dark 改為 navbar-light bg-white
        content = re.sub(
            r'<nav class="navbar navbar-expand-xl navbar-dark bg-dark([^"]*)"',
            r'<nav class="navbar navbar-expand-xl navbar-light bg-white\1"',
            content
        )

        # 同時需要更新導航列的樣式，將 shadow-sm 改為更明顯的陰影
        # 並添加自定義的 CSS 來美化導航列

        # 檢查是否已經有 navbar 的自定義樣式
        if '.navbar {' not in content or 'box-shadow: 0 2px 8px' not in content:
            # 在 </style> 之前添加導航列樣式
            navbar_style = """
        /* Navbar Custom Styles */
        .navbar {
            box-shadow: 0 2px 12px rgba(0,0,0,0.1) !important;
            border-bottom: 1px solid #e5e7eb;
        }

        .navbar-brand {
            font-weight: 600;
            color: #1f2937 !important;
        }

        .navbar-light .navbar-nav .nav-link {
            color: #4b5563 !important;
            font-weight: 500;
            transition: all 0.3s;
        }

        .navbar-light .navbar-nav .nav-link:hover {
            color: #6366f1 !important;
        }

        .navbar-light .navbar-nav .nav-link.active {
            color: #6366f1 !important;
            font-weight: 600;
        }

        .navbar-text {
            color: #6b7280 !important;
        }
"""
            content = content.replace('</style>', navbar_style + '    </style>')

        # 只有內容改變時才寫入
        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 fix_navbar_color(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()
