package ca.canadapost.module.addresscomplete;

import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.appcompat.widget.SearchView;
import androidx.core.os.c;
import androidx.fragment.app.k;
import androidx.lifecycle.c1;
import androidx.lifecycle.u;
import androidx.lifecycle.z0;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.g;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import ca.canadapost.core.common.ui.CpcToolbar;
import ca.canadapost.core.data.addresscomplete.webservice.AddressCompleteFindItem;
import ca.canadapost.core.data.model.CoreConstants;
import ca.canadapost.core.data.model.Event;
import ca.canadapost.module.addresscomplete.AddressCompleteFragment;
import com.google.android.gms.actions.SearchIntents;
import com.google.android.gms.common.api.Api;
import com.google.firebase.analytics.FirebaseAnalytics;
import h.f;
import java.util.List;
import kotlin.Metadata;
import kotlin.jvm.internal.t;
import rm.c0;
import s8.o;
import s8.p;
import u7.x;
import w8.d0;
import w8.r;
import w8.u;
import w8.w;
import x4.d;
import za.f0;
import za.g0;
import za.h0;
import za.i0;
import za.j0;

@Metadata(d1 = {"\u0000z\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\f\u0018\u00002\u00020\u00012\b\u0012\u0004\u0012\u00020\u00030\u00022\u00020\u0004B\u0007¢\u0006\u0004\b>\u0010?J5\u0010\u0010\u001a\u00020\u000f2\u0006\u0010\u0006\u001a\u00020\u00052\b\u0010\b\u001a\u0004\u0018\u00010\u00072\b\u0010\n\u001a\u0004\u0018\u00010\t2\u0006\u0010\f\u001a\u00020\u000b2\u0006\u0010\u000e\u001a\u00020\rH\u0096\u0001J\u0012\u0010\u0012\u001a\u00020\u00112\b\u0010\n\u001a\u0004\u0018\u00010\tH\u0016J$\u0010\u0013\u001a\u00020\u000f2\u0006\u0010\u0006\u001a\u00020\u00052\b\u0010\b\u001a\u0004\u0018\u00010\u00072\b\u0010\n\u001a\u0004\u0018\u00010\tH\u0016J\u001a\u0010\u0015\u001a\u00020\u00112\u0006\u0010\u0014\u001a\u00020\u000f2\b\u0010\n\u001a\u0004\u0018\u00010\tH\u0017J\b\u0010\u0016\u001a\u00020\u0011H\u0016J\u0018\u0010\u001a\u001a\u00020\u00112\u0006\u0010\u0018\u001a\u00020\u00172\u0006\u0010\u0006\u001a\u00020\u0019H\u0016J\u0010\u0010\u001e\u001a\u00020\u001d2\u0006\u0010\u001c\u001a\u00020\u001bH\u0016J\u0010\u0010 \u001a\u00020\u001d2\u0006\u0010\u001f\u001a\u00020\u001bH\u0016R\"\u0010(\u001a\u00020!8\u0006@\u0006X\u0087.¢\u0006\u0012\n\u0004\b\"\u0010#\u001a\u0004\b$\u0010%\"\u0004\b&\u0010'R\"\u0010\u000e\u001a\u00020)8\u0006@\u0006X\u0086.¢\u0006\u0012\n\u0004\b*\u0010+\u001a\u0004\b,\u0010-\"\u0004\b.\u0010/R\u0018\u00103\u001a\u0004\u0018\u0001008\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b1\u00102R\u0014\u00107\u001a\u0002048\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b5\u00106R\u0014\u0010:\u001a\u00020\u001d8BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b8\u00109R\u0014\u0010=\u001a\u00020\u00038\u0016X\u0096\u0005¢\u0006\u0006\u001a\u0004\b;\u0010<¨\u0006@"}, d2 = {"Lca/canadapost/module/addresscomplete/AddressCompleteFragment;", "Lu8/b;", "", "Lab/c;", "Landroidx/appcompat/widget/SearchView$l;", "Landroid/view/LayoutInflater;", "inflater", "Landroid/view/ViewGroup;", "container", "Landroid/os/Bundle;", "savedInstanceState", "Landroidx/lifecycle/u;", "viewLifecycleOwner", "Landroidx/lifecycle/z0;", "viewModel", "Landroid/view/View;", "T", "Lrm/n0;", "onCreate", "onCreateView", "view", "onViewCreated", "onDestroyView", "Landroid/view/Menu;", "menu", "Landroid/view/MenuInflater;", "onCreateOptionsMenu", "", SearchIntents.EXTRA_QUERY, "", "onQueryTextSubmit", "newText", "onQueryTextChange", "Lw8/r;", "t", "Lw8/r;", "Q", "()Lw8/r;", "setShareDataService", "(Lw8/r;)V", "shareDataService", "Lca/canadapost/module/addresscomplete/b;", "u", "Lca/canadapost/module/addresscomplete/b;", "R", "()Lca/canadapost/module/addresscomplete/b;", "b0", "(Lca/canadapost/module/addresscomplete/b;)V", "Landroidx/appcompat/widget/SearchView;", "v", "Landroidx/appcompat/widget/SearchView;", "searchView", "Landroid/content/BroadcastReceiver;", "w", "Landroid/content/BroadcastReceiver;", "mMessageReceiver", "S", "()Z", "isMenuDoneLoading", "P", "()Lab/c;", "binding", "<init>", "()V", "addresscomplete_productionRelease"}, k = 1, mv = {1, 9, 0})
/* loaded from: classes4.dex */
public final class AddressCompleteFragment extends u8.b implements SearchView.l {

