package epson.scan.i2lib;

import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;

public class ImageProcessController {
    private static final String LOG_TAG = "ImageProcessController";
    private boolean mBackSideRotate180;
    private int mBlackAndWhiteThresh;
    private LinkedBlockingDeque<ImageProcessInfo> mBlockingDeque = new LinkedBlockingDeque<>();
    private volatile boolean mCancelRequested;
    private boolean mConvertBlackAndWhite;
    private boolean mDuplex;
    private boolean mError;
    private I2ScanTask mI2ScanTask;
    private String[] mImageProcessOutFilenameFormat;
    private ImageProcessThread mImageProcessThread;
    private final CountDownLatch mProcessEndLatch = new CountDownLatch(1);
    private String[] mScanOutFilenameFormat;

    public ImageProcessController(@NonNull I2ScanTask i2ScanTask, boolean z, int i, int i2, boolean z2, int i3) {
        this.mI2ScanTask = i2ScanTask;
        this.mDuplex = z;
        this.mConvertBlackAndWhite = z2;
        this.mBlackAndWhiteThresh = i3;
        setBackSideRotation(i, i2);
    }

    private void setBackSideRotation(int i, int i2) {
        if (this.mDuplex) {
            boolean z = true;
            if (i2 != 0) {
                if (i != 1) {
                    z = false;
                }
                this.mBackSideRotate180 = z;
                return;
            }
            if (i != 0) {
                z = false;
            }
            this.mBackSideRotate180 = z;
            return;
        }
        this.mBackSideRotate180 = false;
    }

    /* access modifiers changed from: package-private */
    @NonNull
    public ArrayList<String> getScanFiles(int[] iArr) {
        int i = this.mDuplex ? 2 : 1;
        ArrayList<String> arrayList = new ArrayList<>();
        int i2 = 0;
        while (true) {
            for (int i3 = 0; i3 < i; i3++) {
                if (i2 >= iArr[i3]) {
                    return arrayList;
                }
                File allProcessFile = getAllProcessFile(i3, i2);
                if (!allProcessFile.canRead()) {
                    return arrayList;
                }
                arrayList.add(allProcessFile.getPath());
            }
            i2++;
        }
    }

    public void init(File file, Calendar calendar) {
        makeScanFilenameFormat(file, calendar);
    }

    public void startThread() {
        checkStatus();
        this.mImageProcessThread = new ImageProcessThread(this, this.mBlackAndWhiteThresh);
        this.mImageProcessThread.start();
    }

    public void cancelThread() {
        this.mCancelRequested = true;
        if (this.mImageProcessThread == null) {
            this.mProcessEndLatch.countDown();
            return;
        }
        this.mBlockingDeque.clear();
        try {
            this.mBlockingDeque.putFirst(ImageProcessInfo.getEndRequestInfo());
        } catch (InterruptedException unused) {
            this.mProcessEndLatch.countDown();
        }
    }

    public void requestFinishThread() {
        checkThreadStatus();
        try {
            this.mBlockingDeque.putLast(ImageProcessInfo.getEndRequestInfo());
        } catch (InterruptedException unused) {
            this.mProcessEndLatch.countDown();
        }
    }

    public boolean waitUntilThreadEnd() {
        try {
            this.mProcessEndLatch.await();
            return !this.mError;
        } catch (InterruptedException unused) {
            return false;
        }
    }

    @NonNull
    public String[] getScanOutFilenameFormat() {
        checkStatus();
        return this.mScanOutFilenameFormat;
    }

    public File getAllProcessFile(int i, int i2) {
        checkStatus();
        return getFile((getProcessType(i, i2) == ImageProcessInfo.PROCESS_TYPE_EMPTY ? this.mScanOutFilenameFormat : this.mImageProcessOutFilenameFormat)[i], i2);
    }

    private static File getFile(String str, int i) {
        return new File(String.format(Locale.US, str, new Object[]{Integer.valueOf(i)}));
    }

    private void checkStatus() {
        if (this.mScanOutFilenameFormat == null) {
            throw new IllegalStateException("init() is not called");
        }
    }

