package epson.print.copy.Component.ecopycomponent;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.AsyncTask;
import epson.print.copy.Component.ecopycomponent.ECopyComponent;
import epson.print.copy.Component.ecopycomponent.ECopyOptionItem;
import epson.print.copy.Component.ecopycomponent.ECopyPreview;
import epson.print.copy.Component.escandevice.EScanDevice;
import epson.print.copy.Component.escandevice.IScanController;
import java.util.HashMap;

class PreviewScanTask extends AsyncTask<Void, Void, Void> implements IScanController, ECopyComponent.ITask {
    boolean cancelRequested;
    Canvas canvas;
    ECopyPreview copyPreview;
    ECopyOptionContext optionContext;
    Bitmap previewScanBitmap;
    HashMap<Integer, Object> properties = new HashMap<>();
    float scale;
    ECopyComponent.ICopyStatusListener statusListener;
    ECopyComponent.ICopySystemSettings systemSettings;

    public void setClientID(String str) {
    }

    public void setRequestConnectionTimeout(int i) {
    }

    public PreviewScanTask(ECopyPreview eCopyPreview, ECopyComponent.ICopyStatusListener iCopyStatusListener) {
        this.copyPreview = eCopyPreview;
        this.statusListener = iCopyStatusListener;
    }

    private void setPropertyValue(int i, Object obj) {
        this.properties.put(Integer.valueOf(i), obj);
    }

    public ECopyComponent.ICopyCancelRequest start() {
        super.execute(new Void[0]);
        return new ECopyComponent.ICopyCancelRequest() {
            public void cancel() {
                synchronized (this) {
                    PreviewScanTask.this.cancelRequested = true;
                }
            }
        };
    }

    public void setSystemSettings(ECopyComponent.ICopySystemSettings iCopySystemSettings) {
        this.systemSettings = iCopySystemSettings;
    }

    public void setOptionContext(ECopyOptionContext eCopyOptionContext) {
        int i;
        this.optionContext = eCopyOptionContext;
        if (this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.XScale).getSelectedChoice() != ECopyOptionItem.ECopyOptionItemChoice.XScale_Autofit) {
            ECopyOptionItem copyOptionItemOf = this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.PrintMediaSize);
            switch (copyOptionItemOf.getSelectedChoice()) {
                case PrintMediaSize_A4:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_A4;
                    break;
                case PrintMediaSize_B5:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_B5;
                    break;
                case PrintMediaSize_L:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_L;
                    break;
                case PrintMediaSize_2L:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_2L;
                    break;
                case PrintMediaSize_Postcard:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_HAGAKI;
                    break;
                case PrintMediaSize_KG:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_KG;
                    break;
                case PrintMediaSize_8x10in:
                    i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_6GIRI;
                    break;
            }
        }
        i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_AUTOFIT;
        ECopyComponent.ECopyType copyType = this.optionContext.getCopyType();
        if (copyType == ECopyComponent.ECopyType.A4_2up || copyType == ECopyComponent.ECopyType.A4_2up_Book) {
            i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_A4;
        } else if (copyType == ECopyComponent.ECopyType.B5_2up || copyType == ECopyComponent.ECopyType.B5_2up_Book) {
            i = IScanController.CPPREV_VALUE_SCAN_COPYFRAME_B5;
        }
        setPropertyValue(3, Integer.valueOf(i));
        setPropertyValue(1, 65536);
        setPropertyValue(2, Integer.valueOf(IScanController.CPPREV_VALUE_SCAN_RESO_PREVIEW));
    }

    protected Void doInBackground(Void... voidArr) {
        setPropertyValue(4, this.systemSettings.getPrinterIPAddress());
        EScanDevice.nativeStartScan(this);
        return null;
    }

    public int getIntegerValue(int i) {
        return ((Integer) this.properties.get(Integer.valueOf(i))).intValue();
    }

    public String getStringValue(int i) {
        return (String) this.properties.get(Integer.valueOf(i));
    }

    public boolean isCancelRequested() {
        boolean z;
        synchronized (this) {
            z = this.cancelRequested;
        }
        return z;
    }

    public void setScanStatusChanged(final int i, final int i2) {
        ((Activity) this.copyPreview.getContext()).runOnUiThread(new Runnable() {
            public void run() {
                ECopyComponent.ICopyStatusListener.CopyTaskResult copyTaskResult;
                switch (i) {
                    case 1:
                        PreviewScanTask previewScanTask = PreviewScanTask.this;
                        previewScanTask.previewScanBitmap = null;
                        previewScanTask.statusListener.onStarted(ECopyComponent.ICopyStatusListener.CopyTaskType.Preview);
                        return;
                    case 2:
                        int i = i2;
                        if (i != 3) {
                            switch (i) {
                                case 0:
                                    copyTaskResult = ECopyComponent.ICopyStatusListener.CopyTaskResult.Succeed;
                                    break;
                                case 1:
                                    copyTaskResult = ECopyComponent.ICopyStatusListener.CopyTaskResult.Canceled;
                                    break;
                                default:
                                    copyTaskResult = ECopyComponent.ICopyStatusListener.CopyTaskResult.ErrorOther;
                                    break;
                            }
                        } else {
                            copyTaskResult = ECopyComponent.ICopyStatusListener.CopyTaskResult.Busy;
                        }
                        PreviewScanTask.this.statusListener.onFinished(ECopyComponent.ICopyStatusListener.CopyTaskType.Preview, copyTaskResult);
                        return;
                    case 3:
                        PreviewScanTask.this.previewScanBitmap.eraseColor(-1);
                        return;
                    case 4:
                        PreviewScanTask.this.copyPreview.drawScanPreview(PreviewScanTask.this.previewScanBitmap);
                        return;
                    default:
                        return;
                }
            }
        });
    }

    public void setScanImageSize(int i, int i2) {
        this.copyPreview.calculateScanImageSize(i, i2, new ECopyPreview.ScanImageSizeCalculator() {
            public void onScanImageSize(int i, int i2, float f) {
                PreviewScanTask previewScanTask = PreviewScanTask.this;
                previewScanTask.scale = f;
                previewScanTask.previewScanBitmap = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888);
                PreviewScanTask previewScanTask2 = PreviewScanTask.this;
                previewScanTask2.canvas = new Canvas(previewScanTask2.previewScanBitmap);
            }
        });
    }

    public void setScanImageBlockReceived(int[] iArr, int i, int i2, int i3, int i4) {
        Bitmap createBitmap = Bitmap.createBitmap(iArr, i3, i4, Bitmap.Config.ARGB_8888);
        Matrix matrix = new Matrix();
        float f = this.scale;
        matrix.postScale(f, f);
        float f2 = this.scale;
        matrix.postTranslate(((float) i) * f2, ((float) i2) * f2);
        this.canvas.drawBitmap(createBitmap, matrix, (Paint) null);
    }

    public void setCopyFrameSize(int i, int i2, int i3, int i4) {
        RectF rectF = new RectF((float) i, (float) i2, (float) i3, (float) i4);
        synchronized (this) {
            if (this.cancelRequested) {
                rectF = new RectF();
            }
        }
        this.copyPreview.setPaperFrame(rectF);
    }
}
