// Copyright (C) 2003-2026, Foxit Software Inc..
// All Rights Reserved.
//
// http://www.foxitsoftware.com
//
// The following code is copyrighted and contains proprietary information and trade secrets of Foxit Software Inc..
// You cannot distribute any part of Foxit PDF SDK to any third party or general public,
// unless there is a separate license agreement with Foxit Software Inc. which explicitly grants you such rights.
//
// This file contains an example to demonstrate how to use Foxit PDF SDK to loading PDF document asynchronously.


import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Renderer;
import com.foxit.sdk.common.file.AsyncReaderCallback;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;

import java.io.*;
import java.util.ArrayList;

import static com.foxit.sdk.common.Bitmap.e_DIBArgb;
import static com.foxit.sdk.common.Constants.e_ErrDataNotReady;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;

public class async_load {
    private static String key = "8f3g1sFttW8NAgfSNv+BvaJWy4Pp8flcm7ncNDkdvCCO4JwUFbGLyZdujReMaiplbYJwwxam3lr7uHtQaEf0xTSu8j2ZBdrZ8FS4OQkEUjOI9UEGb3Ic0GqmlP9T9OnSItOYqoZZA/OnYg5EaVP+c/ha32nUquOk+5riEdhYgINAZDdgJkPc+YSWsrfixOIROf6t34j3eRcyFnOY0MS7tSXi1AEPE0VZdyu+5bfmBSGvLI+WR5lQ+oOQR5hRGwqf990WEdBelNO7dxhZELVhEZ+/L60PpDf5y9A8qmrY6PiEU8nhqIyLpks0BxeIsYyZ3Cr82jjfbItPGyEARsIVzluBSgVMD4oGjojz8T6wGDEXUbmZH/pra94XAs8wZ28mzbmIEppWnhBqX0c6Ov8iXCfFfUZr4KNuPxMEgk4VOYTMOd70GEjGnylivw18hbn7ZpkWh2Zql+uU2HejvOgn+WHvT0HF9NhK0Xb5vH8aFkgBP92PJx8obIbxBVeeExczxvsqWufuPdXS2sPNIgMYZ2TtvqVVsiUToeGB2FQo09fzZOSH5Vi18u/siyvC3FmjeMf4+OYWPuDPNCcrb21NgcHam7XI8AAoGLTGtLLWR+fxynbBBcg2d2ZPcZ5CMFIfOBHRWCkcA0sDT3cl9nsBppHNJze/jVxHy+lREeQEA/etSIgohTGAWW3ZlpibRXqMxlmVWEPifCubbWTzQ6U54mRMpW2UghH3gvCNFNgdday3Av7EtlIJOdUuiMq3zmmd5J4Jt4C82CKnVewA0DtZ4EEtwRG9IYuLrfpuZlnjBAtoeXRV1lqUZ6BcCe0E3m9jO28sGkALuIeJQ74XG7X8lMC7lMWWQYiYD7D6G9QT6X3qV4vXJwNKRKpYc/rI+amEawTSnDYAMEaYPwtZ0rb/7BL+atsZZaI+ncebdfVfjDJZbCZAc+JnkuNlUn2xrNRuObw8h/G0lm2pNOEJ10S8R4+yplo5B1QmC2dCDhaTaW67QJPqnJPKbVmjw7i6KGBi+CDP3JnAKlOX/YUu8uk56JfeQUuGcQg/KaBMGctnchH2n1i3+oGbibhurQj22+OO66Q6mijKoCRP/QJm5WTGcO4c0iVX0B8dIlai/uv/pGU8Wps9MxysH5ViVIj56LWKuNfy1FNO9+wx4iHYnUxfW0Iccq3YJYAlOBkMv8VZIJl9aTEEFJ626LJEw/UbEBdR9CdlwnylPf4RuNW/lcGYMgq3xNVpp/bzYKwZivvQOFgMdX1tv8D33SHvAm3zrRCU+v94BJXnBi9k25fnKsc705IMwv+I6bueXA+mDnagj1oTqsWXZ0WnPAYI3WUO7sBf7XUH230wfYXnTSVhujJxiEP/2oWqi2813gtQkKwz9N2p6yT8Vsh3+BaAwwDvtOZMJKUFcabTBSL6NoouzmzwItJ4mIboT6KXaef66rs8jCU=";
    private static String sn = "Rew1vB0TqoMIxX444UcpScARWdhgJpsJfzmyMLR32a0+EgrCjp2NaQ==";
    private static String output_path = "../output_files/async_load/";
    private static String input_path = "../input_files/";