    private void checkThreadStatus() {
        if (this.mImageProcessThread == null) {
            throw new IllegalStateException("startThread() is not called");
        }
    }

    private void makeScanFilenameFormat(@NonNull File file, @NonNull Calendar calendar) {
        File file2 = new File(file, geDateFormat(calendar) + "a_%d.jpg");
        this.mScanOutFilenameFormat = new String[]{file2.toString(), new File(file, geDateFormat(calendar) + "b_%d.jpg").toString()};
        File file3 = new File(file, geDateFormat(calendar) + "a2_%d.jpg");
        this.mImageProcessOutFilenameFormat = new String[]{file3.toString(), new File(file, geDateFormat(calendar) + "b2_%d.jpg").toString()};
    }

    @VisibleForTesting
    @NonNull
    static String geDateFormat(@NonNull Calendar calendar) {
        return new SimpleDateFormat("MMdd_HHmmss_SSS", Locale.US).format(calendar.getTime());
    }

    public boolean putScanResult(int i, int i2) {
        String str;
        checkThreadStatus();
        if (this.mCancelRequested) {
            return true;
        }
        int processType = getProcessType(i, i2);
        String path = getFile(this.mScanOutFilenameFormat[i], i2).getPath();
        if (processType == ImageProcessInfo.PROCESS_TYPE_EMPTY) {
            str = null;
        } else {
            str = getAllProcessFile(i, i2).getPath();
        }
        try {
            this.mBlockingDeque.putLast(new ImageProcessInfo(processType, path, str, i, i2));
            return true;
        } catch (InterruptedException unused) {
            this.mProcessEndLatch.countDown();
            return false;
        }
    }

    public ImageProcessInfo checkNexImageProcessRequest() throws InterruptedException {
        return this.mBlockingDeque.takeFirst();
    }

    public void putImageProcessEnd(@NonNull ImageProcessInfo imageProcessInfo, boolean z) {
        if (imageProcessInfo.endRequested() || !z) {
            this.mError = !z;
            this.mProcessEndLatch.countDown();
            return;
        }
        this.mI2ScanTask.onImageProcessEnd(imageProcessInfo.getSide(), imageProcessInfo.getPage());
    }

    private int getProcessType(int i, int i2) {
        int i3 = ImageProcessInfo.PROCESS_TYPE_EMPTY;
        if (i == 1 && this.mBackSideRotate180) {
            i3 |= ImageProcessInfo.PROCESS_TYPE_ROTATE_180;
        }
        return this.mConvertBlackAndWhite ? i3 | ImageProcessInfo.PROCESS_TYPE_BLACK_AND_WHITE : i3;
    }

    public static class ImageProcessInfo {
        public static int PROCESS_TYPE_BLACK_AND_WHITE = 32;
        public static int PROCESS_TYPE_EMPTY = 0;
        public static int PROCESS_TYPE_END = 1;
        public static int PROCESS_TYPE_ROTATE_180 = 16;
        private String mInputFilename;
        private String mOutputFilename;
        private int mPage;
        private int mProcessType;
        private int mSide;

        @NonNull
        public static ImageProcessInfo getEndRequestInfo() {
            return new ImageProcessInfo(PROCESS_TYPE_END, (String) null, (String) null, -1, -1);
        }

        public ImageProcessInfo(int i, String str, String str2, int i2, int i3) {
            this.mProcessType = i;
            this.mInputFilename = str;
            this.mOutputFilename = str2;
            this.mSide = i2;
            this.mPage = i3;
        }

        public String getInputFilename() {
            return this.mInputFilename;
        }

        public String getOutputFilename() {
            return this.mOutputFilename;
        }

        public int getProcessType() {
            return this.mProcessType;
        }

        public boolean rotate180() {
            return (this.mProcessType & PROCESS_TYPE_ROTATE_180) != 0;
        }

        public boolean blackAndWhite() {
            return (this.mProcessType & PROCESS_TYPE_BLACK_AND_WHITE) != 0;
        }

        public boolean endRequested() {
            return (this.mProcessType & PROCESS_TYPE_END) != 0;
        }

        public int getSide() {
            return this.mSide;
        }

        public int getPage() {
            return this.mPage;
        }
    }
}
