package epson.print.rpcopy.Component.ecopycomponent;

import android.os.AsyncTask;

import java.util.Iterator;

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;

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(operation.getHostIP());
        eRemoteDevice.setRequestConnectionTimeout(operation.getRequestConnectionTimeout());
        ERemoteDevice.ERemoteDeviceFunctionsResult functions = eRemoteDevice.getFunctions(new ERemoteOperation.IRemoteOperationParameter() {
            public String client_id() {
                return 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(operation.getHostIP());
        eRemotePrinter.setRequestConnectionTimeout(operation.getRequestConnectionTimeout());
        ERemotePrinter.ERemotePrinterComponentsResult components = eRemotePrinter.getComponents(new ERemoteOperation.IRemoteOperationParameter() {
            public String client_id() {
                return clientID;
            }
        });
        if (!components.success()) {
            return getErrorResult(components);
        }
        ERemoteScanner eRemoteScanner = new ERemoteScanner();
        eRemoteScanner.setHostIP(operation.getHostIP());
        eRemoteScanner.setRequestConnectionTimeout(operation.getRequestConnectionTimeout());
        ERemoteScanner.ERemoteScannerComponentsResult components2 = eRemoteScanner.getComponents(new ERemoteOperation.IRemoteOperationParameter() {
            public String client_id() {
                return clientID;
            }
        });
        if (!components2.success()) {
            return getErrorResult(components2);
        }
        return null;
    }

    protected Result doInBackground(Void... voidArr) {
        operation.setHostIP(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.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) {
        contextListener.onCopyOptionContextCreated(result.copyType, result.optionContext, result.error);
    }
}