    // You can also use System.load("filename") instead. The filename argument must be an absolute path name.
    static {
        String os = System.getProperty("os.name").toLowerCase();
        String lib = "fsdk_java_";
        if (os.startsWith("win")) {
            lib += "win";
        } else if (os.startsWith("mac")) {
            lib += "mac";
        } else {
            lib += "linux";
        }
        
        if (System.getProperty("sun.arch.data.model").equals("64")) {
          if(System.getProperty("os.arch").equals("aarch64")){
            if(os.startsWith("mac")){
              lib += "arm";
             }
             else{
               lib += "armv8";
             }
           }
           else{
              lib += "64";
           }
        } else {
           if(System.getProperty("os.arch").equals("arm")){
              lib += "armv7";
           }
           else{
              lib += "32";
           }
        }
        System.loadLibrary(lib);
    }

    private static void createResultFolder(String output_path) {
        File myPath = new File(output_path);
        if (!myPath.exists()) {
            myPath.mkdir();
        }
    }

    public static void main(String[] args) throws PDFException {
        createResultFolder(output_path);
        // Initialize library
        int error_code = Library.initialize(sn, key);
        if (error_code != e_ErrSuccess) {
            System.out.println(String.format("Library Initialize Error: %d", error_code));
            return;
        }

        String input_file = input_path + "AboutFoxit.pdf";
        String output_file = output_path + "async-load page0.bmp";

        try {

            AsyncFileRead file_read = new AsyncFileRead();
            file_read.loadFile(input_file, false);
            PDFDoc doc = new PDFDoc(file_read, true);

            // Actually, here, application should download needed data which specified by AsyncFileRead::AddDownloadHint().
            // But here, for simple example, we just "download" these data inside AsyncFileRead::AddDownloadHint().
            // So, just continue to check the ready state here, which will trigger AsyncFileRead::AddDownloadHint() to
            // "download" data.
            int code = e_ErrDataNotReady;
            while (code == e_ErrDataNotReady) {
                code = doc.load(null);
            }

            if (code != e_ErrSuccess) {
                System.out.println(String.format("The Doc [%s] Error: %d", input_file, error_code));
                return;
            }

            // Actually, here, application should download needed data which specified by AsyncFileRead::AddDownloadHint().
            // But here, for simple example, we just "download" these data inside AsyncFileRead::AddDownloadHint().
            // So, just continue to check the ready state here, which will trigger AsyncFileRead::AddDownloadHint() to
            // "download" data.
            PDFPage page = null;
            code = e_ErrDataNotReady;
            while (code == e_ErrDataNotReady) {
                page = doc.getPage(0);
                if (!page.isEmpty()) {
                    code = e_ErrSuccess;
                }
            }
            // Parse page.
            page.startParse(e_ParsePageNormal, null, false);

            int width = (int) page.getWidth();
            int height = (int) page.getHeight();
            Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation());

            // Prepare a bitmap for rendering.
            Bitmap bitmap = new Bitmap(width, height, e_DIBArgb, null, 0);
            bitmap.fillRect(0xFFFFFFFF, null);
            // Render page
            Renderer render = new Renderer(bitmap, false);
            render.startRender(page, matrix, null);

            Image image = new Image();
            image.addFrame(bitmap);
            image.saveAs(output_file);

            System.out.println("async-load demo finished.");

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        Library.release();
    }
}

// Data of asynchronous loader callback object.
class DownloadHintDataInfo {
    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    private int offset;
    private long size;
    private boolean is_downloaded;

    public long getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public boolean isIs_downloaded() {
        return is_downloaded;
    }

    public void setIs_downloaded(boolean is_downloaded) {
        this.is_downloaded = is_downloaded;
    }

    DownloadHintDataInfo() {
        this.offset = 0;
        this.size = 0;
        this.is_downloaded = false;
    }

    DownloadHintDataInfo(int offset, long size, boolean is_downloaded) {
        this.offset = offset;
        this.size = size;
        this.is_downloaded = is_downloaded;
    }
}

