package com.ss.android.ugc.aweme.choosemusic.history;

import X.0EB;
import X.0HH;
import X.2yT;
import X.C2846JBl;
import X.C2847JBm;
import X.C2848JBn;
import X.C2849JBo;
import X.DZp;
import X.DaP;
import X.M12;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.base.fragment.AmeBaseFragment;
import com.ss.android.ugc.aweme.music.model.MusicSearchHistory;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kotlin.h.b.n;

public final class SearchMusicHistoryFragment extends AmeBaseFragment implements C2849JBo {
    public static final C2847JBm LIZIZ = new C2847JBm((byte) 0);
    public final ArrayList<DaP> LIZ = new ArrayList<>();
    public SparseArray LIZJ;

    static {
        Covode.recordClassIndex(56173);
    }

    private View LIZIZ() {
        if (this.LIZJ == null) {
            this.LIZJ = new SparseArray();
        }
        View view = (View) this.LIZJ.get(2131368752);
        if (view != null) {
            return view;
        }
        View view2 = getView();
        if (view2 == null) {
            return null;
        }
        View findViewById = view2.findViewById(2131368752);
        this.LIZJ.put(2131368752, findViewById);
        return findViewById;
    }

    public final void onResume() {
        SearchMusicHistoryFragment.super.onResume();
        LIZ();
    }

    private void LIZ() {
        C2846JBl LJI = C2846JBl.LJI();
        n.LIZIZ(LJI, "");
        List<MusicSearchHistory> LJ = LJI.LJ();
        n.LIZIZ(LJ, "");
        LIZ(LJ);
    }

    public final void onDestroyView() {
        SearchMusicHistoryFragment.super.onDestroyView();
        C2846JBl LJI = C2846JBl.LJI();
        if (LJI.LIZJ != null) {
            Iterator<WeakReference<C2849JBo>> it = LJI.LIZJ.iterator();
            while (it.hasNext()) {
                WeakReference next = it.next();
                if (next != null && next.get() == this) {
                    it.remove();
                }
            }
        }
        SparseArray sparseArray = this.LIZJ;
        if (sparseArray != null) {
            sparseArray.clear();
        }
    }

    public final void LIZ(List<? extends MusicSearchHistory> list) {
        int i;
        M12.LIZ(list);
        this.LIZ.clear();
        for (MusicSearchHistory jBn : list) {
            this.LIZ.add(new C2848JBn(jBn));
            if (this.LIZ.size() >= Integer.MAX_VALUE) {
                break;
            }
        }
        DZp LIZIZ2 = LIZIZ();
        if (LIZIZ2 != null) {
            if (this.LIZ.size() > 0) {
                i = 0;
            } else {
                i = 8;
            }
            LIZIZ2.setVisibility(i);
        }
        if (this.LIZ.size() >= 3) {
            this.LIZ.add(new 2yT());
        }
        DZp LIZIZ3 = LIZIZ();
        n.LIZIZ(LIZIZ3, "");
        LIZIZ3.getState().LIZ();
        DZp LIZIZ4 = LIZIZ();
        n.LIZIZ(LIZIZ4, "");
        LIZIZ4.getState().LIZ(this.LIZ);
    }

    public final void onViewCreated(View view, Bundle bundle) {
        M12.LIZ(view);
        SearchMusicHistoryFragment.super.onViewCreated(view, bundle);
        C2846JBl LJI = C2846JBl.LJI();
        if (LJI.LIZJ == null) {
            LJI.LIZJ = new ArrayList();
        }
        LJI.LIZJ.add(new WeakReference(this));
        RecyclerView LIZIZ2 = LIZIZ();
        n.LIZIZ(LIZIZ2, "");
        LIZIZ2.setNestedScrollingEnabled(true);
        RecyclerView LIZIZ3 = LIZIZ();
        n.LIZIZ(LIZIZ3, "");
        LIZIZ3.setItemAnimator((0EB) null);
        LIZIZ().LIZ(new Class[]{SearchMusicHistoryCell.class, ClearSearchHistoryCell.class});
        LIZ();
    }

    public final View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        M12.LIZ(layoutInflater);
        return 0HH.LIZ(layoutInflater, 2131561110, viewGroup, false);
    }
}
