package com.epson.iprint.apf;

import android.content.Context;

import java.io.File;
import java.util.Iterator;

import epson.print.EPImage;
import epson.print.EPImageList;
import epson.print.service.PrintService;

public class ApfEpImageAdapter {

    public interface ProgressCallback {
        boolean updateApfProgress(int i);
    }

    public static EPImageList getApfImageList(Context context, EPImageList ePImageList, PrintService printService) {
        int i;
        EPImage ePImage;
        String str;
        EPImageList ePImageList2 = ePImageList;
        PrintService printService2 = printService;
        if (ePImageList2 == null || ePImageList2.imageList == null) {
            return null;
        }
        ApfAdapter apfAdapter = new ApfAdapter(context);
        EPImageList ePImageList3 = new EPImageList();
        int size = ePImageList2.imageList.size();
        boolean z = ePImageList2.apfModeInPrinting == 1;
        boolean z2 = ePImageList.getSharpness() == 1;
        boolean z3 = ePImageList.getClearlyVivid() == 1;
        Iterator<EPImage> it = ePImageList2.imageList.iterator();
        int i2 = 0;
        while (it.hasNext()) {
            EPImage next = it.next();
            if (printService2 != null && !printService2.updateApfProgress((i2 * 100) / size)) {
                return null;
            }
            String apfFilename = apfAdapter.getApfFilename(next.loadImageFileName, ePImageList2.apfModeInPrinting, ePImageList.getSharpness(), ePImageList.getClearlyVivid());
            try {
                if (!new File(apfFilename).exists()) {
                    str = apfFilename;
                    ePImage = next;
                    i = i2;
                    if (!apfAdapter.processApf(next.loadImageFileName, apfFilename, z, z2, z3)) {
                        return null;
                    }
                } else {
                    str = apfFilename;
                    ePImage = next;
                    i = i2;
                }
                String str2 = str;
                int i3 = i;
                EPImage ePImage2 = new EPImage(str2, i3);
                i2 = i3 + 1;
                EPImage ePImage3 = ePImage;
                copyPositionParams(ePImage3, ePImage2);
                ePImage2.setOrgName(ePImage3.loadImageFileName);
                ePImage2.loadImageFileName = str2;
                ePImageList3.add(ePImage2);
            } catch (OutOfMemoryError unused) {
                return null;
            } catch (Exception unused2) {
                return null;
            }
        }
        if (!printService2.updateApfProgress(100)) {
            return null;
        }
        return ePImageList3;
    }

    private static void copyPositionParams(EPImage ePImage, EPImage ePImage2) {
        ePImage2.previewPaperRectLeft = ePImage.previewPaperRectLeft;
        ePImage2.previewPaperRectTop = ePImage.previewPaperRectTop;
        ePImage2.previewPaperRectRight = ePImage.previewPaperRectRight;
        ePImage2.previewPaperRectBottom = ePImage.previewPaperRectBottom;
        ePImage2.previewImageRectLeft = ePImage.previewImageRectLeft;
        ePImage2.previewImageRectTop = ePImage.previewImageRectTop;
        ePImage2.previewImageRectRight = ePImage.previewImageRectRight;
        ePImage2.previewImageRectBottom = ePImage.previewImageRectBottom;
        ePImage2.previewWidth = ePImage.previewWidth;
        ePImage2.previewHeight = ePImage.previewHeight;
        ePImage2.isPaperLandScape = ePImage.isPaperLandScape;
        ePImage2.scaleFactor = ePImage.scaleFactor;
        ePImage2.previewImageRectCenterX = ePImage.previewImageRectCenterX;
        ePImage2.previewImageRectCenterY = ePImage.previewImageRectCenterY;
    }
}
