package epson.print.copy.Component.ecopycomponent;

import android.os.AsyncTask;

import java.util.ArrayList;
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;

class RemoteDeviceTask extends AsyncTask<Void, Void, Result> implements ECopyComponent.ITask {
    String clientID;
    ERemoteDevice operation = new ERemoteDevice();
    ECopyOptionContext optionContext;
    ECopyComponent.ICopyDeviceStatusListener statusListener;
    ECopyComponent.ICopySystemSettings systemSettings;

    class Result {
        ECopyComponent.ICopyDeviceStatusListener.Connection connetion;
        ArrayList<String> jobTokens;
        ECopyComponent.ICopyDeviceStatusListener.DeviceState printer;
        ArrayList<ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason> printerReasons;
        ECopyComponent.ICopyDeviceStatusListener.DeviceState scanner;
        ArrayList<ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason> scannerReasons;

        Result() {
        }
    }

    public RemoteDeviceTask(ECopyComponent.ICopyDeviceStatusListener iCopyDeviceStatusListener) {
        statusListener = iCopyDeviceStatusListener;
    }

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

    private ECopyComponent.ICopyDeviceStatusListener.DeviceState getDeviceState(ERemoteOperation.ERemoteParam eRemoteParam) {
        ECopyComponent.ICopyDeviceStatusListener.DeviceState deviceState = ECopyComponent.ICopyDeviceStatusListener.DeviceState.Stopped;
        switch (eRemoteParam) {
            case idle:
                return ECopyComponent.ICopyDeviceStatusListener.DeviceState.Idle;
            case processing:
                return ECopyComponent.ICopyDeviceStatusListener.DeviceState.Processing;
            case stopped:
                return ECopyComponent.ICopyDeviceStatusListener.DeviceState.Stopped;
            default:
                return deviceState;
        }
    }

    private ArrayList<ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason> getPrinterErrorReasons() {
        ArrayList<ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason> arrayList = new ArrayList<>();
        ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.OtherError;
        printerStateReason.string = ERemoteOperation.ERemoteParam.other_error.string;
        arrayList.add(printerStateReason);
        return arrayList;
    }

    private ArrayList<ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason> getPrinterReasons(ArrayList<ERemoteOperation.ERemoteParam> arrayList) {
        ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason printerStateReason;
        ArrayList<ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason> arrayList2 = new ArrayList<>();
        Iterator<ERemoteOperation.ERemoteParam> it = arrayList.iterator();
        while (it.hasNext()) {
            ERemoteOperation.ERemoteParam next = it.next();
            ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason printerStateReason2 = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.None;
            switch (next) {
                case none:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.None;
                    break;
                case marker_supply_empty_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.MarkerSupplyEmptyError;
                    break;
                case marker_waste_full_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.MarkerWasteFullError;
                    break;
                case media_jam_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.MediaJamError;
                    break;
                case media_empty_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.MediaEmptyError;
                    break;
                case input_tray_missing_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.InputTrayMissingError;
                    break;
                case cover_open_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.CoverOpenError;
                    break;
                case output_area_full_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.OutputAreaFullError;
                    break;
                case other_error:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.OtherError;
                    break;
                case marker_supply_low_warning:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.MarkerSupplyLowWarning;
                    break;
                case cover_open:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.CoverOpen;
                    break;
                default:
                    printerStateReason = ECopyComponent.ICopyDeviceStatusListener.PrinterStateReason.OtherError;
                    break;
            }
            printerStateReason.string = next.string;
            arrayList2.add(printerStateReason);
        }
        return arrayList2;
    }

    private ArrayList<ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason> getScannerErrorReasons() {
        ArrayList<ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason> arrayList = new ArrayList<>();
        ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.OtherError;
        scannerStateReason.string = ERemoteOperation.ERemoteParam.other_error.string;
        arrayList.add(scannerStateReason);
        return arrayList;
    }

