package com.ifpdos.debugmenu.action;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.cvte.tv.api.aidl.ITvApiManager;
import com.cvte.vman.VmanMiddleWare;
import com.cvte.vman.impl.AtCfgControlManager;
import com.ifpdos.debugmenu.R;
import com.ifpdos.debugmenu.action.BoxInputSerialNumberAction;
import com.ifpdos.debugmenu.action.base.ItemButtonAction;
import com.ifpdos.debugmenu.event.ActivityEvent;
import com.ifpdos.debugmenu.event.ViewEvent;
import com.ifpdos.debugmenu.view.CustomEditText;
import d.f.a.c1;
import d.f.a.i2.a;
import d.f.a.r2.h3;
import d.f.a.s1.e;
import d.i.f.c.c;
import g.h0.c.g;
import g.h0.c.l;
import g.i;
import k.c.a.j;
import org.greenrobot.eventbus.ThreadMode;

/* JADX INFO: compiled from: BoxInputSerialNumberAction.kt */
/* JADX INFO: loaded from: classes.dex */
@i
@SuppressLint({"SetTextI18n"})
public final class BoxInputSerialNumberAction extends ItemButtonAction implements c1 {
    public static final Companion Companion = new Companion(null);
    private static final int DISMISS_DIALOG_DELAY_MILLIS = 1000;
    private static final String SN_LENGTH_PROPERTY = "persist.sys.sn.length";
    private static final String TAG = "BoxInputSerialNumberAction";
    private View mDialogView;
    private final Runnable mDismissDialog;
    private final Handler mHandler;
    private boolean mHasDoubleCheck;
    private String mLastInput;
    private e mQuickActionCallback;
    private final int mSnLength;
    private TextView mTipsView;
    private final VmanMiddleWare vman;

    /* JADX INFO: compiled from: BoxInputSerialNumberAction.kt */
    @i
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public BoxInputSerialNumberAction(Context context) {
        super(context);
        l.f(context, "context");
        this.mHandler = new Handler(Looper.getMainLooper());
        this.mLastInput = "";
        this.mSnLength = c.c(SN_LENGTH_PROPERTY, 0);
        this.mDismissDialog = new Runnable() { // from class: d.f.a.p1.v
            @Override // java.lang.Runnable
            public final void run() {
                BoxInputSerialNumberAction.mDismissDialog$lambda$0(this.f2947b);
            }
        };
        a.a().f2585b.add(this);
        k.c.a.c.c().k(this);
        VmanMiddleWare vmanMiddleWare = VmanMiddleWare.getInstance();
        l.e(vmanMiddleWare, "getInstance()");
        this.vman = vmanMiddleWare;
    }

    private final void doneDialog(EditText editText) {
        if (h3.q(getSn()) || d.i.f.b.a.e()) {
            return;
        }
        editText.setText(getSn());
        editText.setEnabled(false);
        TextView textView = this.mTipsView;
        if (textView == null) {
            l.m("mTipsView");
            throw null;
        }
        textView.setText(R.string.sn_has_input_successfully);
        View view = this.mDialogView;
        if (view == null) {
            l.m("mDialogView");
            throw null;
        }
        View viewFindViewById = view.findViewById(R.id.confirm_button);
        l.e(viewFindViewById, "mDialogView.findViewById…iew>(R.id.confirm_button)");
        c.n.a.G(viewFindViewById);
    }

    private final String getSn() {
        String string = this.vman.getAtCfgControl().getString(AtCfgControlManager.IDX_MACHINE_SN);
        if (string == null) {
            string = "";
        }
        d.b.a.a.a.N("getSn from vman sn = ", string, TAG);
        return string;
    }

