package epson.print.service;

import android.graphics.BitmapFactory;

import com.epson.mobilephone.common.PrintingLib.define.Constants;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingDeque;

import epson.common.ImageUtil;
import epson.common.Info_paper;
import epson.print.CommonDefine;
import epson.print.EPImage;
import epson.print.EPImageList;
import epson.print.EPImageUtil;
import epson.print.pdf.AreaPdfRenderer;

class PdfRenderThread extends Thread {
    private static final String TAG = "PdfRenderThread";
    private LinkedBlockingDeque<Integer> mDeque;
    private EPImageList mEpImageList;
    private boolean mIsLandscape;
    private final int mLayoutMulti;
    private String mOriginalFilename;
    private ArrayList<Integer> mPageList;
    private String mPassword;
    private String mPdfFilename;
    private AreaPdfRenderer mPdfRenderer;
    private int[] mPrintRange;
    private PrintService mPrintService;
    private int[] mPrintableArea;

    public PdfRenderThread(PrintService printService, AreaPdfRenderer areaPdfRenderer, String str, String str2, String str3, boolean z, LinkedBlockingDeque<Integer> linkedBlockingDeque, int[] iArr, int[] iArr2, int i) {
        super("pdf-rendering");
        if (printService == null || areaPdfRenderer == null || linkedBlockingDeque == null || iArr == null || iArr.length < 2) {
            throw new IllegalArgumentException();
        }
        mPrintService = printService;
        mDeque = linkedBlockingDeque;
        mPrintableArea = iArr;
        mPrintRange = iArr2;
        mPassword = str3;
        mIsLandscape = z;
        mPdfRenderer = areaPdfRenderer;
        mPdfFilename = str;
        mOriginalFilename = str2 != null ? str2 : str;
        mLayoutMulti = i;
    }

