package epson.print.rpcopy;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import epson.common.Constants;
import epson.print.ActivityIACommon;
import epson.print.CommonDefine;
import epson.print.R;
import epson.print.Util.EPLog;
import epson.print.copy.DisplayUtil;
import epson.print.rpcopy.Component.ecopycomponent.ECopyComponent;
import epson.print.rpcopy.Component.ecopycomponent.ECopyOptionContext;
import epson.print.rpcopy.Component.ecopycomponent.ECopyOptionItem;
import epson.print.rpcopy.Component.eremoteoperation.ERemoteOperation;
import epson.print.screen.WorkingDialog;
import epson.print.widgets.CustomTitleAlertDialogBuilder;

@SuppressLint({"Registered"})
class ActivityBase extends ActivityIACommon {
    static final String Title = "Title";
    private static final HashMap<String, String> mParamStringToDisplayStringHash = new HashMap<>();
    static String printerId;
    static String printerIp;
    private String TAG = "ActivityBase";
    ECopyComponent copyComponent = ECopyComponent.sharedComponent();
    ECopyComponent.ECopyType copyType = ECopyComponent.ECopyType.Standard;
    boolean isKeepSimpleAPConnection = false;
    boolean isTryConnectSimpleAp = false;
    WorkingDialog loading;
    ECopyOptionContext mECopyOptionContext;
    HashMap<ECopyOptionItem.ECopyOptionItemKey, OptionValue> optionValueMap = new HashMap<>();

    public interface CancelRequestCallback {
        void onCancelRequest();
    }

    enum ClickButton {
        Ok,
        Cancel,
        ClearError
    }

    enum DialogButtons {
        Ok,
        Cancel,
        ClearErrorCancel
    }

    interface IClose {
        void onClose(ClickButton clickButton);
    }

    interface INextPageClose {
        void onClose(ECopyComponent.ICopyResumeRequest.ResumeState resumeState);
    }

    interface OptionItemChangedListener {
        void onOptionItemChanged(ECopyOptionItem eCopyOptionItem);
    }

    private void buildCopyOptions(ArrayList<ECopyOptionItem> arrayList) {
    }

    ActivityBase() {
    }

    class WheelDialog extends Dialog {
        Button cancelButton;
        CancelRequestCallback cancelCallback;
        Dialog cancelDialog;
        TextView messageText = ((TextView) findViewById(R.id.messageText));

