package epson.print.pdf;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.util.Log;
import epson.common.ExternalFileUtils;
import epson.print.CommonDefine;
import epson.print.EPImage;
import epson.print.Util.EPLog;
import epson.print.pdf.AdobePdfContainer;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class pdfRender implements CommonDefine {
    private static final float DEF_ZOOM_PREVIEW = 0.5f;
    private static final float DEF_ZOOM_PRINT = 1.5f;
    private static final String TAG = "PDF_CONVERTER";
    /* access modifiers changed from: private */
    public Thread backgroundThread;
    private Context mContext = null;
    private String mConvertedName;
    private String mConvertedNamePrint;
    protected volatile boolean mIsPreviewCon;
    protected boolean mOpenError = false;
    /* access modifiers changed from: private */
    public AdobePdfContainer mPdfFile;
    private String mPdfFilename;
    protected boolean mPreviewmode = false;
    protected volatile boolean mPrintingCon;
    protected volatile boolean mRendererStart = false;
    protected boolean mRotate = false;
    protected boolean mRotateCheck = false;
    public int mTotalPage;
    /* access modifiers changed from: private */
    public Handler mUiHandler;

    public int StartReloadDocument(String str) {
        return 0;
    }

    public void finalize() {
    }

    public pdfRender(Context context, Handler handler) {
        this.mContext = context;
        this.mUiHandler = handler;
    }

    public synchronized void startConvertPage(final int i, final int i2, final int i3) {
        if (this.backgroundThread == null) {
            this.mRendererStart = false;
            this.backgroundThread = new Thread(new Runnable() {
                public void run() {
                    try {
                        if (pdfRender.this.mPdfFile != null) {
                            if (i3 == 0) {
                                pdfRender.this.mPreviewmode = true;
                                pdfRender.this.mUiHandler.sendEmptyMessage(4);
                                Log.d(pdfRender.TAG, "convert4Preview :start");
                                pdfRender.this.convert4Preview(i);
                                Log.d(pdfRender.TAG, "convert4Preview :finish");
                            } else if (i3 == 1) {
                                pdfRender.this.mPreviewmode = false;
                                Log.d(pdfRender.TAG, "printing");
                                pdfRender.this.convert4Print(i, i2);
                            }
                        }
                    } catch (Exception e) {
                        Log.e(pdfRender.TAG, "GOT ERROR WHEN CONVERT: " + e);
                    }
                    Thread unused = pdfRender.this.backgroundThread = null;
                }
            });
            this.backgroundThread.start();
        }
    }

    public boolean RendererStart() {
        return this.mRendererStart;
    }

    public boolean openPdfFile(String str, String str2, Context context) {
        boolean z;
        this.mRotateCheck = false;
        this.mPdfFile = new AdobePdfContainer(context);
        this.mPdfFilename = str;
        try {
            boolean parsePDF = parsePDF(str, str2);
            z = true;
            if (!parsePDF) {
                z = false;
            }
            try {
                if (this.mPdfFile == null || z) {
                    return z;
                }
                this.mTotalPage = this.mPdfFile.CountPages();
                this.mUiHandler.sendEmptyMessage(2);
                return false;
            } catch (Exception e) {
                e = e;
                Log.e(TAG, "GOT ERROR WHEN CONVERT: " + e);
                return z;
            }
        } catch (Exception e2) {
            e = e2;
            z = false;
            Log.e(TAG, "GOT ERROR WHEN CONVERT: " + e);
            return z;
        }
    }

    public boolean canPrintableDoc() {
        return this.mPdfFile.hasPrintPermission() == 0;
    }

    public void convert4Preview(int i) {
        ExternalFileUtils.getInstance(this.mContext).initPdfDir();
        this.mIsPreviewCon = true;
        if (convertPage(i, DEF_ZOOM_PREVIEW, 0, 1)) {
            Log.d(TAG, "convertPage Preview Finish :" + i);
            try {
                Thread.sleep(200);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (this.mIsPreviewCon) {
                Bundle bundle = new Bundle();
                bundle.putInt(CommonDefine.CONVERTED_PAGE, i);
                Message message = new Message();
                message.setData(bundle);
                message.what = 0;
                this.mUiHandler.sendMessage(message);
                return;
            }
            Log.e(TAG, "Convert Preview NG : mIsPreviewCon = false");
            ExternalFileUtils.getInstance(this.mContext).initPdfDir();
        }
    }

    public void convert4Print(int i, int i2) {
        ExternalFileUtils.getInstance(this.mContext).initPrintDir();
        this.mPrintingCon = true;
        int i3 = 1;
        while (this.mPrintingCon) {
            if (i - 1 <= i2 - 1) {
                try {
                    if (this.mPrintingCon) {
                        ExternalFileUtils.getInstance(this.mContext).initPdfDir();
                        if (convertPage(i, DEF_ZOOM_PRINT, 1, i3)) {
                            this.mRendererStart = true;
                            i++;
                            i3++;
                        } else {
                            return;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.mPrintingCon = false;
            return;
        }
    }

    public boolean rotate() {
        while (!this.mRotateCheck) {
            try {
                Thread.sleep(10);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (this.mOpenError) {
                break;
            }
        }
        return this.mRotate;
    }

    private boolean convertPage(int i, float f, int i2, int i3) {
        int i4;
        AdobePdfContainer adobePdfContainer = this.mPdfFile;
        if (adobePdfContainer != null) {
            try {
                float GetPageSizeX = (float) adobePdfContainer.GetPageSizeX(i);
                float GetPageSizeY = (float) this.mPdfFile.GetPageSizeY(i);
                EPLog.i(TAG, new File(this.mPdfFilename).getName() + " - " + i + CommonDefine.SLASH + this.mTotalPage + ": " + GetPageSizeX + "x" + GetPageSizeY);
                if (i == 1) {
                    if (GetPageSizeX > GetPageSizeY) {
                        this.mRotate = true;
                    } else {
                        this.mRotate = false;
                    }
                    this.mRotateCheck = true;
                    i4 = 0;
                } else if (GetPageSizeX > GetPageSizeY) {
                    i4 = !this.mRotate ? 90 : 0;
                } else {
                    i4 = !this.mRotate ? 0 : EPImage.EPS_ROTATE_270;
                }
                int i5 = (GetPageSizeX > GetPageSizeY ? 1 : (GetPageSizeX == GetPageSizeY ? 0 : -1));
                float f2 = (float) (!this.mPreviewmode ? 4209 : 702);
                float f3 = f2 / GetPageSizeX;
                float f4 = f2 / GetPageSizeY;
                if (f3 <= f4) {
                    f4 = f3;
                }
                float f5 = GetPageSizeX * f4;
                float f6 = f4 * GetPageSizeY;
                EPLog.i(TAG, "scaledWidth = " + f5 + ",scaledHeight = " + f6);
                System.gc();
                this.mConvertedName = "";
                if (i2 != 1) {
                    Bitmap pageBitmap = this.mPdfFile.getPageBitmap(i, (int) f5, (int) f6, i4);
                    if (pageBitmap == null) {
                        this.mUiHandler.sendEmptyMessage(6);
                        return false;
                    }
                    String pdfDir = ExternalFileUtils.getInstance(this.mContext).getPdfDir();
                    this.mConvertedName = new File(pdfDir, CommonDefine.UNDER_BAR + i + ".jpg").getPath();
                    saveImageToJpg(pageBitmap, this.mConvertedName);
                    pageBitmap.recycle();
                    Log.d(TAG, "saveImageToJpg Finish : " + this.mConvertedName);
                } else {
                    String pageBitmapFile = this.mPdfFile.getPageBitmapFile(i, (int) f5, (int) f6, i4 != 0);
                    if (pageBitmapFile == null) {
                        this.mUiHandler.sendEmptyMessage(6);
                        return false;
                    }
                    String decodeJpegFilename = getDecodeJpegFilename(i);
                    new File(pageBitmapFile).renameTo(new File(decodeJpegFilename));
                    Log.d(TAG, "renameTo Finish : " + decodeJpegFilename);
                }
            } catch (Exception e) {
                Log.e(TAG, "GOT ERROR WHEN CONVERT: " + e.toString());
            }
        }
        return true;
    }

    @NonNull
    public String getDecodeJpegFilename(int i) {
        String printDir = ExternalFileUtils.getInstance(this.mContext).getPrintDir();
        return new File(printDir, CommonDefine.UNDER_BAR + i + ".jpg").getPath();
    }

    private void saveImageToJpg(Bitmap bitmap, String str) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(str));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            fileOutputStream.write(byteArrayOutputStream.toByteArray());
            fileOutputStream.flush();
            fileOutputStream.close();
            Log.i(TAG, str + " converted");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean parsePDF(String str, String str2) {
        try {
            long length = new File(str).length();
            if (length == 0) {
                Log.e(TAG, "file is empty");
                this.mUiHandler.sendEmptyMessage(6);
                return false;
            }
            Log.i(TAG, "file: " + this.mPdfFilename + "has" + length + "byte");
            openFile(str, str2);
            return false;
        } catch (AdobePdfContainer.PasswordException unused) {
            Log.i(TAG, "Password Locked");
            return true;
        } catch (Throwable th) {
            th.printStackTrace();
            this.mUiHandler.sendEmptyMessage(6);
            this.mOpenError = true;
            return false;
        }
    }

    public void openFile(String str, String str2) throws AdobePdfContainer.PasswordException, AdobePdfContainer.NoPdfException {
        this.mPdfFile.LoadDocument(str, str2);
    }

    private byte[] readBytes(File file) throws IOException {
        int length = (int) file.length();
        byte[] bArr = new byte[length];
        FileInputStream fileInputStream = new FileInputStream(file);
        int i = 0;
        int read = fileInputStream.read(bArr, 0, length + 0);
        while (read > 0) {
            i += read;
            read = fileInputStream.read(bArr, i, length - i);
        }
        return bArr;
    }

    public boolean isFileExit() {
        return this.mPdfFile != null;
    }

    public int totalPages() {
        AdobePdfContainer adobePdfContainer = this.mPdfFile;
        if (adobePdfContainer != null) {
            return adobePdfContainer.CountPages();
        }
        return 0;
    }

    public String getConvertedPagePrint() {
        return this.mConvertedNamePrint;
    }

    public String getConvertedPagePreview() {
        return this.mConvertedName;
    }

    public void setPrintingStt(boolean z) {
        try {
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.mPrintingCon = z;
        if (!z) {
            while (this.backgroundThread != null && this.backgroundThread.isAlive()) {
                try {
                    Thread.sleep(2000);
                    EPLog.d("Convert Preview", "convert4Print shutdown...");
                } catch (InterruptedException | NullPointerException unused) {
                    return;
                }
            }
        }
    }

    public void setMIsPreviewCon(boolean z) {
        this.mIsPreviewCon = z;
    }
}
