package ca.canadapost.core.common.customView;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.a;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.messaging.Constants;
import java.util.Arrays;
import kotlin.Metadata;
import kotlin.jvm.internal.u0;
import rm.n0;

@Metadata(d1 = {"\u0000Z\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\b\r\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\b\b\u0007\u0018\u0000 @2\u00020\u0001:\u0002ABB'\b\u0007\u0012\u0006\u0010:\u001a\u000209\u0012\n\b\u0002\u0010<\u001a\u0004\u0018\u00010;\u0012\b\b\u0002\u0010=\u001a\u00020\u0004¢\u0006\u0004\b>\u0010?J\b\u0010\u0003\u001a\u00020\u0002H\u0002J\u000e\u0010\u0006\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u0004J\u0010\u0010\t\u001a\u00020\u00022\b\u0010\b\u001a\u0004\u0018\u00010\u0007J\u0010\u0010\u000b\u001a\u00020\u00022\b\u0010\n\u001a\u0004\u0018\u00010\u0007J\u0006\u0010\f\u001a\u00020\u0007J\u0010\u0010\u000e\u001a\u00020\u00022\b\u0010\r\u001a\u0004\u0018\u00010\u0007J\n\u0010\u0010\u001a\u00060\u000fR\u00020\u0000J#\u0010\u0014\u001a\u00020\u0002\"\f\b\u0000\u0010\u0012*\u0006\u0012\u0002\b\u00030\u00112\u0006\u0010\u0013\u001a\u00028\u0000¢\u0006\u0004\b\u0014\u0010\u0015J\f\u0010\u0016\u001a\b\u0012\u0002\b\u0003\u0018\u00010\u0011J\u000e\u0010\u0018\u001a\u00020\u00022\u0006\u0010\u0017\u001a\u00020\u0004J\u000e\u0010\u001b\u001a\u00020\u00022\u0006\u0010\u001a\u001a\u00020\u0019R\"\u0010\u0005\u001a\u00020\u00048\u0006@\u0006X\u0086\u000e¢\u0006\u0012\n\u0004\b\u001c\u0010\u001d\u001a\u0004\b\u001e\u0010\u001f\"\u0004\b \u0010!R\u001b\u0010&\u001a\u00060\u000fR\u00020\u00008\u0006¢\u0006\f\n\u0004\b\"\u0010#\u001a\u0004\b$\u0010%R\u001b\u0010,\u001a\u00020'8BX\u0082\u0084\u0002¢\u0006\f\n\u0004\b(\u0010)\u001a\u0004\b*\u0010+R\u001b\u00101\u001a\u00020-8BX\u0082\u0084\u0002¢\u0006\f\n\u0004\b.\u0010)\u001a\u0004\b/\u00100R\u001b\u0010\n\u001a\u0002028BX\u0082\u0084\u0002¢\u0006\f\n\u0004\b3\u0010)\u001a\u0004\b4\u00105R\u001b\u00108\u001a\u0002028BX\u0082\u0084\u0002¢\u0006\f\n\u0004\b6\u0010)\u001a\u0004\b7\u00105¨\u0006C"}, d2 = {"Lca/canadapost/core/common/customView/CpcDropdownInput;", "Landroid/widget/LinearLayout;", "Lrm/n0;", "g", "", "mode", "setDisplayMode", "", Constants.IPC_BUNDLE_KEY_SEND_ERROR, "setError", Constants.ScionAnalytics.PARAM_LABEL, "setLabel", "getText", "text", "setText", "Lca/canadapost/core/common/customView/CpcDropdownInput$b;", "getDialog", "Lca/canadapost/core/common/customView/i;", "T", "adapter", "setAdapter", "(Lca/canadapost/core/common/customView/i;)V", "getAdapter", FirebaseAnalytics.Param.INDEX, "setSelection", "Landroid/widget/AdapterView$OnItemClickListener;", "onItemClickListener", "setOnItemClickListener", "p", "I", "getMode", "()I", "setMode", "(I)V", "q", "Lca/canadapost/core/common/customView/CpcDropdownInput$b;", "getDialogPopup", "()Lca/canadapost/core/common/customView/CpcDropdownInput$b;", "dialogPopup", "Lcom/google/android/material/textfield/TextInputLayout;", "r", "Lrm/o;", "getTextInputLayout", "()Lcom/google/android/material/textfield/TextInputLayout;", "textInputLayout", "Landroid/widget/AutoCompleteTextView;", "s", "getTextView", "()Landroid/widget/AutoCompleteTextView;", "textView", "Landroid/widget/TextView;", "t", "getLabel", "()Landroid/widget/TextView;", "u", "getContentDesc", "contentDesc", "Landroid/content/Context;", "context", "Landroid/util/AttributeSet;", "attrs", "defStyle", "<init>", "(Landroid/content/Context;Landroid/util/AttributeSet;I)V", "v", "a", "b", "core_common_productionRelease"}, k = 1, mv = {1, 9, 0})
/* loaded from: classes.dex */
public final class CpcDropdownInput extends LinearLayout {

