/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0.
 */
package com.easyagents.core.file2text;

import com.easyagents.core.file2text.source.DocumentSource;

import java.util.Objects;
import java.util.function.BooleanSupplier;

/**
 * 轻量文档读取请求及结构安全边界。
 */
public class LightweightDocumentReadRequest {

    private final DocumentSource source;
    private int maxPdfPages = 200;
    private int maxSlides = 200;
    private int maxSheets = 20;
    private int maxNonEmptyCells = 50_000;
    private long maxExpandedChars = 150L * 1024L * 1024L;
    private BooleanSupplier cancelled = () -> false;

    /**
     * 创建读取请求。
     *
     * @param source 文档来源
     */
    public LightweightDocumentReadRequest(DocumentSource source) {
        this.source = Objects.requireNonNull(source, "DocumentSource cannot be null");
    }

    /** @return 文档来源 */
    public DocumentSource getSource() { return source; }

    /** @return 最大 PDF 页数 */
    public int getMaxPdfPages() { return maxPdfPages; }

    /** @param maxPdfPages 最大 PDF 页数 */
    public void setMaxPdfPages(int maxPdfPages) { this.maxPdfPages = positive(maxPdfPages, "maxPdfPages"); }

    /** @return 最大幻灯片数 */
    public int getMaxSlides() { return maxSlides; }

    /** @param maxSlides 最大幻灯片数 */
    public void setMaxSlides(int maxSlides) { this.maxSlides = positive(maxSlides, "maxSlides"); }

    /** @return 最大工作表数 */
    public int getMaxSheets() { return maxSheets; }

    /** @param maxSheets 最大工作表数 */
    public void setMaxSheets(int maxSheets) { this.maxSheets = positive(maxSheets, "maxSheets"); }

    /** @return 最大非空单元格数 */
    public int getMaxNonEmptyCells() { return maxNonEmptyCells; }

    /** @param maxNonEmptyCells 最大非空单元格数 */
    public void setMaxNonEmptyCells(int maxNonEmptyCells) {
        this.maxNonEmptyCells = positive(maxNonEmptyCells, "maxNonEmptyCells");
    }

    /** @return 最大展开字符数 */
    public long getMaxExpandedChars() { return maxExpandedChars; }

    /** @param maxExpandedChars 最大展开字符数 */
    public void setMaxExpandedChars(long maxExpandedChars) {
        if (maxExpandedChars <= 0) {
            throw new IllegalArgumentException("maxExpandedChars must be positive");
        }
        this.maxExpandedChars = maxExpandedChars;
    }

    /** @return 取消检查器 */
    public BooleanSupplier getCancelled() { return cancelled; }

    /** @param cancelled 取消检查器 */
    public void setCancelled(BooleanSupplier cancelled) {
        this.cancelled = cancelled == null ? () -> false : cancelled;
    }

    /**
     * 检查当前读取是否已取消。
     *
     * @throws DocumentReadException 已取消时抛出
     */
    public void checkCancelled() throws DocumentReadException {
        if (cancelled.getAsBoolean() || Thread.currentThread().isInterrupted()) {
            throw new DocumentReadException(DocumentReadErrorCode.DOCUMENT_READ_CANCELLED, "Document read cancelled");
        }
    }

    private int positive(int value, String name) {
        if (value <= 0) {
            throw new IllegalArgumentException(name + " must be positive");
        }
        return value;
    }
}
