package epson.print.copy.Component.ecopycomponent;

import android.os.AsyncTask;
import epson.print.copy.Component.ecopycomponent.ECopyComponent;
import epson.print.copy.Component.ecopycomponent.ECopyOptionItem;
import epson.print.copy.Component.eremoteoperation.ERemoteCopyPhoto;
import epson.print.copy.Component.eremoteoperation.ERemoteOperation;
import java.util.ArrayList;
import java.util.Iterator;

class RemoteCopyPhotoSetOptionTask extends AsyncTask<Void, Void, Result> implements ECopyComponent.ITask {
    ArrayList<ERemoteOperation.ERemoteParam> changableParams;
    String clientID;
    ERemoteCopyPhoto operation = new ERemoteCopyPhoto();
    ECopyComponent.ICopyOptionListener optionChangedListener;
    ECopyOptionContext optionContext;
    ECopyOptionItem replacedItem;
    ECopyOptionItem selectedItem;
    ECopyComponent.ICopySystemSettings systemSettings;

    class Result {
        ArrayList<ECopyOptionItem> changedItems;
        ECopyComponent.ICopyOptionListener.CopyOptionChangedError error;
        ECopyOptionItem selectedItem;

        Result() {
        }
    }

    public RemoteCopyPhotoSetOptionTask(ECopyOptionItem eCopyOptionItem, ECopyComponent.ICopyOptionListener iCopyOptionListener) {
        this.selectedItem = eCopyOptionItem;
        this.optionChangedListener = iCopyOptionListener;
    }

    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;
    }

    public void setOptionContext(ECopyOptionContext eCopyOptionContext) {
        this.optionContext = eCopyOptionContext;
        this.replacedItem = this.optionContext.replace(new ECopyOptionItem(this.selectedItem));
        this.changableParams = new ArrayList<>();
        this.changableParams.add(ERemoteOperation.ERemoteParam.x_borderless);
        this.changableParams.add(ERemoteOperation.ERemoteParam.print_media_size);
        this.changableParams.add(ERemoteOperation.ERemoteParam.print_media_type);
        this.changableParams.add(ERemoteOperation.ERemoteParam.x_apf);
        this.changableParams.add(ERemoteOperation.ERemoteParam.print_quality);
        this.changableParams.add(ERemoteOperation.ERemoteParam.print_media_source);
    }

    public Result getResultLocalOptions() {
        Result result = new Result();
        result.selectedItem = this.selectedItem;
        result.changedItems = new ArrayList<>();
        return result;
    }

    protected Result doInBackground(Void... voidArr) {
        this.operation.setHostIP(this.systemSettings.getPrinterIPAddress());
        if (this.selectedItem.isLocalOption) {
            return getResultLocalOptions();
        }
        ERemoteCopyPhoto.ERemoteCopyPhotoOptionResult options = this.operation.getOptions(new ERemoteCopyPhoto.IRemoteCopyPhotoOptionsParameter() {
            public boolean default_as_fixed() {
                return true;
            }

            public String client_id() {
                return RemoteCopyPhotoSetOptionTask.this.clientID;
            }

            public ERemoteOperation.ERemoteParam layout() {
                return RemoteCopyPhotoSetOptionTask.this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.XBorderless).getSelectedChoice().param;
            }

            public ERemoteOperation.ERemoteParam print_media_type() {
                return RemoteCopyPhotoSetOptionTask.this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.PrintMediaType).getSelectedChoice().param;
            }

            public ERemoteOperation.ERemoteParam print_media_size() {
                return RemoteCopyPhotoSetOptionTask.this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.PrintMediaSize).getSelectedChoice().param;
            }

            public ERemoteOperation.ERemoteParam print_quality() {
                return RemoteCopyPhotoSetOptionTask.this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.PrintQuality).getSelectedChoice().param;
            }

            public ERemoteOperation.ERemoteParam print_media_source() {
                return RemoteCopyPhotoSetOptionTask.this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.PrintMediaSource).getSelectedChoice().param;
            }

            public ERemoteOperation.ERemoteParam x_apf() {
                return RemoteCopyPhotoSetOptionTask.this.optionContext.getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey.XApf).getSelectedChoice().param;
            }

            public ArrayList<ERemoteOperation.ERemoteParam> fixed_parameters() {
                return new ArrayList<>();
            }

            public ArrayList<ERemoteOperation.ERemoteParam> preferred_parameters() {
                return new ArrayList<>();
            }

            public ArrayList<ERemoteOperation.ERemoteParam> priority_order() {
                ArrayList<ERemoteOperation.ERemoteParam> arrayList = new ArrayList<>();
                arrayList.add(ERemoteOperation.ERemoteParam.layout);
                arrayList.add(ERemoteOperation.ERemoteParam.print_media_size);
                arrayList.add(ERemoteOperation.ERemoteParam.print_media_type);
                arrayList.add(ERemoteOperation.ERemoteParam.x_apf);
                arrayList.add(ERemoteOperation.ERemoteParam.print_quality);
                arrayList.add(ERemoteOperation.ERemoteParam.print_media_source);
                return arrayList;
            }
        });
        Result result = new Result();
        result.selectedItem = this.selectedItem;
        if (options.success()) {
            this.optionContext.setCopyOptionsResult(options);
            result.changedItems = new ArrayList<>();
            Iterator<ERemoteOperation.ERemoteParam> it = this.changableParams.iterator();
            while (it.hasNext()) {
                ECopyOptionItem createOptionItem = ECopyOptionItem.createOptionItem(it.next(), options);
                if (this.optionContext.isChanged(createOptionItem)) {
                    result.changedItems.add(createOptionItem);
                    this.optionContext.replace(createOptionItem);
                }
            }
            Iterator<ECopyOptionItem> it2 = result.changedItems.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break;
                }
                ECopyOptionItem next = it2.next();
                if (next.getKey() == ECopyOptionItem.ECopyOptionItemKey.XBorderless) {
                    ECopyOptionItem createLocalOptionItem = ECopyOptionItem.createLocalOptionItem(ERemoteOperation.ERemoteParam.print_x_bleed, options);
                    if (next.getSelectedChoice() == ECopyOptionItem.ECopyOptionItemChoice.XBorderless_Standard) {
                        createLocalOptionItem.enabled = false;
                    }
                    result.changedItems.add(createLocalOptionItem);
                }
            }
            if (result.changedItems.size() == 0) {
                result.changedItems = null;
            }
        } else {
            if (options.isNull(ERemoteOperation.ERemoteParam.success)) {
                result.error = ECopyComponent.ICopyOptionListener.CopyOptionChangedError.ErrorCommunication;
            } else {
                result.error = ECopyComponent.ICopyOptionListener.CopyOptionChangedError.Error;
            }
            result.selectedItem = new ECopyOptionItem(this.replacedItem);
            this.optionContext.replace(this.replacedItem);
        }
        return result;
    }

    protected void onPostExecute(Result result) {
        this.optionChangedListener.onCopyOptionChanged(result.selectedItem, result.changedItems, result.error);
    }
}
