package com.example.myapplication.service;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.OpenableColumns;
import android.util.Log;

import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.text.TextRecognition;
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions;

// Keep only PDFBox and I/O for text extraction
import com.tom_roush.pdfbox.pdmodel.PDDocument;
import com.tom_roush.pdfbox.text.PDFTextStripper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class UploadService {
    public interface ProgressListener {
        void onProgress(int progress);
        void onCompleted();
    }

    public static void simulateUpload(Handler handler, ProgressListener listener) {
        for (int i = 1; i <= 10; i++) {
            int progress = i * 10;
            handler.postDelayed(() -> {
                if (listener != null) listener.onProgress(progress);
                if (progress == 100 && listener != null) listener.onCompleted();
            }, i * 200);
        }
    }

    public static String getMimeType(Context ctx, Uri uri) {
        ContentResolver cr = ctx.getContentResolver();
        return cr.getType(uri);
    }

    public static String getDisplayName(Context ctx, Uri uri) {
        String name = null;
        try (Cursor cursor = ctx.getContentResolver()
                .query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                name = cursor.getString(0);
            }
        } catch (Exception ignored) {}
        return name;
    }

    // Removed: OcrCallback interface and any runTextRecognition code.
    // OCR must be performed via OCRService.recognize(...)

    public static String extractPdfText(Context ctx, Uri uri) throws IOException {
        InputStream inputStream = ctx.getContentResolver().openInputStream(uri);
        if (inputStream == null) return "";
        java.io.File tmp = java.io.File.createTempFile("ocr_pdf_", ".pdf", ctx.getCacheDir());
        try (java.io.FileOutputStream fos = new java.io.FileOutputStream(tmp)) {
            byte[] buf = new byte[8192];
            int len;
            while ((len = inputStream.read(buf)) != -1) fos.write(buf, 0, len);
        } finally { inputStream.close(); }

        String full = com.example.myapplication.service.OCRService.extractTextFromFile(ctx, tmp.getAbsolutePath());
        try { tmp.delete(); } catch (Exception ignored) {}
        return full == null ? "" : full;
    }

    public static String extractPlainText(Context ctx, Uri uri) throws IOException {
        InputStream inputStream = ctx.getContentResolver().openInputStream(uri);
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }
        inputStream.close();
        return result.toString("UTF-8");
    }

    public static String guessSubject(String lower, String[] dictMath, String[] dictPhysics, String[] dictChem, String[] dictEnglish, String[] dictCS, String[] dictRobotics) {
        for (String k : dictRobotics) { if (lower.contains(k.toLowerCase())) return "机器人学导论"; }
        for (String k : dictCS) { if (lower.contains(k.toLowerCase())) return "计算机基础"; }
        for (String k : dictMath) { if (lower.contains(k.toLowerCase())) return "数学"; }
        for (String k : dictPhysics) { if (lower.contains(k.toLowerCase())) return "物理"; }
        for (String k : dictChem) { if (lower.contains(k.toLowerCase())) return "化学"; }
        for (String k : dictEnglish) { if (lower.contains(k.toLowerCase())) return "英语"; }
        return null;
    }
}