// This callback just simulates the downloading progress by DownloadHintDataInfo::is_downloaded,
// not really to download data.
class AsyncFileRead extends AsyncReaderCallback {
    private RandomAccessFile file_ = null;
    private boolean is_large_file_ = false;
    // Used to record added hint range and if the range is downloaded.
    private ArrayList<DownloadHintDataInfo> hint_data_record_;

    AsyncFileRead() {

    }

    public boolean loadFile(String file_path, boolean is_large_file) throws FileNotFoundException {
        this.is_large_file_ = is_large_file;
        hint_data_record_ = new ArrayList<DownloadHintDataInfo>();
        file_ = new RandomAccessFile(file_path, "r");
        return true;

    }

    @Override
    public long getSize() {
        try {
            return this.file_.length();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return 0;
    }

    @Override
    public boolean readBlock(byte[] buffer, long offset, long size) {
      try {
            file_.seek(offset);
            int read = file_.read(buffer, 0, (int) size);
            return read == size ? true : false;
        } catch (IOException e) {
            e.printStackTrace();
        }
      return false;
    }

    @Override
    public void release() {
        try {
             this.file_.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isDataAvail(int offset, long size) {
        boolean ret = checkRecordDownloaded(offset, size, true);
        return ret;
    }

    @Override
    public boolean addDownloadHint(int offset, long size) {
        // Record the range and downloaded data.
        boolean ret = checkRecordDownloaded(offset, size, true);
        return ret;
    }

    boolean checkRecordDownloaded(int offset, long size, boolean to_download) {
        int record_count = hint_data_record_.size();
        for (int i = 0; i < record_count; i++) {
            DownloadHintDataInfo data_info = (DownloadHintDataInfo) hint_data_record_.get(i);

            // If (offset+size) is out of current data_info, just continue to check other record in hint_data_record_.
            if (offset > (data_info.getOffset() + data_info.getSize())) {
                continue;
            }
            if (offset + size < data_info.getOffset()) {
                continue;
            }

            // If data defined by <offset, size> has been in/within current data info, just download the data.
            if (offset >= data_info.getOffset() && (offset + size) <= (data_info.getOffset() + data_info.getSize())) {
                if (to_download)
                    data_info.setIs_downloaded(true);
                return data_info.isIs_downloaded();
            }

            // If only part of data defined by <offset, size> is in current data_info, download current data_info and
            // also check and download rest data.
            if (offset >= data_info.getOffset() && offset < (data_info.getOffset() + data_info.getSize()) &&
                    (offset + size) > (data_info.getOffset() + data_info.getSize())) {
                if (to_download)
                    data_info.setIs_downloaded(true);
                if (!data_info.isIs_downloaded())
                    return data_info.isIs_downloaded();
                int new_offset = data_info.getOffset() + (int)data_info.getSize() + 1;
                long new_size = size - 1 - (data_info.getOffset() + data_info.getSize() - offset);
                return checkRecordDownloaded(new_offset, new_size, to_download);
            }

            if (offset < data_info.getOffset() && (offset + size) >= data_info.getOffset() &&
                    (offset + size) <= (data_info.getOffset() + data_info.getSize())) {
                if (to_download)
                    data_info.setIs_downloaded(true);
                if (!data_info.isIs_downloaded())
                    return data_info.isIs_downloaded();
                int new_offset = offset;
                int new_size = data_info.getOffset() - 1 - offset;
                return checkRecordDownloaded(new_offset, new_size, to_download);
            }
            if (offset < data_info.getOffset() && (offset + size) > (data_info.getOffset() + data_info.getSize())) {
                if (to_download)
                    data_info.setIs_downloaded(true);
                if (!data_info.isIs_downloaded())
                    return data_info.isIs_downloaded();
                int new_offset = offset;
                long new_size = data_info.getOffset() - 1 - offset;
                if (checkRecordDownloaded(new_offset, new_size, to_download)) {
                    new_offset = data_info.getOffset() + (int)data_info.getSize() + 1;
                    new_size = size - 1 - (data_info.getOffset() - offset + data_info.getSize());
                    return checkRecordDownloaded(new_offset, new_size, to_download);
                } else
                    return false;
            }
        }
        if (to_download) {
            return downloadData(offset, size);
        }
        return false;
    }

    boolean downloadData(int offset, long size) {
        DownloadHintDataInfo new_info = new DownloadHintDataInfo(offset, size, true);
        hint_data_record_.add(new_info);
        return true;
    }
}
