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);
        this.mRenderingController = renderingController;
        this.mPrintService = printService;
        this.mLocalImageCreator = localImageCreator;
        this.mImageList = ePImageList;
        this.mImagePerPage = i;
        this.mTotalPage = i2;
    }

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

    private void createImageLoop() {
        this.mException = null;
        int size = this.mImageList.size();
        int i = 0;
        int i2 = 0;
        while (i < this.mTotalPage) {
            try {
                int i3 = this.mImagePerPage + i2;
                if (i3 > size) {
                    i3 = size;
                }
                this.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) {
                this.mException = e;
                e.printStackTrace();
                return;
            }
        }
    }

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

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

    public int waitePrintImage() throws Exception {
        Exception exc = this.mException;
        if (exc != null) {
            throw exc;
        } else if (this.mLoopEnd) {
            return -1;
        } else {
            Log.v(TAG, "next take");
            Integer poll = this.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) {
            this.mPaperSize = i;
            this.mColor = i2;
            this.mDuplex = i3;
            this.mLayout = i4;
            this.mLayoutMulti = i5;
            this.mQuality = i6;
            this.mDate = i7;
        }

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