    public void createEpImageList() {
        String str = mPdfFilename;
        int[] iArr = mPrintRange;
        mPageList = new ArrayList<>();
        mEpImageList = new EPImageList();
        if (this.mPdfRenderer.openPdfFile(str, mPassword, mPrintService.getLocalApplicationContext())) {
            int i = 0;
            int i2 = (iArr == null || iArr.length < 1) ? 1 : iArr[0];
            int i3 = mPdfRenderer.totalPages();
            if (iArr != null && iArr.length >= 2) {
                i3 = iArr[1];
            }
            while (i2 <= i3) {
                mPageList.add(Integer.valueOf(i2));
                EPImage ePImage = new EPImage();
                ePImage.index = i;
                ePImage.loadImageFileName = getPdfDecodeFilename(i2).getPath();
                ePImage.setOrgName(this.mOriginalFilename);
                mEpImageList.add(ePImage);
                i2++;
                i++;
            }
            return;
        }
        throw new IllegalStateException();
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public File getPdfDecodeFilename(int i) {
        return AreaPdfRenderer.getPdfDecodeFilename(this.mPrintService.getLocalApplicationContext(), i);
    }

    public EPImageList getImageList() {
        return mEpImageList;
    }

    public void run() {
        renderPdf();
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public boolean renderPdf() {
        AreaPdfRenderer areaPdfRenderer = mPdfRenderer;
        if (areaPdfRenderer == null || mEpImageList == null || mDeque == null) {
            throw new IllegalStateException();
        }
        try {
            boolean isPageLandscape = areaPdfRenderer.isPageLandscape(1);
            boolean z = isPageLandscape == mIsLandscape;
            int[] limitAreaSize = getLimitAreaSize(this.mPrintableArea);
            for (int i = 0; i < mPageList.size(); i++) {
                int intValue = mPageList.get(i).intValue();
                if (this.mPrintService.getCancelPrinting()) {
                    return false;
                }
                boolean isPageLandscape2 = mPdfRenderer.isPageLandscape(intValue);
                int[] portraitOrLandscapeSize = AreaPdfRenderer.getPortraitOrLandscapeSize(limitAreaSize, isPageLandscape2 != z);
                EPImage ePImage = mEpImageList.get(i);
                if (!this.mPdfRenderer.convertPageForPrint(ePImage.loadImageFileName, intValue, portraitOrLandscapeSize)) {
                    return false;
                }
                if (isPageLandscape != isPageLandscape2) {
                    epImageRotate(ePImage, getRotate(isPageLandscape));
                }
                int[] localGetImageSize = localGetImageSize(ePImage.loadImageFileName);
                setEPImage(ePImage, localGetImageSize, getPreviewAreaSize(this.mPrintableArea, z, localGetImageSize));
                mDeque.offerLast(Integer.valueOf(i));
            }
            return true;
        } catch (Exception unused) {
            return false;
        }
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    @NonNull
    public int[] getPreviewAreaSize(@NonNull int[] iArr, boolean z, @NonNull int[] iArr2) {
        boolean z2 = false;
        if ((iArr2[0] <= iArr2[1]) == z) {
            z2 = true;
        }
        return AreaPdfRenderer.getCircumscribedSize(iArr2, AreaPdfRenderer.getPortraitOrLandscapeSize(iArr, z2));
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public int[] getLimitAreaSize(@NonNull int[] iArr) {
        int[] portraitOrLandscapeSize = AreaPdfRenderer.getPortraitOrLandscapeSize(iArr, true);
        int[] portraitOrLandscapeSize2 = AreaPdfRenderer.getPortraitOrLandscapeSize(getMaxAreaSize(), true);
        return new int[]{Math.min(portraitOrLandscapeSize[0], portraitOrLandscapeSize2[0]), Math.min(portraitOrLandscapeSize[1], portraitOrLandscapeSize2[1])};
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public int[] getMaxAreaSize() {
        Info_paper infoPaper = Info_paper.getInfoPaper(this.mPrintService.getLocalApplicationContext(), Constants.PaperName.EPS_MSID_A3.getCode());
        return new int[]{infoPaper.getPaper_width_boder(), infoPaper.getPaper_height_boder()};
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void setEPImage(@NonNull EPImage ePImage, @NonNull int[] iArr, @NonNull int[] iArr2) {
        ePImage.srcWidth = iArr[0];
        ePImage.srcHeight = iArr[1];
        ePImage.isPaperLandScape = mIsLandscape;
        ePImage.previewPaperRectTop = 0;
        ePImage.previewPaperRectLeft = 0;
        ePImage.previewPaperRectRight = iArr2[0];
        ePImage.previewPaperRectBottom = iArr2[1];
        copyPreviewPaperRectToPreviewImageRect(ePImage);
        ePImage.previewWidth = (int) (ePImage.previewImageRectRight - ePImage.previewImageRectLeft);
        ePImage.previewHeight = (int) (ePImage.previewImageRectBottom - ePImage.previewImageRectTop);
    }

    private void copyPreviewPaperRectToPreviewImageRect(@NonNull EPImage ePImage) {
        ePImage.previewImageRectLeft = (float) ePImage.previewPaperRectLeft;
        ePImage.previewImageRectTop = (float) ePImage.previewPaperRectTop;
        ePImage.previewImageRectRight = (float) ePImage.previewPaperRectRight;
        ePImage.previewImageRectBottom = (float) ePImage.previewPaperRectBottom;
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    @NonNull
    public int[] localGetImageSize(@NonNull String str) {
        BitmapFactory.Options imageSize2 = ImageUtil.getImageSize2(str);
        return new int[]{imageSize2.outWidth, imageSize2.outHeight};
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public int getRotate(boolean z) {
        if (this.mLayoutMulti != 65536) {
            if (z) {
                return EPImage.EPS_ROTATE_270;
            }
            return 90;
        } else if (!z) {
            return EPImage.EPS_ROTATE_270;
        } else {
            return 90;
        }
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void epImageRotate(@NonNull EPImage ePImage, int i) throws IOException {
        String str = ePImage.loadImageFileName;
        try {
            String rotateFilename = getRotateFilename(str);
            checkFileExists(rotateFilename);
            rotateImage(str, rotateFilename, i);
            ePImage.loadImageFileName = rotateFilename;
        } finally {
            deleteFile(str);
        }
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void rotateImage(@NonNull String str, @NonNull String str2, int i) throws IOException {
        EPImageUtil ePImageUtil = new EPImageUtil();
        String str3 = str + ".bmp";
        if (ePImageUtil.jpg2bmp(str, str3, 1) != 0) {
            throw new IOException();
        } else if (ePImageUtil.rotateImage(str3, str2, i) != 0) {
            throw new IOException();
        }
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void checkFileExists(String str) throws IOException {
        if (new File(str).exists()) {
            throw new IOException("rotate out file '" + str + "] exists");
        }
    }

    /* access modifiers changed from: package-private */
    @VisibleForTesting
    public void deleteFile(String str) throws IOException {
        new File(str).delete();
    }

    @VisibleForTesting
    static String getRotateFilename(String str) {
        int lastIndexOf = str.lastIndexOf(CommonDefine.DOT);
        if (lastIndexOf < 0) {
            lastIndexOf = str.length();
        }
        return str.substring(0, lastIndexOf) + "-90" + ".bmp";
    }
}
