package com.davos.assessment.common;

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.Locale;
import java.util.Map;
import org.apache.fontbox.ttf.TrueTypeCollection;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthController {
  @GetMapping("/api/health")
  public Map<String, String> health() {
    return Map.of("status", "ok", "time", Instant.now().toString());
  }

  @GetMapping("/api/health/pdf-font")
  public Map<String, String> pdfFont() throws IOException {
    try (PDDocument document = new PDDocument()) {
      PDFont font = loadChineseFont(document);
      font.encode("中文PDF导出测试");
      return Map.of("status", "ok", "font", font.getName(), "time", Instant.now().toString());
    }
  }

  private PDFont loadChineseFont(PDDocument document) throws IOException {
    String[] fontPaths = {
        "/app/fonts/NotoSansCJK-Regular.ttc",
        "/Library/Fonts/Arial Unicode.ttf",
        "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
        "/System/Library/Fonts/Hiragino Sans GB.ttc",
        "/System/Library/Fonts/Supplemental/Songti.ttc",
        "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/noto/NotoSansCJK-Regular.ttc",
        "/System/Library/Fonts/PingFang.ttc",
        "/System/Library/Fonts/STHeiti Light.ttc"
    };
    for (String path : fontPaths) {
      File font = new File(path);
      if (!font.exists()) continue;
      if (path.toLowerCase(Locale.ROOT).endsWith(".ttc")) {
        PDFont loaded = loadTtcFont(document, font);
        if (loaded != null) return loaded;
        continue;
      }
      return PDType0Font.load(document, font);
    }
    throw new IOException("未找到可用于 PDF 中文导出的字体");
  }

  private PDFont loadTtcFont(PDDocument document, File fontFile) throws IOException {
    try (TrueTypeCollection collection = new TrueTypeCollection(fontFile)) {
      String[] preferredNames = {
          "NotoSansCJKsc-Regular",
          "NotoSansCJKSC-Regular",
          "NotoSansCJK-Regular",
          "PingFangSC-Regular",
          "HiraginoSansGB-W3",
          "STHeitiSC-Light"
      };
      for (String name : preferredNames) {
        TrueTypeFont font = collection.getFontByName(name);
        if (font != null) return PDType0Font.load(document, font, true);
      }
      final PDFont[] loaded = new PDFont[1];
      collection.processAllFonts(font -> {
        if (loaded[0] != null) return;
        String name = font.getName();
        if (name != null && (name.toLowerCase(Locale.ROOT).contains("cjk")
            || name.toLowerCase(Locale.ROOT).contains("pingfang")
            || name.toLowerCase(Locale.ROOT).contains("hiragino")
            || name.toLowerCase(Locale.ROOT).contains("heiti")
            || name.toLowerCase(Locale.ROOT).contains("songti"))) {
          loaded[0] = PDType0Font.load(document, font, true);
        }
      });
      return loaded[0];
    }
  }
}
