package epson.print.copy.Component.ecopycomponent;

import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.Iterator;

import epson.print.copy.Component.eremoteoperation.ERemoteCopyPhoto;
import epson.print.copy.Component.eremoteoperation.ERemoteOperation;

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) {
        selectedItem = eCopyOptionItem;
        optionChangedListener = iCopyOptionListener;
    }

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

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

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

    protected Result doInBackground(Void... voidArr) {
        operation.setHostIP(this.systemSettings.getPrinterIPAddress());
        if (this.selectedItem.isLocalOption) {
            return getResultLocalOptions();
        }
        ERemoteCopyPhoto.ERemoteCopyPhotoOptionResult options = 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 = selectedItem;
        if (options.success()) {
            optionContext.setCopyOptionsResult(options);
            result.changedItems = new ArrayList<>();
            Iterator<ERemoteOperation.ERemoteParam> it = changableParams.iterator();
            while (it.hasNext()) {
                ECopyOptionItem createOptionItem = ECopyOptionItem.createOptionItem(it.next(), options);
                if (this.optionContext.isChanged(createOptionItem)) {
                    result.changedItems.add(createOptionItem);
                    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);
            optionContext.replace(this.replacedItem);
        }
        return result;
    }

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