package org.libreoffice;

import android.graphics.Bitmap;
import android.util.Log;

import org.libreoffice.kit.Document;
import org.libreoffice.kit.LibreOfficeKit;
import org.libreoffice.kit.Office;
import org.mozilla.gecko.gfx.BufferedCairoImage;
import org.mozilla.gecko.gfx.CairoImage;
import org.mozilla.gecko.gfx.LayerController;
import org.mozilla.gecko.gfx.SubTile;

import java.nio.ByteBuffer;

public class LOKitTileProvider implements TileProvider {
    private static final String LOGTAG = LOKitShell.class.getSimpleName();

    private final LayerController mLayerController;

    public static int TILE_SIZE = 256;
    private final double mTileWidth;
    private final double mTileHeight;


    public final Office mOffice;
    public final Document mDocument;

    private double mDPI;

    private double twipToPixel(double input, double dpi) {
        return input / 1440.0 * dpi;
    }

    private double pixelToTwip(double input, double dpi) {
        return (input / dpi) * 1440.0;
    }

    public LOKitTileProvider(LayerController layerController, String input) {
        mLayerController = layerController;
        mDPI = (double) LOKitShell.getDpi();
        mTileWidth = pixelToTwip(TILE_SIZE, mDPI);
        mTileHeight = pixelToTwip(TILE_SIZE, mDPI);

        LibreOfficeKit.putenv("SAL_LOG=+WARN+INFO-INFO.legacy.osl-INFO.i18nlangtag");
        LibreOfficeKit.init(LibreOfficeMainActivity.mAppContext);

        mOffice = new Office(LibreOfficeKit.getLibreOfficeKitHandle());

        mDocument = mOffice.documentLoad(input);

        if(mDocument == null) {
            Log.e(LOGTAG, "Error: " + mOffice.getError());
        } else {
            Log.i(LOGTAG, "Document parts: " + mDocument.getParts());
            if (mDocument.getParts() >= 1) {
                mDocument.setPart(0);
            }
            Log.i(LOGTAG, "Document size: " + mDocument.getDocumentWidth() + " x " + mDocument.getDocumentHeight());
        }
    }

    @Override
    public int getPageWidth() {
        return (int) twipToPixel(mDocument.getDocumentWidth(), mDPI);
    }

    @Override
    public int getPageHeight() {
        return (int) twipToPixel(mDocument.getDocumentHeight(), mDPI);
    }

    @Override
    public boolean isReady() {
        return mDocument != null;
    }

    public SubTile createTile(int x, int y) {
        ByteBuffer buffer = ByteBuffer.allocateDirect(TILE_SIZE * TILE_SIZE * 4);
        Bitmap bitmap = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE, Bitmap.Config.ARGB_8888);

        mDocument.paintTile(buffer, TILE_SIZE, TILE_SIZE, (int) pixelToTwip(x, mDPI), (int) pixelToTwip(y, mDPI), (int)mTileWidth, (int)mTileHeight);

        bitmap.copyPixelsFromBuffer(buffer);

        CairoImage image = new BufferedCairoImage(bitmap);
        SubTile tile = new SubTile(image, x, y);
        tile.beginTransaction();
        return tile;
    }
}

@@ -499,21 +499,28 @@
  = LOKit
-Shell
+TileProvider
 .class.g
@@ -1567,26 +1567,31 @@
       if
-(m
+ (check
 Document
  == null
@@ -1586,19 +1586,59 @@
 ment
- == null) %7B
+()) %7B%0A            int parts = mDocument.getParts();
 %0A   
@@ -1650,17 +1650,17 @@
     Log.
-e
+i
 (LOGTAG,
@@ -1665,54 +1665,235 @@
 G, %22
-Error: %22 + mOffice.getError());%0A        %7D else
+Document parts: %22 + parts);%0A            if (parts %3E= 1) %7B%0A                mDocument.setPart(0);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private boolean checkDocument() %7B%0A        if(mDocument == null %7C%7C !mOffice.getError().isEmpty())
  %7B%0A 
@@ -1899,33 +1899,33 @@
             Log.
-i
+e
 (LOGTAG, %22Docume
@@ -1922,46 +1922,46 @@
 G, %22
-Document parts: %22 + mDocument.getParts
+Error at loading: %22 + mOffice.getError
 ());
@@ -1977,37 +1977,116 @@
     
-if (mDocument.getParts() %3E= 1
+return false;%0A        %7D%0A%0A        if (mDocument.getDocumentWidth() == 0 && mDocument.getDocumentHeight() == 0
 ) %7B%0A
@@ -2097,39 +2097,86 @@
         
-    mDocument.setPart(0
+Log.e(LOGTAG, %22Document size zero - last error: %22 + mOffice.getError()
 );%0A     
@@ -2170,37 +2170,40 @@
 ror());%0A        
-    %7D
+%7D else %7B
 %0A            Log
@@ -2305,32 +2305,54 @@
 ht());%0A        %7D
+%0A%0A        return true;
 %0A    %7D%0A%0A    @Ove

Check if document has been loaded correctly