        private Dialog createCancelDialog() {
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setMessage(ActivityBase.this.getString(R.string.str_msg_scan_cancel));
            builder.setCancelable(false);
            builder.setPositiveButton(ActivityBase.this.getString(R.string.str_yes), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    WheelDialog.this.cancelButton.setEnabled(false);
                    WheelDialog.this.cancelCallback.onCancelRequest();
                    dialogInterface.cancel();
                }
            });
            builder.setNegativeButton(ActivityBase.this.getString(R.string.str_no), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    WheelDialog.this.cancelButton.setEnabled(true);
                    dialogInterface.cancel();
                }
            });
            return builder.create();
        }

        public WheelDialog(Context context) {
            super(context, R.style.AppTheme_Translucent_Gray);
            setContentView(R.layout.wheel_dialog);
            setCancelable(false);
        }

        private void show(String str, CancelRequestCallback cancelRequestCallback) {
            cancelCallback = cancelRequestCallback;
            messageText.setText(str);
            messageText.setVisibility(0);
            cancelButton = (Button) findViewById(R.id.cancelButton);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    WheelDialog wheelDialog = WheelDialog.this;
                    wheelDialog.cancelDialog = wheelDialog.createCancelDialog();
                    WheelDialog.this.cancelDialog.show();
                }
            });
            cancelButton.setVisibility(0);
            show();
        }

        private void setText(String str) {
            messageText.setText(str);
        }

        private void hideCancelButton() {
            cancelButton.setVisibility(4);
        }

        private void dissmiss() {
            super.dismiss();
            Dialog dialog = cancelDialog;
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        loading = new WorkingDialog(this);
        DisplayUtil.setMap(this, mParamStringToDisplayStringHash);
    }

    public class errorDialog {
        public static final int MESSAGE = 1;
        public static final int TITLE = 0;
        Context context;
        Dialog dialog;

        public errorDialog(Context context2) {
            context = context2;
        }

        private void showErrorDialog(String str, String str2) {
            showErrorDialog(str, str2, DialogButtons.Ok, (IClose) null);
        }

        private void showErrorDialog(int i) {
            showErrorDialog((String) null, ActivityBase.this.getString(i));
        }

        private void showErrorDialog(String str, String str2, final DialogButtons dialogButtons, final IClose iClose) {
            String str3;
            CustomTitleAlertDialogBuilder customTitleAlertDialogBuilder = new CustomTitleAlertDialogBuilder(this.context);
            String str4 = null;
            switch (dialogButtons) {
                case Ok:
                    str4 = ActivityBase.this.getString(R.string.str_ok);
                    str3 = null;
                    break;
                case Cancel:
                    str3 = ActivityBase.this.getString(R.string.str_cancel);
                    break;
                case ClearErrorCancel:
                    str4 = ActivityBase.this.getString(R.string.str_continue);
                    str3 = ActivityBase.this.getString(R.string.str_cancel);
                    break;
                default:
                    str3 = null;
                    break;
            }
            if (str != null) {
                customTitleAlertDialogBuilder.setTitle(str);
            }
            customTitleAlertDialogBuilder.setMessage(str2);
            customTitleAlertDialogBuilder.setCancelable(false);
            if (str4 != null) {
                customTitleAlertDialogBuilder.setPositiveButton(str4, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                        IClose iClose = iClose;
                        if (iClose != null) {
                            iClose.onClose(dialogButtons == DialogButtons.Ok ? ClickButton.Ok : ClickButton.ClearError);
                        }
                    }
                });
            }
            if (str3 != null) {
                customTitleAlertDialogBuilder.setNegativeButton(str3, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                        IClose iClose = iClose;
                        if (iClose != null) {
                            iClose.onClose(ClickButton.Cancel);
                        }
                    }
                });
            }
            dialog = customTitleAlertDialogBuilder.create();
            dialog.show();
        }

        private void Cancel() {
            Dialog dialog2 = dialog;
            if (dialog2 != null) {
                dialog2.cancel();
            }
        }

        public String[] getReasonText(ECopyComponent.ICopyStatusListener.CopyTaskResult copyTaskResult) {
            String[] strArr = new String[2];
            switch (copyTaskResult) {
                case ErrorCommunication:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_COMM_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyCommErrorText);
                    break;
                case Busy:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNST_BUSY_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNST_BUSY_MSG);
                    break;
                case RemoveAdfPaper:
                    strArr[0] = ActivityBase.this.getString(R.string.CopyTaskResultRemoveAdfPaperTitle);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyTaskResultRemoveAdfPaper);
                    break;
                case ErrorOther:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_GENERAL_ERR);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyTaskResultErrorOtherText);
                    break;
            }
            return strArr;
        }

        public String[] getReasonText(ECopyComponent.ICopyOptionContextListener.ContextCreationError contextCreationError) {
            String[] strArr = new String[2];
            switch (contextCreationError) {
                case ErrorCommunication:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_COMM_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_COMM5);
                    break;
                case ErrorNotCopySupported:
                case Error:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_GENERAL_ERR);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyTaskResultErrorOtherText);
                    break;
            }
            return strArr;
        }

        public String[] getReasonText(ECopyComponent.ICopyOptionListener.CopyOptionChangedError copyOptionChangedError) {
            String[] strArr = new String[2];
            switch (copyOptionChangedError) {
                case ErrorCommunication:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_COMM_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_COMM5);
                    break;
                case ErrorInvalidOption:
                case Error:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_GENERAL_ERR);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyTaskResultErrorOtherText);
                    break;
            }
            return strArr;
        }

        public String[] getReasonText(ECopyComponent.ICopyResumeRequest.StopReason stopReason) {
            String[] strArr = new String[2];
            switch (stopReason) {
                case PrinterMarkerSupplyEmptyError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_INKOUT_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_INKOUT_MSG);
                    break;
                case PrinterMarkerWasteFullError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_WEB_REMOTE_MAINTENANCE_BOX_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_WEB_REMOTE_MAINTENANCE_BOX_MSG);
                    break;
                case PrinterMediaJamError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_PAPERJAM_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyPaperJamErrorText);
                    break;
                case PrinterMediaEmptyError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_PAPEROUT_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_PAPEROUT_MSG);
                    break;
                case ManualfeedGuide:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_MANUALFEED_SET_PAPER_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_MANUALFEED_SET_PAPER_MSG);
                    break;
                case PrinterCoverOpenError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_COVEROPEN_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.EPS_PRNERR_COVEROPEN_MSG);
                    break;
                case PrinterOutputAreaFullError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_CDRGUIDEOPEN_TITLE);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyCDdiscErrorText);
                    break;
                case PrinterOtherError:
                    strArr[0] = ActivityBase.this.getString(R.string.EPS_PRNERR_GENERAL_ERR);
                    strArr[1] = ActivityBase.this.getString(R.string.CopyTaskResultErrorOtherText);
                    break;
                case ScannerOtherError:
                    strArr[0] = ActivityBase.this.getString(R.string.str_err_msg_scan_generic_internal_title);
                    strArr[1] = ActivityBase.this.getString(R.string.str_err_msg_scan_generic_internal);
                    break;
            }
            return strArr;
        }
    }

    private void showNextPageDialog(final INextPageClose iNextPageClose) {
        C22541 r0 = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                ECopyComponent.ICopyResumeRequest.ResumeState resumeState;
                dialogInterface.cancel();
                if (i == -1) {
                    resumeState = ECopyComponent.ICopyResumeRequest.ResumeState.NextPageReady;
                } else if (i == -3) {
                    resumeState = ECopyComponent.ICopyResumeRequest.ResumeState.NextPageNotExist;
                } else {
                    resumeState = ECopyComponent.ICopyResumeRequest.ResumeState.Cancel;
                }
                iNextPageClose.onClose(resumeState);
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.waiting_2nd_page);
        builder.setCancelable(true);
        builder.setPositiveButton(getString(R.string.next_page_ready), r0);
        builder.setNeutralButton(R.string.next_page_not_exist, r0);
        builder.setNegativeButton(getString(R.string.str_cancel), r0);
        builder.create().show();
    }

    abstract class OptionValue {
        OptionItemChangedListener changedListener;
        ECopyOptionItem optionItem;

        OptionValue() {
        }

        private void setOptionValueChangedListener(OptionItemChangedListener optionItemChangedListener) {
            changedListener = optionItemChangedListener;
        }
    }

    class ListOptionValue extends OptionValue {
        List<ECopyOptionItem.ECopyOptionItemChoice> choices;
        ECopyOptionItem.ECopyOptionItemChoice selected;

        ListOptionValue() {
            super();
        }

        private void bindOption(ECopyOptionItem eCopyOptionItem) {
            if (ECopyOptionItem.ECopyOptionItemKey.XCutLineStyle.equals(eCopyOptionItem.getKey()) || ECopyOptionItem.ECopyOptionItemKey.XCutLineWeight.equals(eCopyOptionItem.getKey())) {
                eCopyOptionItem.updateChoicesXCutLine();
            }
            optionItem = eCopyOptionItem;
            choices = eCopyOptionItem.getSelectableChoices();
            selected = eCopyOptionItem.getSelectedChoice();
        }

        private String[] getKeyArray() {
            String[] strArr = new String[this.choices.size()];
            for (int i = 0; i < choices.size(); i++) {
                strArr[i] = ActivityBase.this.getDisplayString(this.choices.get(i).name());
            }
            return strArr;
        }
    }

    class NumberOptionValue extends OptionValue {
        Button countDown;
        Button countUp;
        TextView editText;
        int value;

        NumberOptionValue() {
            super();
        }

        private void updateWithAddition(int i) {
            try {
                value = Integer.valueOf(this.editText.getText().toString()).intValue();
                value = Math.min(Math.max(this.optionItem.getMinimumValue(), value + i), optionItem.getMaximumValue());
                optionItem.selectValue(this.value);
                if (this.changedListener != null) {
                    changedListener.onOptionItemChanged(this.optionItem);
                }
            } catch (NumberFormatException unused) {
            }
        }

        class Counter implements View.OnClickListener {
            int amount;

            public Counter(int i) {
                amount = i;
            }

            public void onClick(View view) {
                NumberOptionValue.this.updateWithAddition(this.amount);
            }
        }

        private void bindOption(ECopyOptionItem eCopyOptionItem) {
            optionItem = eCopyOptionItem;
            value = eCopyOptionItem.getSelectedValue();
        }

        private void bindOption(int i, ECopyOptionItem eCopyOptionItem) {
            optionItem = eCopyOptionItem;
            editText = (TextView) ActivityBase.this.findViewById(i);
            value = eCopyOptionItem.getSelectedValue();
            editText.setText(String.valueOf(this.value));
            editText.setEnabled(eCopyOptionItem.isEnabled());
        }

        private void bindCountUp(int i) {
            countUp = (Button) ActivityBase.this.findViewById(i);
            countUp.setOnClickListener(new Counter(1));
            if (this.value == optionItem.getMaximumValue()) {
                countUp.setEnabled(false);
            } else {
                countUp.setEnabled(true);
            }
        }

        private void bindCountDown(int i) {
            countDown = (Button) ActivityBase.this.findViewById(i);
            countDown.setOnClickListener(new Counter(-1));
            if (this.value == optionItem.getMinimumValue()) {
                countDown.setEnabled(false);
            } else {
                countDown.setEnabled(true);
            }
        }
    }

    class CopyMagnificationValue extends NumberOptionValue {
        public CopyMagnificationValue(ECopyOptionItem eCopyOptionItem) {
            super();
            bindOption(eCopyOptionItem);
        }
    }

    private String getDisplayString(String str) {
        return (String) (mParamStringToDisplayStringHash.containsKey(str) ? mParamStringToDisplayStringHash.get(str) : mParamStringToDisplayStringHash.get("Unknown"));
    }

    class settingPreference {
        private SharedPreferences.Editor editer;
        private SharedPreferences presettings;

        settingPreference() {
        }

        private SharedPreferences getPreferences() {
            return ActivityBase.this.getSharedPreferences(CommonDefine.PREFS_INFO_REPEAT_COPY, 0);
        }

        private String loadPrePrinter() {
            presettings = getPreferences();
            return presettings.getString(Constants.PRINTER_ID, (String) null);
        }

        private void savePrePrinter() {
            presettings = getPreferences();
            editer = presettings.edit();
            editer.putString(Constants.PRINTER_ID, ActivityBase.printerId);
            editer.commit();
        }

        private void setCopyOptions(ArrayList<ECopyOptionItem> arrayList) {
            ECopyOptionContext bindedCopyOptionContext = ActivityBase.this.copyComponent.getBindedCopyOptionContext();
            Iterator<ECopyOptionItem> it = arrayList.iterator();
            while (it.hasNext()) {
                ECopyOptionItem next = it.next();
                ECopyOptionItem.ECopyOptionItemKey key = next.getKey();
                if (!key.equals(ECopyOptionItem.ECopyOptionItemKey.Copies)) {
                    setOption(key, next);
                    bindedCopyOptionContext.replace(next);
                }
            }
            Iterator<ECopyOptionItem> it2 = arrayList.iterator();
            while (it2.hasNext()) {
                ActivityBase.this.copyComponent.setCopyOptionItem(it2.next());
            }
        }

        private void setOption(ECopyOptionItem.ECopyOptionItemKey eCopyOptionItemKey, ECopyOptionItem eCopyOptionItem) {
            presettings = getPreferences();
            if (eCopyOptionItem.getChoiceType() != ECopyOptionItem.ECopyOptionItemChoiceType.ChoiceArray) {
                eCopyOptionItem.selectValue(this.presettings.getInt(eCopyOptionItemKey.name(), eCopyOptionItem.getDefaultValue()));
            } else if (eCopyOptionItem.getDefaultChoice() == null || eCopyOptionItem.getDefaultChoice().getParam() == null) {
                throw new RuntimeException("key: <" + eCopyOptionItem.getKey() + "> error");
            } else {
                eCopyOptionItem.selectChoice(ECopyOptionItem.ECopyOptionItemChoice.valueOf(eCopyOptionItemKey, ERemoteOperation.ERemoteParam.stringOf(this.presettings.getString(eCopyOptionItemKey.name(), eCopyOptionItem.getDefaultChoice().getParam().toString()))));
            }
        }

        private void saveCopyOptions(ArrayList<ECopyOptionItem> arrayList) {
            savePrePrinter();
            Iterator<ECopyOptionItem> it = arrayList.iterator();
            while (it.hasNext()) {
                ECopyOptionItem next = it.next();
                ECopyOptionItem.ECopyOptionItemKey key = next.getKey();
                if (!key.equals(ECopyOptionItem.ECopyOptionItemKey.Copies)) {
                    saveOption(key, next);
                }
            }
        }

        private void saveOption(ECopyOptionItem.ECopyOptionItemKey eCopyOptionItemKey, ECopyOptionItem eCopyOptionItem) {
            presettings = getPreferences();
            if (eCopyOptionItem.getChoiceType() == ECopyOptionItem.ECopyOptionItemChoiceType.ChoiceArray) {
                String access$000 = ActivityBase.this.TAG;
                EPLog.i(access$000, "cursetting:" + eCopyOptionItem.getSelectedChoice().name());
                editer = presettings.edit();
                editer.putString(eCopyOptionItemKey.name(), eCopyOptionItem.getSelectedChoice().getParam().toString());
                editer.commit();
                return;
            }
            String access$0002 = ActivityBase.this.TAG;
            EPLog.i(access$0002, "cursetting:" + String.valueOf(eCopyOptionItem.getSelectedValue()));
            editer = presettings.edit();
            editer.putInt(eCopyOptionItemKey.name(), eCopyOptionItem.getSelectedValue());
            editer.commit();
        }
    }
}