    /* renamed from: t, reason: collision with root package name and from kotlin metadata */
    public r shareDataService;

    /* renamed from: u, reason: collision with root package name and from kotlin metadata */
    public ca.canadapost.module.addresscomplete.b viewModel;

    /* renamed from: v, reason: collision with root package name and from kotlin metadata */
    private SearchView searchView;

    /* renamed from: s, reason: collision with root package name */
    private final /* synthetic */ x f8077s = new x(h0.f53329b);

    /* renamed from: w, reason: collision with root package name and from kotlin metadata */
    private final BroadcastReceiver mMessageReceiver = new a();

    /* loaded from: classes4.dex */
    public static final class a extends BroadcastReceiver {
        a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            SearchView searchView;
            t.g(context, "context");
            t.g(intent, "intent");
            String stringExtra = intent.getStringExtra(CoreConstants.VOICE_SEARCH_QUERY);
            if (!AddressCompleteFragment.this.S() || (searchView = AddressCompleteFragment.this.searchView) == null) {
                return;
            }
            searchView.d0(stringExtra, true);
        }
    }

    /* loaded from: classes4.dex */
    public static final class b implements MenuItem.OnActionExpandListener {
        b() {
        }

        @Override // android.view.MenuItem.OnActionExpandListener
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            t.g(menuItem, "menuItem");
            if (AddressCompleteFragment.this.getActivity() == null) {
                return true;
            }
            AddressCompleteFragment.this.requireActivity().onBackPressed();
            return true;
        }

        @Override // android.view.MenuItem.OnActionExpandListener
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            t.g(menuItem, "menuItem");
            return true;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean S() {
        return this.searchView != null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void U(AddressCompleteFragment this$0, View view) {
        t.g(this$0, "this$0");
        w.d(d.a(this$0), g0.f53320a, c.a(c0.a("CREATE_ACCOUNT_EDIT_ADDRESS", Boolean.TRUE)), null, 4, null);
        this$0.C();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void V(AddressCompleteFragment this$0, Boolean bool) {
        t.g(this$0, "this$0");
        t.d(bool);
        if (bool.booleanValue()) {
            this$0.P().A.B.setText(this$0.getString(j0.f53345k));
        } else {
            this$0.P().A.B.setText(this$0.getString(j0.f53346l));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void W(AddressCompleteFragment this$0, Boolean bool) {
        t.g(this$0, "this$0");
        SwipeRefreshLayout swipeRefreshLayout = this$0.P().F;
        t.d(bool);
        swipeRefreshLayout.setRefreshing(bool.booleanValue());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void X(AddressCompleteFragment this$0, o adapter, List list) {
        t.g(this$0, "this$0");
        t.g(adapter, "$adapter");
        t.d(list);
        if (!list.isEmpty()) {
            this$0.R().X();
        }
        adapter.N(list);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void Y(o adapter, AddressCompleteFragment this$0, RecyclerView recyclerView, int i10, View view) {
        t.g(adapter, "$adapter");
        t.g(this$0, "this$0");
        Object K = adapter.K(i10);
        if (K != null) {
            AddressCompleteFindItem addressCompleteFindItem = (AddressCompleteFindItem) K;
            this$0.R().f8089e.q(addressCompleteFindItem);
            if (this$0.S()) {
                SearchView searchView = this$0.searchView;
                if (searchView != null) {
                    searchView.setOnQueryTextListener(null);
                }
                SearchView searchView2 = this$0.searchView;
                if (searchView2 != null) {
                    searchView2.d0(addressCompleteFindItem.Text, false);
                }
                SearchView searchView3 = this$0.searchView;
                if (searchView3 != null) {
                    searchView3.setOnQueryTextListener(this$0);
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean Z(AddressCompleteFragment this$0, View view, MotionEvent motionEvent) {
        t.g(this$0, "this$0");
        if (motionEvent.getAction() != 2) {
            return false;
        }
        this$0.C();
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void a0(AddressCompleteFragment this$0, Event event) {
        t.g(this$0, "this$0");
        Boolean bool = (Boolean) event.getContentIfNotHandled();
        if (bool == null || !bool.booleanValue()) {
            return;
        }
        this$0.requireActivity().onBackPressed();
    }

    public ab.c P() {
        return (ab.c) this.f8077s.b();
    }

    public final r Q() {
        r rVar = this.shareDataService;
        if (rVar != null) {
            return rVar;
        }
        t.y("shareDataService");
        return null;
    }

    public final ca.canadapost.module.addresscomplete.b R() {
        ca.canadapost.module.addresscomplete.b bVar = this.viewModel;
        if (bVar != null) {
            return bVar;
        }
        t.y("viewModel");
        return null;
    }

    public View T(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState, u viewLifecycleOwner, z0 viewModel) {
        t.g(inflater, "inflater");
        t.g(viewLifecycleOwner, "viewLifecycleOwner");
        t.g(viewModel, "viewModel");
        return this.f8077s.c(inflater, container, savedInstanceState, viewLifecycleOwner, viewModel);
    }

    public final void b0(ca.canadapost.module.addresscomplete.b bVar) {
        t.g(bVar, "<set-?>");
        this.viewModel = bVar;
    }

    @Override // u8.b, androidx.fragment.app.f
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setHasOptionsMenu(true);
        k requireActivity = requireActivity();
        t.f(requireActivity, "requireActivity(...)");
        b0((ca.canadapost.module.addresscomplete.b) new c1(requireActivity, y()).a(ca.canadapost.module.addresscomplete.b.class));
    }

    @Override // androidx.fragment.app.f
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        t.g(menu, "menu");
        t.g(inflater, "inflater");
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(i0.f53334a, menu);
        MenuItem findItem = menu.findItem(g0.f53323d);
        findItem.expandActionView();
        findItem.setOnActionExpandListener(new b());
        View actionView = findItem.getActionView();
        t.e(actionView, "null cannot be cast to non-null type androidx.appcompat.widget.SearchView");
        SearchView searchView = (SearchView) actionView;
        this.searchView = searchView;
        ImageView imageView = searchView != null ? (ImageView) searchView.findViewById(f.f20240x) : null;
        if (imageView != null) {
            imageView.setImageResource(f0.f53317b);
        }
        SearchView searchView2 = this.searchView;
        if (searchView2 != null) {
            searchView2.setMaxWidth(Api.BaseClientBuilder.API_PRIORITY_OTHER);
            Object systemService = requireActivity().getSystemService(FirebaseAnalytics.Event.SEARCH);
            t.e(systemService, "null cannot be cast to non-null type android.app.SearchManager");
            searchView2.setSearchableInfo(((SearchManager) systemService).getSearchableInfo(requireActivity().getComponentName()));
            searchView2.setOnQueryTextListener(this);
            searchView2.setQueryHint(requireActivity().getString(j0.f53335a));
            searchView2.requestFocus();
            searchView2.setIconified(false);
        }
        w8.b.f40952a.a(this.searchView);
    }

    @Override // androidx.fragment.app.f
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        t.g(inflater, "inflater");
        u viewLifecycleOwner = getViewLifecycleOwner();
        t.f(viewLifecycleOwner, "getViewLifecycleOwner(...)");
        View T = T(inflater, container, savedInstanceState, viewLifecycleOwner, R());
        k activity = getActivity();
        t.e(activity, "null cannot be cast to non-null type androidx.appcompat.app.AppCompatActivity");
        ((i.c) activity).N(P().B.B);
        return T;
    }

    @Override // androidx.fragment.app.f
    public void onDestroyView() {
        C();
        k activity = getActivity();
        if (activity != null) {
            u4.a.b(activity).e(this.mMessageReceiver);
        }
        super.onDestroyView();
    }

    @Override // androidx.appcompat.widget.SearchView.l
    public boolean onQueryTextChange(String newText) {
        t.g(newText, "newText");
        R().f8087c.q(newText);
        return true;
    }

    @Override // androidx.appcompat.widget.SearchView.l
    public boolean onQueryTextSubmit(String query) {
        t.g(query, "query");
        R().f8087c.q(query);
        return true;
    }

    @Override // u8.b, androidx.fragment.app.f
    public void onViewCreated(View view, Bundle bundle) {
        t.g(view, "view");
        super.onViewCreated(view, bundle);
        Bundle bundle2 = (Bundle) Q().e("address_activity_bundle");
        if (bundle2 != null) {
            if (bundle2.containsKey("responseTag")) {
                R().f8106v = bundle2.getString("responseTag");
            }
            if (bundle2.containsKey("searchCountry")) {
                R().g0(bundle2.getString("searchCountry"));
            }
            if (bundle2.containsKey("CREATE_ACCOUNT_EDIT_ADDRESS")) {
                R().C.q(Boolean.valueOf(bundle2.getBoolean("CREATE_ACCOUNT_EDIT_ADDRESS")));
                R().e0();
            }
        }
        R().B.j(getViewLifecycleOwner(), new androidx.lifecycle.g0() { // from class: za.o
            @Override // androidx.lifecycle.g0
            public final void d(Object obj) {
                AddressCompleteFragment.V(AddressCompleteFragment.this, (Boolean) obj);
            }
        });
        P().F.setEnabled(false);
        R().f8090f.j(getViewLifecycleOwner(), new androidx.lifecycle.g0() { // from class: za.p
            @Override // androidx.lifecycle.g0
            public final void d(Object obj) {
                AddressCompleteFragment.W(AddressCompleteFragment.this, (Boolean) obj);
            }
        });
        final o oVar = new o(R().f8087c, new p(), getContext());
        P().E.setAdapter(oVar);
        Context context = P().E.getContext();
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) P().E.getLayoutManager();
        t.d(linearLayoutManager);
        g gVar = new g(context, linearLayoutManager.t2());
        gVar.m(requireContext().getResources().getDrawable(f0.f53318c, requireContext().getTheme()));
        P().E.h(gVar);
        R().f8088d.j(getViewLifecycleOwner(), new androidx.lifecycle.g0() { // from class: za.q
            @Override // androidx.lifecycle.g0
            public final void d(Object obj) {
                AddressCompleteFragment.X(AddressCompleteFragment.this, oVar, (List) obj);
            }
        });
        w8.u.e(P().E).f(new u.d() { // from class: za.r
            @Override // w8.u.d
            public final void a(RecyclerView recyclerView, int i10, View view2) {
                AddressCompleteFragment.Y(s8.o.this, this, recyclerView, i10, view2);
            }
        });
        P().E.setOnTouchListener(new View.OnTouchListener() { // from class: za.s
            @Override // android.view.View.OnTouchListener
            public final boolean onTouch(View view2, MotionEvent motionEvent) {
                boolean Z;
                Z = AddressCompleteFragment.Z(AddressCompleteFragment.this, view2, motionEvent);
                return Z;
            }
        });
        k activity = getActivity();
        t.d(activity);
        u4.a.b(activity).c(this.mMessageReceiver, new IntentFilter(CoreConstants.VOICE_SEARCH));
        R().f8092h.j(getViewLifecycleOwner(), new androidx.lifecycle.g0() { // from class: za.t
            @Override // androidx.lifecycle.g0
            public final void d(Object obj) {
                AddressCompleteFragment.a0(AddressCompleteFragment.this, (Event) obj);
            }
        });
        P().A.A.setOnClickListener(new View.OnClickListener() { // from class: za.u
            @Override // android.view.View.OnClickListener
            public final void onClick(View view2) {
                AddressCompleteFragment.U(AddressCompleteFragment.this, view2);
            }
        });
        CpcToolbar toolbar = P().B.B;
        t.f(toolbar, "toolbar");
        d0.k(toolbar);
    }
}
