const fs = require('fs');
const path = require('path');

// 读取 C 盘根目录的配置文件
const cDriveFile = 'C:\\system-templates.json';
const projectFile = path.join(__dirname, '../electron/resources/extra/common/config/system-templates.json');

console.log('=== 对比 C 盘配置 vs 项目配置 ===\n');

if (!fs.existsSync(cDriveFile)) {
  console.log('❌ C 盘根目录下未找到 system-templates.json');
  process.exit(1);
}

const cDriveConfig = JSON.parse(fs.readFileSync(cDriveFile, 'utf-8'));
const projectConfig = JSON.parse(fs.readFileSync(projectFile, 'utf-8'));

const cTemplates = cDriveConfig.subtitleTemplates || [];
const pTemplates = projectConfig.subtitleTemplates || [];

console.log(`C 盘配置: ${cTemplates.length} 个模板`);
console.log(`项目配置: ${pTemplates.length} 个模板\n`);

// 对比每个模板的标题字幕配置
cTemplates.forEach(cTemplate => {
  const pTemplate = pTemplates.find(t => t.id === cTemplate.id);
  
  if (!pTemplate) {
    console.log(`⚠️  模板 ${cTemplate.name} (${cTemplate.id}) 在项目配置中不存在`);
    return;
  }
  
  const cTitle = cTemplate.config.titleSubtitleConfig;
  const pTitle = pTemplate.config.titleSubtitleConfig;
  
  if (!cTitle || !pTitle) {
    console.log(`⚠️  模板 ${cTemplate.name} 缺少标题字幕配置`);
    return;
  }
  
  // 检查是否相同
  const cKey = `${cTitle.maxCharsPerLine}-${cTitle.lines.length}-${cTitle.lines.map(l => `${l.text}:${l.fontName}:${l.fontColor}`).join('|')}`;
  const pKey = `${pTitle.maxCharsPerLine}-${pTitle.lines.length}-${pTitle.lines.map(l => `${l.text}:${l.fontName}:${l.fontColor}`).join('|')}`;
  
  if (cKey === pKey) {
    console.log(`✅ 模板 ${cTemplate.name}: 配置相同`);
  } else {
    console.log(`❌ 模板 ${cTemplate.name}: 配置不同`);
    console.log(`   C盘: ${cTitle.maxCharsPerLine}字符/行, ${cTitle.lines.length}行`);
    console.log(`   项目: ${pTitle.maxCharsPerLine}字符/行, ${pTitle.lines.length}行`);
    cTitle.lines.forEach((l, i) => {
      const pLine = pTitle.lines[i];
      if (pLine) {
        if (l.text !== pLine.text || l.fontName !== pLine.fontName || l.fontColor !== pLine.fontColor) {
          console.log(`   第${i+1}行:`);
          console.log(`     C盘: "${l.text}" (${l.fontName}, ${l.fontColor})`);
          console.log(`     项目: "${pLine.text}" (${pLine.fontName}, ${pLine.fontColor})`);
        }
      }
    });
  }
});

// 检查关键词组样式
console.log('\n=== 关键词组样式对比 ===\n');

cTemplates.forEach(cTemplate => {
  const pTemplate = pTemplates.find(t => t.id === cTemplate.id);
  if (!pTemplate) return;
  
  const cKeywords = cTemplate.config.keywordGroupsStyles || [];
  const pKeywords = pTemplate.config.keywordGroupsStyles || [];
  
  let hasDiff = false;
  cKeywords.forEach((cKw, idx) => {
    const pKw = pKeywords[idx];
    if (!pKw) return;
    
    if (cKw.groupName === pKw.groupName) {
      const cFont = cKw.styleOverride?.fontName || 'N/A';
      const pFont = pKw.styleOverride?.fontName || 'N/A';
      const cEffect = cKw.effectId || 'N/A';
      const pEffect = pKw.effectId || 'N/A';
      
      if (cFont !== pFont || cEffect !== pEffect) {
        if (!hasDiff) {
          console.log(`模板 ${cTemplate.name}:`);
          hasDiff = true;
        }
        console.log(`  ${cKw.groupName}:`);
        if (cFont !== pFont) {
          console.log(`    字体: ${cFont} (C盘) vs ${pFont} (项目)`);
        }
        if (cEffect !== pEffect) {
          console.log(`    特效: ${cEffect} (C盘) vs ${pEffect} (项目)`);
        }
      }
    }
  });
  
  if (!hasDiff && cKeywords.length > 0) {
    console.log(`✅ 模板 ${cTemplate.name}: 关键词组样式相同`);
  }
});