    /* JADX WARN: Removed duplicated region for block: B:30:0x0050  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private final boolean isItemCanClicked(java.lang.String r8) {
        /*
            r7 = this;
            int r0 = r8.length()
            r1 = 0
            r2 = 1
            if (r0 != 0) goto La
            r0 = r2
            goto Lb
        La:
            r0 = r1
        Lb:
            if (r0 != 0) goto L50
            int r0 = r8.length()
            int r0 = r0 - r2
            r3 = r1
            r4 = r3
        L14:
            if (r3 > r0) goto L39
            if (r4 != 0) goto L1a
            r5 = r3
            goto L1b
        L1a:
            r5 = r0
        L1b:
            char r5 = r8.charAt(r5)
            r6 = 32
            int r5 = g.h0.c.l.h(r5, r6)
            if (r5 > 0) goto L29
            r5 = r2
            goto L2a
        L29:
            r5 = r1
        L2a:
            if (r4 != 0) goto L33
            if (r5 != 0) goto L30
            r4 = r2
            goto L14
        L30:
            int r3 = r3 + 1
            goto L14
        L33:
            if (r5 != 0) goto L36
            goto L39
        L36:
            int r0 = r0 + (-1)
            goto L14
        L39:
            int r0 = r0 + r2
            java.lang.CharSequence r8 = r8.subSequence(r3, r0)
            java.lang.String r8 = r8.toString()
            int r8 = r8.length()
            if (r8 != 0) goto L4a
            r8 = r2
            goto L4b
        L4a:
            r8 = r1
        L4b:
            if (r8 == 0) goto L4e
            goto L50
        L4e:
            r8 = r1
            goto L51
        L50:
            r8 = r2
        L51:
            boolean r0 = d.i.f.b.a.e()
            if (r8 != 0) goto L59
            if (r0 == 0) goto L5a
        L59:
            r1 = r2
        L5a:
            java.lang.StringBuilder r2 = new java.lang.StringBuilder
            r2.<init>()
            java.lang.String r3 = "isItemCanClicked: isSnEmpty = "
            r2.append(r3)
            r2.append(r8)
            java.lang.String r8 = ", isProwise = "
            r2.append(r8)
            r2.append(r0)
            java.lang.String r8 = ", canClick = "
            r2.append(r8)
            r2.append(r1)
            java.lang.String r8 = r2.toString()
            java.lang.String r0 = "BoxInputSerialNumberAction"
            android.util.Log.d(r0, r8)
            return r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ifpdos.debugmenu.action.BoxInputSerialNumberAction.isItemCanClicked(java.lang.String):boolean");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void mDismissDialog$lambda$0(BoxInputSerialNumberAction boxInputSerialNumberAction) {
        l.f(boxInputSerialNumberAction, "this$0");
        d.b.a.a.a.X(k.c.a.c.c());
        e eVar = boxInputSerialNumberAction.mQuickActionCallback;
        if (eVar != null) {
            eVar.call();
        }
        boxInputSerialNumberAction.mQuickActionCallback = null;
    }

    private final void performConfirm(EditText editText, e eVar) {
        String string = editText.getText().toString();
        int length = string.length() - 1;
        int i2 = 0;
        boolean z = false;
        while (i2 <= length) {
            boolean z2 = l.h(string.charAt(!z ? i2 : length), 32) <= 0;
            if (z) {
                if (!z2) {
                    break;
                } else {
                    length--;
                }
            } else if (z2) {
                i2++;
            } else {
                z = true;
            }
        }
        if (setSerialNumber(string.subSequence(i2, length + 1).toString())) {
            eVar.call();
        }
    }

    private final boolean setMachineSN(String str) {
        boolean string = this.vman.getAtCfgControl().setString(AtCfgControlManager.IDX_MACHINE_SN, str);
        d.b.a.a.a.P("setMachineSN: ", string, TAG);
        return string;
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final boolean setSerialNumber(String str) {
        View view = this.mDialogView;
        if (view == null) {
            l.m("mDialogView");
            throw null;
        }
        final EditText editText = (EditText) view.findViewById(R.id.edit_text);
        this.mHandler.postDelayed(new Runnable() { // from class: d.f.a.p1.q
            @Override // java.lang.Runnable
            public final void run() {
                editText.requestFocus();
            }
        }, 200L);
        editText.setText("");
        if (this.mSnLength > 0) {
            int length = str.length();
            int i2 = this.mSnLength;
            if (length != i2) {
                String string = this.mContext.getString(R.string.str_debug_sn_length_illegal, Integer.valueOf(i2));
                l.e(string, "mContext.getString(R.str…ength_illegal, mSnLength)");
                editText.setHint(string);
                k.c.a.c.c().g(new ActivityEvent.ShowToast(string));
                editText.setHint(string);
                return false;
            }
        }
        if (!c.n.a.f(str)) {
            editText.setHint(R.string.str_debug_sn_illegal);
            d.b.a.a.a.J(R.string.str_debug_sn_illegal, k.c.a.c.c());
            editText.setHint(this.mContext.getResources().getString(R.string.str_debug_sn_illegal));
            return false;
        }
        if (str.length() > 30) {
            editText.setHint(R.string.str_debug_sn_too_long);
            d.b.a.a.a.J(R.string.str_debug_sn_too_long, k.c.a.c.c());
            editText.setHint(this.mContext.getResources().getString(R.string.str_debug_sn_illegal));
            return false;
        }
        if (!this.mHasDoubleCheck) {
            this.mLastInput = str;
            this.mHasDoubleCheck = true;
            TextView textView = this.mTipsView;
            if (textView == null) {
                l.m("mTipsView");
                throw null;
            }
            textView.setText(this.mContext.getString(R.string.str_debug_sn_input_confirm_again_tips));
            editText.setHint(this.mContext.getString(R.string.str_debug_sn_input_title));
            return false;
        }
        if (!l.a(this.mLastInput, str)) {
            StringBuilder sbB = d.b.a.a.a.B("mLastInput:");
            sbB.append(this.mLastInput);
            sbB.append(" sn:");
            sbB.append(str);
            Log.i(TAG, sbB.toString());
            this.mLastInput = "";
            this.mHasDoubleCheck = false;
            TextView textView2 = this.mTipsView;
            if (textView2 != null) {
                textView2.setText(this.mContext.getString(R.string.str_debug_sn_input_not_match_tips));
                return false;
            }
            l.m("mTipsView");
            throw null;
        }
        this.mHasDoubleCheck = false;
        if (!setMachineSN(str)) {
            TextView textView3 = this.mTipsView;
            if (textView3 != null) {
                textView3.setText(this.mContext.getString(R.string.str_set_sn_fail));
                return false;
            }
            l.m("mTipsView");
            throw null;
        }
        String sn = getSn();
        if (!(sn.length() > 0)) {
            return false;
        }
        VM vm = this.mViewModel;
        if (vm != 0) {
            ((d.f.a.j2.e.e) vm).f2607h.i(sn);
            ((d.f.a.j2.e.e) this.mViewModel).f2610k.i(-7829368);
            ((d.f.a.j2.e.e) this.mViewModel).d(false);
        }
        d.b.a.a.a.J(R.string.str_debug_sn_done, k.c.a.c.c());
        return true;
    }

    private final void showSNInputDialog(e eVar) {
        this.mQuickActionCallback = eVar;
        this.mLastInput = "";
        this.mHasDoubleCheck = false;
        View viewInflate = LayoutInflater.from(this.mContext).inflate(R.layout.serial_number_dialog, (ViewGroup) null, false);
        l.e(viewInflate, "from(mContext)\n         …mber_dialog, null, false)");
        this.mDialogView = viewInflate;
        if (viewInflate == null) {
            l.m("mDialogView");
            throw null;
        }
        final CustomEditText customEditText = (CustomEditText) viewInflate.findViewById(R.id.edit_text);
        View view = this.mDialogView;
        if (view == null) {
            l.m("mDialogView");
            throw null;
        }
        View viewFindViewById = view.findViewById(R.id.tips_view);
        l.e(viewFindViewById, "mDialogView.findViewById(R.id.tips_view)");
        this.mTipsView = (TextView) viewFindViewById;
        if (d.i.f.b.a.e()) {
            TextView textView = this.mTipsView;
            if (textView == null) {
                l.m("mTipsView");
                throw null;
            }
            textView.setText("");
        }
        customEditText.setSingleLine();
        customEditText.setInputType(ITvApiManager.Stub.TRANSACTION_getTVApiVChipCanadaRating);
        customEditText.setImeOptions(6);
        customEditText.requestFocus();
        customEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { // from class: d.f.a.p1.p
            @Override // android.widget.TextView.OnEditorActionListener
            public final boolean onEditorAction(TextView textView2, int i2, KeyEvent keyEvent) {
                return BoxInputSerialNumberAction.showSNInputDialog$lambda$3(customEditText, this, textView2, i2, keyEvent);
            }
        });
        customEditText.addTextChangedListener(new TextWatcher() { // from class: com.ifpdos.debugmenu.action.BoxInputSerialNumberAction.showSNInputDialog.2
            @Override // android.text.TextWatcher
            public void afterTextChanged(Editable editable) {
                l.f(editable, "s");
            }

            @Override // android.text.TextWatcher
            public void beforeTextChanged(CharSequence charSequence, int i2, int i3, int i4) {
                l.f(charSequence, "s");
            }

            @Override // android.text.TextWatcher
            public void onTextChanged(CharSequence charSequence, int i2, int i3, int i4) {
                l.f(charSequence, "s");
                TextView textView2 = BoxInputSerialNumberAction.this.mTipsView;
                if (textView2 == null) {
                    l.m("mTipsView");
                    throw null;
                }
                textView2.setText(BoxInputSerialNumberAction.this.mContext.getString(R.string.sn_length) + customEditText.length());
            }
        });
        customEditText.setOnKeyDownListener(new CustomEditText.a() { // from class: d.f.a.p1.u
            @Override // com.ifpdos.debugmenu.view.CustomEditText.a
            public final boolean a(KeyEvent keyEvent) {
                return BoxInputSerialNumberAction.showSNInputDialog$lambda$5(customEditText, this, keyEvent);
            }
        });
        View view2 = this.mDialogView;
        if (view2 == null) {
            l.m("mDialogView");
            throw null;
        }
        view2.findViewById(R.id.confirm_button).setOnClickListener(new View.OnClickListener() { // from class: d.f.a.p1.n
            @Override // android.view.View.OnClickListener
            public final void onClick(View view3) {
                BoxInputSerialNumberAction.showSNInputDialog$lambda$7(this.f2830b, customEditText, view3);
            }
        });
        View view3 = this.mDialogView;
        if (view3 == null) {
            l.m("mDialogView");
            throw null;
        }
        view3.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { // from class: d.f.a.p1.r
            @Override // android.view.View.OnClickListener
            public final void onClick(View view4) {
                BoxInputSerialNumberAction.showSNInputDialog$lambda$8(view4);
            }
        });
        l.e(customEditText, "snInput");
        updateDialog(customEditText);
        Object systemService = this.mContext.getSystemService("input_method");
        l.d(systemService, "null cannot be cast to non-null type android.view.inputmethod.InputMethodManager");
        ((InputMethodManager) systemService).showSoftInputFromInputMethod(customEditText.getWindowToken(), 0);
        k.c.a.c cVarC = k.c.a.c.c();
        View view4 = this.mDialogView;
        if (view4 != null) {
            cVarC.g(new ActivityEvent.ShowNormalDialog(view4, new DialogInterface.OnCancelListener() { // from class: d.f.a.p1.s
                @Override // android.content.DialogInterface.OnCancelListener
                public final void onCancel(DialogInterface dialogInterface) {
                    BoxInputSerialNumberAction.showSNInputDialog$lambda$9(this.f2917b, dialogInterface);
                }
            }));
        } else {
            l.m("mDialogView");
            throw null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean showSNInputDialog$lambda$3(final CustomEditText customEditText, final BoxInputSerialNumberAction boxInputSerialNumberAction, TextView textView, int i2, KeyEvent keyEvent) {
        l.f(boxInputSerialNumberAction, "this$0");
        Log.i(TAG, "setOnEditorActionListener：" + customEditText.length());
        l.e(customEditText, "snInput");
        boxInputSerialNumberAction.performConfirm(customEditText, new e() { // from class: d.f.a.p1.o
            @Override // d.f.a.s1.e
            public final void call() {
                BoxInputSerialNumberAction.showSNInputDialog$lambda$3$lambda$2(this.a, customEditText);
            }
        });
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void showSNInputDialog$lambda$3$lambda$2(BoxInputSerialNumberAction boxInputSerialNumberAction, CustomEditText customEditText) {
        l.f(boxInputSerialNumberAction, "this$0");
        l.e(customEditText, "snInput");
        boxInputSerialNumberAction.updateDialog(customEditText);
        boxInputSerialNumberAction.mHandler.postDelayed(boxInputSerialNumberAction.mDismissDialog, 1000L);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean showSNInputDialog$lambda$5(final CustomEditText customEditText, final BoxInputSerialNumberAction boxInputSerialNumberAction, KeyEvent keyEvent) {
        l.f(boxInputSerialNumberAction, "this$0");
        l.f(keyEvent, "event");
        if (keyEvent.getKeyCode() != 23) {
            return false;
        }
        StringBuilder sbB = d.b.a.a.a.B("showSNInputDialog: ");
        sbB.append((Object) customEditText.getText());
        Log.d(TAG, sbB.toString());
        l.e(customEditText, "snInput");
        boxInputSerialNumberAction.performConfirm(customEditText, new e() { // from class: d.f.a.p1.w
            @Override // d.f.a.s1.e
            public final void call() {
                BoxInputSerialNumberAction.showSNInputDialog$lambda$5$lambda$4(this.a, customEditText);
            }
        });
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void showSNInputDialog$lambda$5$lambda$4(BoxInputSerialNumberAction boxInputSerialNumberAction, CustomEditText customEditText) {
        l.f(boxInputSerialNumberAction, "this$0");
        l.e(customEditText, "snInput");
        boxInputSerialNumberAction.updateDialog(customEditText);
        boxInputSerialNumberAction.mHandler.postDelayed(boxInputSerialNumberAction.mDismissDialog, 1000L);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void showSNInputDialog$lambda$7(final BoxInputSerialNumberAction boxInputSerialNumberAction, final CustomEditText customEditText, View view) {
        l.f(boxInputSerialNumberAction, "this$0");
        l.e(customEditText, "snInput");
        boxInputSerialNumberAction.performConfirm(customEditText, new e() { // from class: d.f.a.p1.t
            @Override // d.f.a.s1.e
            public final void call() {
                BoxInputSerialNumberAction.showSNInputDialog$lambda$7$lambda$6(this.a, customEditText);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void showSNInputDialog$lambda$7$lambda$6(BoxInputSerialNumberAction boxInputSerialNumberAction, CustomEditText customEditText) {
        l.f(boxInputSerialNumberAction, "this$0");
        l.e(customEditText, "snInput");
        boxInputSerialNumberAction.doneDialog(customEditText);
        boxInputSerialNumberAction.mHandler.postDelayed(boxInputSerialNumberAction.mDismissDialog, 1000L);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void showSNInputDialog$lambda$8(View view) {
        d.b.a.a.a.W(k.c.a.c.c());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void showSNInputDialog$lambda$9(BoxInputSerialNumberAction boxInputSerialNumberAction, DialogInterface dialogInterface) {
        l.f(boxInputSerialNumberAction, "this$0");
        e eVar = boxInputSerialNumberAction.mQuickActionCallback;
        if (eVar != null) {
            eVar.call();
        }
        boxInputSerialNumberAction.mQuickActionCallback = null;
    }

    private final void updateDialog(EditText editText) {
        if (h3.q(getSn()) || d.i.f.b.a.e()) {
            return;
        }
        editText.setText(getSn());
        editText.setEnabled(false);
        TextView textView = this.mTipsView;
        if (textView == null) {
            l.m("mTipsView");
            throw null;
        }
        textView.setText(R.string.sn_has_input_cant_redo);
        View view = this.mDialogView;
        if (view == null) {
            l.m("mDialogView");
            throw null;
        }
        View viewFindViewById = view.findViewById(R.id.confirm_button);
        l.e(viewFindViewById, "mDialogView.findViewById…iew>(R.id.confirm_button)");
        c.n.a.G(viewFindViewById);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction
    public String getValue(Context context) {
        l.f(context, "context");
        setColor(-7829368);
        String sn = getSn();
        if (!isItemCanClicked(sn)) {
            return sn;
        }
        String string = this.mContext.getString(R.string.tips_input_sn);
        l.e(string, "mContext.getString(R.string.tips_input_sn)");
        return string;
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction
    public boolean isEnable() {
        return (getSn().length() == 0) || d.i.f.b.a.e();
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, d.f.a.j2.e.e.a
    public void onClick(Context context, d.f.a.j2.e.e eVar) {
        l.f(context, "context");
        l.f(eVar, "vm");
        Log.e(TAG, "onClick called");
        if (isItemCanClicked(getSn())) {
            Log.d(TAG, "onClick: showSNInputDialog, you can type in SN No. and burn it into machine.");
            showSNInputDialog(null);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    @j(threadMode = ThreadMode.MAIN)
    public final void onEvent(ViewEvent.RefreshSerialNumber refreshSerialNumber) {
        Log.e(TAG, "onEvent: 清空产品序列号");
        d.f.a.j2.e.e eVar = (d.f.a.j2.e.e) this.mViewModel;
        eVar.f2607h.i(this.mContext.getString(R.string.tips_input_sn));
        ((d.f.a.j2.e.e) this.mViewModel).d(true);
        setColor(-7829368);
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, d.f.a.j2.e.e.a
    public void onLongClick(Context context, d.f.a.j2.e.e eVar) {
    }

    @Override // d.f.a.c1
    public void quickAction(e eVar) {
        l.f(eVar, "callback");
        showSNInputDialog(eVar);
    }
}
