package epson.print.phlayout;


import com.epson.cameracopy.printlayout.ImageAndLayout;

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;

import java.io.File;

import epson.common.ExternalFileUtils;
import epson.print.EPImage;
import epson.print.IprintApplication;

public class AltImageProcessing {
    @Nullable
    public String createPreviewImage(EPImage ePImage, int i, int i2, int i3, boolean z) {
        if (!z && ePImage.previewImageFileName != null) {
            return ePImage.previewImageFileName;
        }
        if (i <= 0 || i2 <= 0) {
            throw new IllegalArgumentException("width x height <" + i + PreferencesConstants.COOKIE_DELIMITER + i2 + ">");
        }
        ePImage.decodeImageFileName = null;
        ePImage.previewImageFileName = null;
        ePImage.decodeWidth = 0;
        ePImage.decodeHeight = 0;
        int[] imageSize = getImageSize(ePImage.loadImageFileName);
        if (imageSize[0] <= 0 || imageSize[1] <= 0) {
            return null;
        }
        int[] gaisetsuSize = getGaisetsuSize(new int[]{i, i2}, imageSize);
        if (gaisetsuSize[0] <= 0 || gaisetsuSize[1] <= 0) {
            return null;
        }
        String previewImageFileName = getPreviewImageFileName(ePImage.index);
        localResizeFile(previewImageFileName, ePImage.loadImageFileName, gaisetsuSize, i3 == 1);
        int[] imageSize2 = getImageSize(previewImageFileName);
        if (imageSize2[0] <= 0 || imageSize2[1] <= 0) {
            return null;
        }
        ePImage.decodeWidth = imageSize2[0];
        ePImage.decodeHeight = imageSize2[1];
        ePImage.previewWidth = imageSize2[0];
        ePImage.previewHeight = imageSize2[1];
        ePImage.previewImageFileName = previewImageFileName;
        return previewImageFileName;
    }

    private void localResizeFile(@NonNull String str, @NonNull String str2, @NonNull int[] iArr, boolean z) {
        String str3 = str2;
        int[] imageSize = getImageSize(str3);
        ImageAndLayout imageAndLayout = new ImageAndLayout();
        imageAndLayout.setOrgFileName(str3);
        imageAndLayout.setLayoutAreaSize(imageSize[0], imageSize[1]);
        imageAndLayout.setLayout((double) imageSize[0], (double) imageSize[1], 0.0d, 0.0d, 0);
        imageAndLayout.setOpenCvExifRotationCancel(false);
        String str4 = str;
        imageAndLayout.createPrintData(str, z, iArr, 0);
    }

    @NonNull
    public int[] getGaisetsuSize(@NonNull int[] iArr, @NonNull int[] iArr2) {
        double d = ((double) iArr[0]) / ((double) iArr2[0]);
        double d2 = ((double) iArr[1]) / ((double) iArr2[1]);
        if (d > d2) {
            return new int[]{iArr[0], (int) (d * ((double) iArr2[1]))};
        }
        return new int[]{(int) (d2 * ((double) iArr2[0])), iArr[1]};
    }

    @NonNull
    public int[] getImageSize(@NonNull String str) {
        Mat imread = Imgcodecs.imread(str, -1);
        Size size = imread.size();
        imread.release();
        return new int[]{(int) size.width, (int) size.height};
    }

    @NonNull
    public String getPreviewImageFileName(int i) {
        String tempViewDir = ExternalFileUtils.getInstance(IprintApplication.getInstance()).getTempViewDir();
        return new File(tempViewDir, "preview_" + (i + 1) + ".bmp").getPath();
    }
}
