package epson.scan.i2lib;


import androidx.annotation.NonNull;
import androidx.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) {
        mI2ScanTask = i2ScanTask;
        mDuplex = z;
        mConvertBlackAndWhite = z2;
        mBlackAndWhiteThresh = i3;
        setBackSideRotation(i, i2);
    }

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

    /* access modifiers changed from: package-private */
    @NonNull
    public ArrayList<String> getScanFiles(int[] iArr) {
        int i = 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();
        mImageProcessThread = new ImageProcessThread(this, mBlackAndWhiteThresh);
        mImageProcessThread.start();
    }

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

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

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

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

    public File getAllProcessFile(int i, int i2) {
        checkStatus();
        return getFile((getProcessType(i, i2) == ImageProcessInfo.PROCESS_TYPE_EMPTY ? mScanOutFilenameFormat : 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 (mScanOutFilenameFormat == null) {
            throw new IllegalStateException("init() is not called");
        }
    }

    private void checkThreadStatus() {
        if (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");
        mScanOutFilenameFormat = new String[]{file2.toString(), new File(file, geDateFormat(calendar) + "b_%d.jpg").toString()};
        File file3 = new File(file, geDateFormat(calendar) + "a2_%d.jpg");
        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 (mCancelRequested) {
            return true;
        }
        int processType = getProcessType(i, i2);
        String path = getFile(mScanOutFilenameFormat[i], i2).getPath();
        if (processType == ImageProcessInfo.PROCESS_TYPE_EMPTY) {
            str = null;
        } else {
            str = getAllProcessFile(i, i2).getPath();
        }
        try {
            mBlockingDeque.putLast(new ImageProcessInfo(processType, path, str, i, i2));
            return true;
        } catch (InterruptedException unused) {
            mProcessEndLatch.countDown();
            return false;
        }
    }

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

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

    private int getProcessType(int i, int i2) {
        int i3 = ImageProcessInfo.PROCESS_TYPE_EMPTY;
        if (i == 1 && mBackSideRotate180) {
            i3 |= ImageProcessInfo.PROCESS_TYPE_ROTATE_180;
        }
        return 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) {
            mProcessType = i;
            mInputFilename = str;
            mOutputFilename = str2;
            mSide = i2;
            mPage = i3;
        }

        public String getInputFilename() {
            return mInputFilename;
        }

        public String getOutputFilename() {
            return mOutputFilename;
        }

        public int getProcessType() {
            return mProcessType;
        }

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

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

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

        public int getSide() {
            return mSide;
        }

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