package epson.print.service;

import android.util.Log;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

import epson.print.EPImageList;

public class CreatePrintImageThread extends Thread {
    private static final String TAG = "CreatePrintImageThread";
    private Exception mException;
    private EPImageList mImageList;
    private int mImagePerPage;
    private LocalImageCreator mLocalImageCreator;
    private boolean mLoopEnd;
    private PrintService mPrintService;
    private SynchronousQueue<Integer> mQueue = new SynchronousQueue<>();
    private RenderingController mRenderingController;
    private int mTotalPage;

    public CreatePrintImageThread(RenderingController renderingController, PrintService printService, LocalImageCreator localImageCreator, EPImageList ePImageList, int i, int i2) {
        super(TAG);
        mRenderingController = renderingController;
        mPrintService = printService;
        mLocalImageCreator = localImageCreator;
        mImageList = ePImageList;
        mImagePerPage = i;
        mTotalPage = i2;
    }

    public void run() {
        mLoopEnd = false;
        createImageLoop();
        mLoopEnd = true;
        Log.v(TAG, "run() end");
    }

    private void createImageLoop() {
        mException = null;
        int size = mImageList.size();
        int i = 0;
        int i2 = 0;
        while (i < mTotalPage) {
            try {
                int i3 = mImagePerPage + i2;
                if (i3 > size) {
                    i3 = size;
                }
                mRenderingController.waitPage(i2, i3);
                if (this.mPrintService.getCancelPrinting()) {
                    throw new LocalInterrupt();
                } else if (localCreateImage(this.mImageList, i) != null) {
                    if (!this.mPrintService.getCancelPrinting()) {
                        putSheet(i);
                        i++;
                        i2 = i3;
                    } else {
                        throw new LocalInterrupt();
                    }
                } else {
                    return;
                }
            } catch (Exception e) {
                mException = e;
                e.printStackTrace();
                return;
            }
        }
    }

    private String localCreateImage(EPImageList ePImageList, int i) {
        return mLocalImageCreator.createImage(this.mPrintService, ePImageList, i);
    }

    private void putSheet(int i) throws InterruptedException {
        Log.v(TAG, "queue.put " + i);
        mQueue.put(Integer.valueOf(i));
    }

    public int waitePrintImage() throws Exception {
        Exception exc = mException;
        if (exc != null) {
            throw exc;
        } else if (this.mLoopEnd) {
            return -1;
        } else {
            Log.v(TAG, "next take");
            Integer poll = mQueue.poll(180, TimeUnit.MINUTES);
            Log.v(TAG, "queue.take " + poll);
            if (poll == null) {
                return -1;
            }
            return poll.intValue();
        }
    }

    static class LocalImageCreator {
        int mColor;
        int mDate;
        int mDuplex;
        int mLayout;
        int mLayoutMulti;
        int mPaperSize;
        int mQuality;

        public LocalImageCreator(int i, int i2, int i3, int i4, int i5, int i6, int i7) {
            mPaperSize = i;
            mColor = i2;
            mDuplex = i3;
            mLayout = i4;
            mLayoutMulti = i5;
            mQuality = i6;
            mDate = i7;
        }

        public String createImage(PrintService printService, EPImageList ePImageList, int i) {
            return printService.createPrintImage(ePImageList, i, mPaperSize, mColor, mDuplex, mLayout, mLayoutMulti, mQuality, mDate);
        }
    }
}
