package epson.print.copy.Component.ecopycomponent;

import android.os.AsyncTask;

import java.util.Iterator;

import epson.print.copy.Component.eremoteoperation.ERemoteCopy;
import epson.print.copy.Component.eremoteoperation.ERemoteDevice;
import epson.print.copy.Component.eremoteoperation.ERemoteOperation;
import epson.print.copy.Component.eremoteoperation.ERemotePrinter;
import epson.print.copy.Component.eremoteoperation.ERemoteScanner;

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) {
        copyType = eCopyType;
        contextListener = iCopyOptionContextListener;
    }

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

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

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

    public void setClientID(String str) {
        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 = 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.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) {
        operation.setHostIP(this.systemSettings.getPrinterIPAddress());
        Result checkDeviceError = checkDeviceError();
        if (checkDeviceError != null) {
            return checkDeviceError;
        }
        Result result = new Result();
        result.copyType = copyType;
        ERemoteCopy.ERemoteCopyOptionsResult selectableOptions = operation.getSelectableOptions();
        if (selectableOptions.success()) {
            result.optionContext = new ECopyOptionContext(result.copyType);
            result.optionContext.setCopyOptionsResult(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.scan_content_type, selectableOptions));
            ECopyOptionItem createScaleOptionItem = ECopyOptionItem.createScaleOptionItem(ERemoteOperation.ERemoteParam.x_scale, selectableOptions);
            ECopyOptionItem createLocalOptionItem = ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.copy_magnification, 25, 400, ECopyOptionItem.CopyMagnificationAutofitValue);
            createLocalOptionItem.numberRange.exceptionValue = ECopyOptionItem.CopyMagnificationAutofitValue;
            result.optionContext.add(createScaleOptionItem);
            result.optionContext.add(createLocalOptionItem);
            boolean z = true;
            result.optionContext.add(ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.copies, 1, 99, 1));
            result.optionContext.add(ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.x_density, -4, 4, 0));
            result.optionContext.add(ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.color_effects_type, selectableOptions));
            ECopyOptionItem createLocalOptionItem2 = ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.print_x_bleed, selectableOptions);
            if (result.copyType != ECopyComponent.ECopyType.Borderless) {
                z = false;
            }
            createLocalOptionItem2.enabled = z;
            result.optionContext.add(createLocalOptionItem2);
        } 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) {
        contextListener.onCopyOptionContextCreated(result.copyType, result.optionContext, result.error);
    }
}