    private ArrayList<ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason> getScannerReasons(ArrayList<ERemoteOperation.ERemoteParam> arrayList) {
        ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason scannerStateReason;
        ArrayList<ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason> arrayList2 = new ArrayList<>();
        Iterator<ERemoteOperation.ERemoteParam> it = arrayList.iterator();
        while (it.hasNext()) {
            ERemoteOperation.ERemoteParam next = it.next();
            ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason scannerStateReason2 = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.None;
            int i = C22033.f378xed9088c4[next.ordinal()];
            if (i == 4) {
                scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.None;
            } else if (i == 12) {
                scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.OtherError;
            } else if (i != 15) {
                switch (i) {
                    case 7:
                        scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.MediaJamError;
                        break;
                    case 8:
                        scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.MediaEmptyError;
                        break;
                    default:
                        scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.OtherError;
                        break;
                }
            } else {
                scannerStateReason = ECopyComponent.ICopyDeviceStatusListener.ScannerStateReason.MediaSizeMissmatchError;
            }
            scannerStateReason.string = next.string;
            arrayList2.add(scannerStateReason);
        }
        return arrayList2;
    }

    private ArrayList<String> getJobTokens() {
        ERemoteCopy eRemoteCopy = new ERemoteCopy();
        eRemoteCopy.setHostIP(this.operation.getHostIP());
        eRemoteCopy.setRequestConnectionTimeout(this.operation.getRequestConnectionTimeout());
        ERemoteCopy.ERemoteCopyStatusResult status = eRemoteCopy.getStatus(new ERemoteCopy.IRemoteCopyStatusParameter() {
            public String job_token() {
                return "";
            }

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

            public ArrayList<ERemoteOperation.ERemoteParam> keys() {
                ArrayList<ERemoteOperation.ERemoteParam> arrayList = new ArrayList<>();
                arrayList.add(ERemoteOperation.ERemoteParam.job_tokens);
                return arrayList;
            }
        });
        if (status.success()) {
            return status.job_tokens();
        }
        return new ArrayList<>();
    }

    protected Result doInBackground(Void... voidArr) {
        operation.setHostIP(this.systemSettings.getPrinterIPAddress());
        ERemoteDevice.ERemoteDeviceStatusResult status = operation.getStatus(new ERemoteOperation.IRemoteStatusParameter() {
            public String client_id() {
                return RemoteDeviceTask.this.clientID;
            }

            public ArrayList<ERemoteOperation.ERemoteParam> keys() {
                ArrayList<ERemoteOperation.ERemoteParam> arrayList = new ArrayList<>();
                arrayList.add(ERemoteOperation.ERemoteParam.printer_state);
                arrayList.add(ERemoteOperation.ERemoteParam.printer_state_reasons);
                arrayList.add(ERemoteOperation.ERemoteParam.scanner_state);
                arrayList.add(ERemoteOperation.ERemoteParam.scanner_state_reasons);
                return arrayList;
            }
        });
        Result result = new Result();
        if (status.success()) {
            result.connetion = ECopyComponent.ICopyDeviceStatusListener.Connection.Succeed;
            result.printer = getDeviceState(status.printer_state());
            result.scanner = getDeviceState(status.scanner_state());
            result.printerReasons = getPrinterReasons(status.printer_state_reasons());
            result.scannerReasons = getScannerReasons(status.scanner_state_reasons());
            result.jobTokens = getJobTokens();
        } else {
            result.connetion = ECopyComponent.ICopyDeviceStatusListener.Connection.Failed;
            result.printer = ECopyComponent.ICopyDeviceStatusListener.DeviceState.Stopped;
            result.scanner = ECopyComponent.ICopyDeviceStatusListener.DeviceState.Stopped;
            result.printerReasons = getPrinterErrorReasons();
            result.scannerReasons = getScannerErrorReasons();
            result.jobTokens = new ArrayList<>();
        }
        return result;
    }

    protected void onPostExecute(Result result) {
        statusListener.onDeviceStatus(result.connetion, result.printer, result.scanner, result.printerReasons, result.scannerReasons, result.jobTokens);
    }
}
