package com.epson.iprint.apf;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;

import com.epson.iprint.apf.ImageSizeConversionCache;
import epson.print.EPImage;
import epson.print.EPImageCreator;
import epson.print.EPImageList;
import epson.print.IprintApplication;
import epson.print.phlayout.PhotoPreview;
import epson.print.phlayout.PhotoPreviewImageList;
import java.io.File;

public class ApfPreviewView implements PhotoPreview.PreviewBitmapMaker {
    public static final String APF_RESIZE_FILE_PREFIX = "rsapf_";
    protected ApfAdapter mApfAdapter;
    private int mApfMode;
    protected Context mApplicationContext;
    private int mClearlyVividMode;
    protected PhotoPreview mEPPreviewImageView;
    private ImageSizeConversionCache mResizeCache = new ImageSizeConversionCache();
    private int mSharpnessValue;

    public void setZoomControlHandler(Handler handler) {
        this.mEPPreviewImageView.setZoomControlHandler(handler);
    }

    public void init(Context context, PhotoPreview photoPreview) {
        this.mApplicationContext = context.getApplicationContext();
        this.mApfAdapter = new ApfAdapter(this.mApplicationContext);
        this.mEPPreviewImageView = photoPreview;
        this.mEPPreviewImageView.setPreviewBitmapMaker(this);
    }

    public static void deleteWorkingDirectory(Context context) {
        ApfAdapter.clearDirectory(context);
    }

    public void invalidate() {
        this.mEPPreviewImageView.invalidate();
    }

    public void setIsPaperLandscape(boolean z, Activity activity) {
        this.mEPPreviewImageView.setIsPaperLandscape(z, activity);
    }

    public boolean getIsPaperLandscape() {
        return this.mEPPreviewImageView.getIsPaperLandscape();
    }

    public void setDrawEndHandler(Handler handler) {
        this.mEPPreviewImageView.setDrawEndHandler(handler);
    }

    public void postInvalidate() {
        this.mEPPreviewImageView.postInvalidate();
    }

    public void setPreviewImageView(PhotoPreview photoPreview) {
        this.mEPPreviewImageView = photoPreview;
    }

    public void setApfLibParams(int i, int i2, int i3) {
        this.mApfMode = i;
        this.mSharpnessValue = i2;
        this.mClearlyVividMode = i3;
    }

    public int getApfMode() {
        return this.mApfMode;
    }

    public int getSharpnessValue() {
        return this.mSharpnessValue;
    }

    public int getClearlyVividMode() {
        return this.mClearlyVividMode;
    }

    public void setImageList(@NonNull PhotoPreviewImageList photoPreviewImageList) {
        this.mEPPreviewImageView.setImageList(photoPreviewImageList);
    }

    public EPImageList getImageList() {
        return this.mEPPreviewImageView.getImageList();
    }

    public void setLayout(int i, int i2) {
        this.mEPPreviewImageView.setLayout(i, i2);
    }

    public boolean setPaper(int i) {
        return this.mEPPreviewImageView.setPaper(i);
    }

    public void setColor(int i) {
        this.mEPPreviewImageView.setColor(i);
    }

    public void invalidateImageNo() {
        this.mEPPreviewImageView.invalidateImageNo();
    }

    public void invalidatePreview() {
        this.mResizeCache.invalidateCache();
        this.mEPPreviewImageView.invalidatePreview();
    }

    public boolean setImage(int i, int i2, Activity activity) throws OutOfMemoryError {
        return this.mEPPreviewImageView.setImage(i, i2, activity);
    }

    public Bitmap getPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z) {
        String filename;
        if (this.mApfMode == 0 && this.mSharpnessValue == 0) {
            return null;
        }
        String apfResult = this.mApfAdapter.getApfResult(ePImage.loadImageFileName, this.mApfMode, this.mSharpnessValue, this.mClearlyVividMode);
        if (apfResult == null) {
            throw new OutOfMemoryError("in getPreviewBitmap()");
        } else if (z || (filename = this.mResizeCache.getFilename(apfResult, new ImageSizeConversionCache.ConvertCondition(ePImage.previewWidth, ePImage.previewHeight, i3))) == null) {
            Bitmap bitmapConventionally = getBitmapConventionally(ePImage, apfResult, i, i2, i3);
            if (bitmapConventionally != null) {
                return bitmapConventionally;
            }
            throw new OutOfMemoryError("in getPreviewBitmap()");
        } else {
            Bitmap decodeFile = BitmapFactory.decodeFile(filename);
            if (decodeFile != null) {
                return decodeFile;
            }
            throw new OutOfMemoryError("in getPreviewBitmap()");
        }
    }

    protected Bitmap getBitmapConventionally(EPImage ePImage, String str, int i, int i2, int i3) {
        EPImage ePImage2 = new EPImage(str, 0);
        new EPImageCreator(IprintApplication.getInstance()).createPreviewImage(ePImage2, i, i2, i3, true);
        if (ePImage2.previewImageFileName == null) {
            return null;
        }
        File file = new File(ePImage2.previewImageFileName);
        if (!file.canRead()) {
            return null;
        }
        ePImage.srcWidth = ePImage2.srcWidth;
        ePImage.srcHeight = ePImage2.srcHeight;
        ePImage.decodeWidth = ePImage2.decodeWidth;
        ePImage.decodeHeight = ePImage2.decodeHeight;
        ePImage.previewWidth = ePImage2.previewWidth;
        ePImage.previewHeight = ePImage2.previewHeight;
        ePImage.previewImageFileName = null;
        Bitmap decodeFile = BitmapFactory.decodeFile(ePImage2.previewImageFileName);
        File apfResizeCacheFile = getApfResizeCacheFile(file, str);
        if (file.renameTo(apfResizeCacheFile)) {
            this.mResizeCache.setFilename(str, new ImageSizeConversionCache.ConvertCondition(ePImage.previewWidth, ePImage.previewHeight, i3), apfResizeCacheFile.toString());
        }
        return decodeFile;
    }

    private File getApfResizeCacheFile(File file, String str) {
        File parentFile = file.getParentFile();
        String name = new File(str).getName();
        return new File(parentFile, APF_RESIZE_FILE_PREFIX + name);
    }
}