    /* renamed from: x, reason: collision with root package name */
    private static final int f7554x = 0;

    /* renamed from: p, reason: collision with root package name and from kotlin metadata */
    private int mode;

    /* renamed from: q, reason: collision with root package name and from kotlin metadata */
    private final b dialogPopup;

    /* renamed from: r, reason: collision with root package name and from kotlin metadata */
    private final rm.o textInputLayout;

    /* renamed from: s, reason: collision with root package name and from kotlin metadata */
    private final rm.o textView;

    /* renamed from: t, reason: collision with root package name and from kotlin metadata */
    private final rm.o label;

    /* renamed from: u, reason: collision with root package name and from kotlin metadata */
    private final rm.o contentDesc;

    /* renamed from: v, reason: collision with root package name and from kotlin metadata */
    public static final Companion INSTANCE = new Companion(null);

    /* renamed from: w, reason: collision with root package name */
    public static final int f7553w = 8;

    /* renamed from: y, reason: collision with root package name */
    private static final int f7555y = 1;

    /* renamed from: ca.canadapost.core.common.customView.CpcDropdownInput$a, reason: from kotlin metadata */
    /* loaded from: classes.dex */
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(kotlin.jvm.internal.k kVar) {
            this();
        }

        public final int a() {
            return CpcDropdownInput.f7555y;
        }
    }

    /* loaded from: classes.dex */
    public final class b implements DialogInterface.OnClickListener {

        /* renamed from: p, reason: collision with root package name */
        private androidx.appcompat.app.a f7562p;

        /* renamed from: q, reason: collision with root package name */
        private i f7563q;

        /* renamed from: r, reason: collision with root package name */
        private int f7564r;

        /* renamed from: s, reason: collision with root package name */
        private AdapterView.OnItemClickListener f7565s;

        public b() {
        }

        public final void a() {
            androidx.appcompat.app.a aVar = this.f7562p;
            if (aVar != null) {
                kotlin.jvm.internal.t.d(aVar);
                aVar.dismiss();
                this.f7562p = null;
            }
        }

        public final i b() {
            return this.f7563q;
        }

        public final void c() {
            Object systemService = CpcDropdownInput.this.getContext().getApplicationContext().getSystemService("input_method");
            kotlin.jvm.internal.t.e(systemService, "null cannot be cast to non-null type android.view.inputmethod.InputMethodManager");
            ((InputMethodManager) systemService).hideSoftInputFromWindow(CpcDropdownInput.this.getWindowToken(), 0);
        }

        public final boolean d() {
            androidx.appcompat.app.a aVar = this.f7562p;
            if (aVar == null) {
                return false;
            }
            kotlin.jvm.internal.t.d(aVar);
            return aVar.isShowing();
        }

        public final void e(i iVar) {
            this.f7563q = iVar;
            if (iVar == null || iVar.getCount() <= 1) {
                return;
            }
            g(this.f7564r);
        }

        public final void f(AdapterView.OnItemClickListener onItemClickListener) {
            this.f7565s = onItemClickListener;
        }

        public final void g(int i10) {
            i iVar = this.f7563q;
            if (iVar != null) {
                CpcDropdownInput cpcDropdownInput = CpcDropdownInput.this;
                this.f7564r = i10;
                cpcDropdownInput.getTextView().setText(String.valueOf(iVar.getItem(i10)));
            }
        }

        public final void h() {
            if (d() || this.f7563q == null) {
                return;
            }
            a.C0023a c0023a = new a.C0023a(CpcDropdownInput.this.getContext(), h7.m.f21243a);
            if (CpcDropdownInput.this.getLabel() != null) {
                View inflate = View.inflate(CpcDropdownInput.this.getContext(), h7.j.f21097l, null);
                TextView textView = (TextView) inflate.findViewById(h7.i.L0);
                textView.setText(CpcDropdownInput.this.getLabel().getText());
                u0 u0Var = u0.f26205a;
                String string = CpcDropdownInput.this.getResources().getString(h7.l.f21242z3);
                kotlin.jvm.internal.t.f(string, "getString(...)");
                String format = String.format(string, Arrays.copyOf(new Object[]{CpcDropdownInput.this.getLabel().getText()}, 1));
                kotlin.jvm.internal.t.f(format, "format(...)");
                textView.setContentDescription(format);
                c0023a.e(inflate);
            }
            c();
            androidx.appcompat.app.a a10 = c0023a.o(this.f7563q, this.f7564r, this).a();
            this.f7562p = a10;
            if (a10 != null) {
                a10.show();
            }
        }

        @Override // android.content.DialogInterface.OnClickListener
        public void onClick(DialogInterface dialog, int i10) {
            kotlin.jvm.internal.t.g(dialog, "dialog");
            g(i10);
            AdapterView.OnItemClickListener onItemClickListener = this.f7565s;
            if (onItemClickListener != null) {
                androidx.appcompat.app.a aVar = this.f7562p;
                ListView f10 = aVar != null ? aVar.f() : null;
                i iVar = this.f7563q;
                kotlin.jvm.internal.t.d(iVar);
                onItemClickListener.onItemClick(f10, null, i10, iVar.getItemId(i10));
            }
            a();
        }
    }

    /* loaded from: classes.dex */
    static final class c extends kotlin.jvm.internal.v implements dn.a {
        c() {
            super(0);
        }

        @Override // dn.a
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final TextView invoke() {
            return (TextView) CpcDropdownInput.this.findViewById(h7.i.f21072t);
        }
    }

    /* loaded from: classes.dex */
    static final class d extends kotlin.jvm.internal.v implements dn.a {
        d() {
            super(0);
        }

        @Override // dn.a
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final TextView invoke() {
            return (TextView) CpcDropdownInput.this.findViewById(h7.i.I);
        }
    }

    /* loaded from: classes.dex */
    static final class e extends kotlin.jvm.internal.v implements dn.a {
        e() {
            super(0);
        }

        @Override // dn.a
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final TextInputLayout invoke() {
            return (TextInputLayout) CpcDropdownInput.this.findViewById(h7.i.J0);
        }
    }

    /* loaded from: classes.dex */
    static final class f extends kotlin.jvm.internal.v implements dn.a {
        f() {
            super(0);
        }

        @Override // dn.a
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final AutoCompleteTextView invoke() {
            return (AutoCompleteTextView) CpcDropdownInput.this.findViewById(h7.i.K0);
        }
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public CpcDropdownInput(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0, 4, null);
        kotlin.jvm.internal.t.g(context, "context");
    }

    private final void g() {
        if (this.mode != f7555y) {
            getTextInputLayout().setEndIconOnClickListener(new View.OnClickListener() { // from class: ca.canadapost.core.common.customView.h
                @Override // android.view.View.OnClickListener
                public final void onClick(View view) {
                    CpcDropdownInput.j(CpcDropdownInput.this, view);
                }
            });
            return;
        }
        getTextView().setInputType(0);
        getTextView().setOnClickListener(new View.OnClickListener() { // from class: ca.canadapost.core.common.customView.f
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                CpcDropdownInput.h(CpcDropdownInput.this, view);
            }
        });
        getTextInputLayout().setEndIconOnClickListener(new View.OnClickListener() { // from class: ca.canadapost.core.common.customView.g
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                CpcDropdownInput.i(CpcDropdownInput.this, view);
            }
        });
    }

    private final TextView getContentDesc() {
        Object value = this.contentDesc.getValue();
        kotlin.jvm.internal.t.f(value, "getValue(...)");
        return (TextView) value;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final TextView getLabel() {
        Object value = this.label.getValue();
        kotlin.jvm.internal.t.f(value, "getValue(...)");
        return (TextView) value;
    }

    private final TextInputLayout getTextInputLayout() {
        Object value = this.textInputLayout.getValue();
        kotlin.jvm.internal.t.f(value, "getValue(...)");
        return (TextInputLayout) value;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final AutoCompleteTextView getTextView() {
        Object value = this.textView.getValue();
        kotlin.jvm.internal.t.f(value, "getValue(...)");
        return (AutoCompleteTextView) value;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void h(CpcDropdownInput this$0, View view) {
        kotlin.jvm.internal.t.g(this$0, "this$0");
        this$0.getDialogPopup().h();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void i(CpcDropdownInput this$0, View view) {
        kotlin.jvm.internal.t.g(this$0, "this$0");
        this$0.getDialogPopup().h();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void j(CpcDropdownInput this$0, View view) {
        kotlin.jvm.internal.t.g(this$0, "this$0");
        this$0.getTextView().showDropDown();
    }

    public final i getAdapter() {
        int i10 = this.mode;
        if (i10 == f7554x) {
            ListAdapter adapter = getTextView().getAdapter();
            kotlin.jvm.internal.t.e(adapter, "null cannot be cast to non-null type ca.canadapost.core.common.customView.CpcDropdownInputAdapter<*>");
            return (i) adapter;
        }
        if (i10 != f7555y) {
            return null;
        }
        i b10 = getDialogPopup().b();
        kotlin.jvm.internal.t.e(b10, "null cannot be cast to non-null type ca.canadapost.core.common.customView.CpcDropdownInputAdapter<*>");
        return b10;
    }

    /* renamed from: getDialog, reason: from getter */
    public final b getDialogPopup() {
        return this.dialogPopup;
    }

    public final b getDialogPopup() {
        return this.dialogPopup;
    }

    public final int getMode() {
        return this.mode;
    }

    public final String getText() {
        return getTextView().getText().toString();
    }

    public final <T extends i> void setAdapter(T adapter) {
        kotlin.jvm.internal.t.g(adapter, "adapter");
        int i10 = this.mode;
        if (i10 == f7554x) {
            adapter.c(h7.j.f21099n);
            getTextView().setAdapter(adapter);
        } else if (i10 == f7555y) {
            adapter.c(h7.j.f21096k);
            getDialogPopup().e(adapter);
        }
    }

    public final void setDisplayMode(int i10) {
        this.mode = i10;
        g();
    }

    public final void setError(String str) {
        n0 n0Var;
        if (str != null) {
            TextInputLayout textInputLayout = getTextInputLayout();
            u0 u0Var = u0.f26205a;
            String string = getContext().getString(h7.l.f21209t0);
            kotlin.jvm.internal.t.f(string, "getString(...)");
            String format = String.format(string, Arrays.copyOf(new Object[]{str}, 1));
            kotlin.jvm.internal.t.f(format, "format(...)");
            Context context = getContext();
            kotlin.jvm.internal.t.d(context);
            textInputLayout.setError(Html.fromHtml(format, 0, new w8.s(context), null));
            n0Var = n0.f34904a;
        } else {
            n0Var = null;
        }
        if (n0Var == null) {
            getTextInputLayout().setError(null);
        }
    }

    public final void setLabel(String str) {
        if (str != null) {
            getLabel().setText(str);
            getContentDesc().setText(str);
        }
    }

    public final void setMode(int i10) {
        this.mode = i10;
    }

    public final void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        kotlin.jvm.internal.t.g(onItemClickListener, "onItemClickListener");
        int i10 = this.mode;
        if (i10 == f7554x) {
            getTextView().setOnItemClickListener(onItemClickListener);
        } else if (i10 == f7555y) {
            getDialogPopup().f(onItemClickListener);
        }
    }

    public final void setSelection(int i10) {
        int i11 = this.mode;
        if (i11 == f7554x) {
            getTextView().setSelection(i10);
        } else if (i11 == f7555y) {
            getDialogPopup().g(i10);
        }
    }

    public final void setText(String str) {
        getTextView().setText(str);
    }

    public /* synthetic */ CpcDropdownInput(Context context, AttributeSet attributeSet, int i10, int i11, kotlin.jvm.internal.k kVar) {
        this(context, (i11 & 2) != 0 ? null : attributeSet, (i11 & 4) != 0 ? 0 : i10);
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public CpcDropdownInput(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        rm.o a10;
        rm.o a11;
        rm.o a12;
        rm.o a13;
        kotlin.jvm.internal.t.g(context, "context");
        int i11 = f7555y;
        this.mode = i11;
        this.dialogPopup = new b();
        a10 = rm.q.a(new e());
        this.textInputLayout = a10;
        a11 = rm.q.a(new f());
        this.textView = a11;
        a12 = rm.q.a(new d());
        this.label = a12;
        a13 = rm.q.a(new c());
        this.contentDesc = a13;
        LayoutInflater.from(context).inflate(h7.j.f21098m, (ViewGroup) this, true);
        setOrientation(1);
        if (attributeSet != null) {
            TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, h7.n.f21304l0, 0, 0);
            kotlin.jvm.internal.t.f(obtainStyledAttributes, "obtainStyledAttributes(...)");
            setLabel(obtainStyledAttributes.getString(h7.n.f21319o0));
            getLabel().setTextColor(obtainStyledAttributes.getColor(h7.n.f21324p0, androidx.core.content.a.getColor(context, h7.f.f20969j)));
            getTextInputLayout().setHelperText(obtainStyledAttributes.getString(h7.n.f21314n0));
            getTextInputLayout().setEndIconCheckable(false);
            String string = obtainStyledAttributes.getString(h7.n.f21309m0);
            getTextInputLayout().setEndIconContentDescription(string == null ? context.getString(h7.l.f21176m2) : string);
            this.mode = obtainStyledAttributes.getInt(h7.n.f21329q0, i11);
            obtainStyledAttributes.recycle();
        }
        setFocusable(false);
        getTextView().setKeyListener(null);
        getTextView().setInputType(0);
        getTextView().setFocusable(0);
        g();
    }
}
