package epson.print.rpcopy.Component.ecopycomponent;

import android.os.AsyncTask;
import epson.print.rpcopy.Component.ecopycomponent.ECopyComponent;
import epson.print.rpcopy.Component.eremoteoperation.ERemoteCopy;
import epson.print.rpcopy.Component.eremoteoperation.ERemoteDevice;
import epson.print.rpcopy.Component.eremoteoperation.ERemoteOperation;
import epson.print.rpcopy.Component.eremoteoperation.ERemotePrinter;
import epson.print.rpcopy.Component.eremoteoperation.ERemoteScanner;
import java.util.Iterator;

class RemoteCopyGetOptionTask extends AsyncTask<Void, Void, Result> implements ECopyComponent.ITask {
    String clientID;
    ECopyComponent.ICopyOptionContextListener contextListener;
    ECopyComponent.ECopyType copyType;
    ERemoteCopy operation = new ERemoteCopy();
    ECopyComponent.ICopySystemSettings systemSettings;

    public void setOptionContext(ECopyOptionContext eCopyOptionContext) {
    }

    class Result {
        ECopyComponent.ECopyType copyType;
        ECopyComponent.ICopyOptionContextListener.ContextCreationError error;
        ECopyOptionContext optionContext;

        Result() {
        }
    }

    public RemoteCopyGetOptionTask(ECopyComponent.ECopyType eCopyType, ECopyComponent.ICopyOptionContextListener iCopyOptionContextListener) {
        this.copyType = eCopyType;
        this.contextListener = iCopyOptionContextListener;
    }

    public ECopyComponent.ICopyCancelRequest start() {
        super.execute(new Void[0]);
        return null;
    }

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

    public void setRequestConnectionTimeout(int i) {
        this.operation.setRequestConnectionTimeout(i);
    }

    public void setClientID(String str) {
        this.clientID = str;
    }

    private Result getErrorResult(ERemoteOperation.ERemoteOperationResult eRemoteOperationResult) {
        Result result = new Result();
        if (eRemoteOperationResult.isNull(ERemoteOperation.ERemoteParam.success)) {
            result.error = ECopyComponent.ICopyOptionContextListener.ContextCreationError.ErrorCommunication;
        } else {
            result.error = ECopyComponent.ICopyOptionContextListener.ContextCreationError.Error;
        }
        return result;
    }

    private Result checkDeviceError() {
        Result result = new Result();
        result.copyType = this.copyType;
        ERemoteDevice eRemoteDevice = new ERemoteDevice();
        eRemoteDevice.setHostIP(this.operation.getHostIP());
        eRemoteDevice.setRequestConnectionTimeout(this.operation.getRequestConnectionTimeout());
        ERemoteDevice.ERemoteDeviceFunctionsResult functions = eRemoteDevice.getFunctions(new ERemoteOperation.IRemoteOperationParameter() {
            public String client_id() {
                return RemoteCopyGetOptionTask.this.clientID;
            }
        });
        if (!functions.success()) {
            return getErrorResult(functions);
        }
        boolean z = false;
        Iterator<ERemoteOperation.ERemoteParam> it = functions.functions().iterator();
        while (true) {
            if (it.hasNext()) {
                if (it.next() == ERemoteOperation.ERemoteParam.repeat_copy) {
                    z = true;
                    break;
                }
            } else {
                break;
            }
        }
        if (!z) {
            result.error = ECopyComponent.ICopyOptionContextListener.ContextCreationError.ErrorNotCopySupported;
            return result;
        }
        ERemotePrinter eRemotePrinter = new ERemotePrinter();
        eRemotePrinter.setHostIP(this.operation.getHostIP());
        eRemotePrinter.setRequestConnectionTimeout(this.operation.getRequestConnectionTimeout());
        ERemotePrinter.ERemotePrinterComponentsResult components = eRemotePrinter.getComponents(new ERemoteOperation.IRemoteOperationParameter() {
            public String client_id() {
                return RemoteCopyGetOptionTask.this.clientID;
            }
        });
        if (!components.success()) {
            return getErrorResult(components);
        }
        ERemoteScanner eRemoteScanner = new ERemoteScanner();
        eRemoteScanner.setHostIP(this.operation.getHostIP());
        eRemoteScanner.setRequestConnectionTimeout(this.operation.getRequestConnectionTimeout());
        ERemoteScanner.ERemoteScannerComponentsResult components2 = eRemoteScanner.getComponents(new ERemoteOperation.IRemoteOperationParameter() {
            public String client_id() {
                return RemoteCopyGetOptionTask.this.clientID;
            }
        });
        if (!components2.success()) {
            return getErrorResult(components2);
        }
        return null;
    }

    protected Result doInBackground(Void... voidArr) {
        this.operation.setHostIP(this.systemSettings.getPrinterIPAddress());
        Result checkDeviceError = checkDeviceError();
        if (checkDeviceError != null) {
            return checkDeviceError;
        }
        Result result = new Result();
        result.copyType = this.copyType;
        ERemoteCopy.ERemoteCopyOptionsResult selectableOptions = this.operation.getSelectableOptions();
        if (selectableOptions.success()) {
            result.optionContext = new ECopyOptionContext(result.copyType);
            result.optionContext.setCopyOptionsResult(selectableOptions);
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.color_effects_type, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.layout, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.print_media_size, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.print_media_type, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.print_media_source, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.print_quality, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.x_remove_background, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.x_cut_line, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.x_cut_line_style, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createOptionItem(ERemoteOperation.ERemoteParam.x_cut_line_weight, selectableOptions));
            result.optionContext.add(ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.copies, 1, 99, 1));
            result.optionContext.add(ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.x_density, -4, 4, 0));
        } else if (selectableOptions.isNull(ERemoteOperation.ERemoteParam.success)) {
            result.error = ECopyComponent.ICopyOptionContextListener.ContextCreationError.ErrorCommunication;
        } else {
            result.error = ECopyComponent.ICopyOptionContextListener.ContextCreationError.Error;
        }
        return result;
    }

    protected void onPostExecute(Result result) {
        this.contextListener.onCopyOptionContextCreated(result.copyType, result.optionContext, result.error);
    }
}
