package epson.print.rpcopy;

import android.content.Context;

import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.util.Iterator;

import epson.print.CommonDefine;
import epson.print.R;
import epson.print.rpcopy.Component.ecopycomponent.ECopyComponent;
import epson.print.rpcopy.Component.ecopycomponent.ECopyOptionItem;

public class CopyProcess implements ECopyComponent.ICopyStatusListener {
    ActivityBase activityBase;
    ECopyComponent.ICopyCancelRequest cancelRequest;
    ECopyComponent copyComponent;
    boolean disconnectWifi;
    String jobToken;
    ActivityBase.errorDialog printererror;
    ActivityBase.WheelDialog wheel;

    public CopyProcess(ActivityBase activityBase2) {
        this(activityBase2, (String) null);
    }

    public CopyProcess(ActivityBase activityBase2, String str) {
        copyComponent = ECopyComponent.sharedComponent();
        disconnectWifi = false;
        activityBase = activityBase2;
        jobToken = str;
        ActivityBase activityBase3 = activityBase;
        activityBase3.getClass();
        wheel = new ActivityBase.WheelDialog(activityBase);
    }

    public void startCopy(Context context) {
        String str = jobToken;
        cancelRequest = str == null ? copyComponent.startCopy(this, context) : copyComponent.recoverCopy(str, this);
    }

    public void onStarted(ECopyComponent.ICopyStatusListener.CopyTaskType copyTaskType) {
        wheel.show(activityBase.getString(R.string.CopyTaskProgressCopying), new ActivityBase.CancelRequestCallback() {
            public void onCancelRequest() {
                cancelRequest.cancel();
            }
        });
    }

    public void onProcessed(ECopyComponent.ICopyStatusListener.CopyTaskType copyTaskType, int i, int i2, ECopyComponent.ICopyStatusListener.CopyTaskProgress copyTaskProgress, final ECopyComponent.ICopyResumeRequest iCopyResumeRequest) {
        ActivityBase.errorDialog errordialog = printererror;
        if (errordialog != null) {
            errordialog.Cancel();
        }
        if (copyTaskProgress.equals(ECopyComponent.ICopyStatusListener.CopyTaskProgress.Copying) || copyTaskProgress.equals(ECopyComponent.ICopyStatusListener.CopyTaskProgress.Processing)) {
            ActivityBase.WheelDialog wheelDialog = wheel;
            wheelDialog.setText(activityBase.getString(R.string.CopyTaskProgressCopying) + " " + i2 + CommonDefine.SLASH + i);
        } else {
            wheel.setText("");
        }
        switch (copyTaskProgress) {
            case Stopped:
                ActivityBase activityBase2 = activityBase;
                activityBase2.getClass();
                printererror = new ActivityBase.errorDialog(activityBase);
                ECopyComponent.ICopyResumeRequest.StopReason stopReason = iCopyResumeRequest.getStopReason();
                if (stopReason.equals(ECopyComponent.ICopyResumeRequest.StopReason.PrinterMediaEmptyError)) {
                    Iterator<ECopyOptionItem> it = copyComponent.getCopyOptionItems().iterator();
                    while (it.hasNext()) {
                        ECopyOptionItem next = it.next();
                        if (next.getKey().equals(ECopyOptionItem.ECopyOptionItemKey.PrintMediaSource) && next.getSelectedChoice().equals(ECopyOptionItem.ECopyOptionItemChoice.PrintMediaSource_Manual)) {
                            stopReason = ECopyComponent.ICopyResumeRequest.StopReason.ManualfeedGuide;
                        }
                    }
                }
                String[] reasonText = printererror.getReasonText(stopReason);
                printererror.showErrorDialog(reasonText[0], reasonText[1], iCopyResumeRequest.isPossibleClearError() ? ActivityBase.DialogButtons.ClearErrorCancel : ActivityBase.DialogButtons.Cancel, new ActivityBase.IClose() {
                    public void onClose(ActivityBase.ClickButton clickButton) {
                        iCopyResumeRequest.resume(clickButton == ActivityBase.ClickButton.ClearError ? ECopyComponent.ICopyResumeRequest.ResumeState.ClearError : ECopyComponent.ICopyResumeRequest.ResumeState.Cancel);
                    }
                });
                return;
            case Waiting2ndPage:
                activityBase.showNextPageDialog(new ActivityBase.INextPageClose() {
                    public void onClose(ECopyComponent.ICopyResumeRequest.ResumeState resumeState) {
                        iCopyResumeRequest.resume(resumeState);
                    }
                });
                return;
            case Canceling:
                wheel.hideCancelButton();
                return;
            default:
                return;
        }
    }

    public void onFinished(ECopyComponent.ICopyStatusListener.CopyTaskType copyTaskType, ECopyComponent.ICopyStatusListener.CopyTaskResult copyTaskResult) {
        wheel.dismiss();
        switch (copyTaskResult) {
            case Succeed:
                Iterator<ECopyOptionItem> it = copyComponent.getCopyOptionItems().iterator();
                while (it.hasNext()) {
                    ECopyOptionItem next = it.next();
                    if (next.getKey().equals(ECopyOptionItem.ECopyOptionItemKey.Copies)) {
                        next.selectValue(next.getDefaultValue());
                        copyComponent.setCopyOptionItem(next);
                    }
                }
                break;
            case Busy:
            case ErrorCommunication:
            case RemoveAdfPaper:
            case ErrorOther:
                ActivityBase activityBase2 = activityBase;
                activityBase2.getClass();
                printererror = new ActivityBase.errorDialog(activityBase);
                String[] reasonText = printererror.getReasonText(copyTaskResult);
                printererror.showErrorDialog(reasonText[0], reasonText[1], ActivityBase.DialogButtons.Ok, new ActivityBase.IClose() {
                    public void onClose(ActivityBase.ClickButton clickButton) {
                    }
                });
                break;
        }
        if (disconnectWifi) {
            WiFiDirectManager.disconnect(activityBase, WiFiDirectManager.DEVICE_TYPE_PRINTER, ActivityBase.printerIp);
        }
    }

    public boolean isProccessing() {
        return wheel.isShowing();
    }

    public void setDisconnectWifi(boolean z) {
        disconnectWifi = z;
    }
}
