package com.ss.android.ugc.aweme.kids.choosemusic.fragment;

import X.0CM;
import X.0E3;
import X.0HH;
import X.1hK;
import X.2GO;
import X.2HZ;
import X.33g;
import X.3BB;
import X.86a;
import X.878;
import X.C5339ORq;
import X.C5344ORv;
import X.C5418OUr;
import X.C5421OUu;
import X.C5431OVe;
import X.C5433OVg;
import X.C5522OYr;
import X.Cmx;
import X.ISm;
import X.ONE;
import X.OSE;
import X.OSM;
import X.OSY;
import X.OV7;
import X.OVA;
import X.OVG;
import X.OVU;
import X.OWT;
import X.Rc9;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.arch.widgets.base.DataCenter;
import com.ss.android.ugc.aweme.base.fragment.AmeBaseFragment;
import com.ss.android.ugc.aweme.out.AVExternalServiceImpl;
import com.ss.android.ugc.aweme.services.AsyncAVService;
import com.ss.android.ugc.aweme.services.SimpleServiceLoadCallback;
import com.ss.android.ugc.aweme.services.external.ui.RecordConfig;
import com.ss.android.ugc.aweme.shortvideo.model.MusicModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseMusicListFragment<T> extends AmeBaseFragment implements 0CM<33g>, OV7<T>, C5522OYr<C5433OVg>, C5339ORq, OVG, 2HZ, 2GO {
    public OSE LIZ;
    public MusicModel LIZIZ;
    public boolean LIZJ = true;
    public C5418OUr LIZLLL;
    public DataCenter LJ;
    public OVA<T> LJIIIZ;
    public int LJIIJ;
    public C5421OUu LJIIJJI;

    static {
        Covode.recordClassIndex(89277);
    }

    public abstract OVA<T> LIZIZ(View view);

    public boolean LJFF() {
        return true;
    }

    public abstract int LJIIJ();

    public abstract String LJIIL();

    public final boolean LJJIJIIJI() {
        return true;
    }

    public Map<Integer, Object> getSubscriberEventTypes() {
        HashMap hashMap = new HashMap();
        hashMap.put(367, new Rc9(BaseMusicListFragment.class, "onMusicCollectEvent", C5431OVe.class, ThreadMode.POSTING, 0, true));
        return hashMap;
    }

    public final MusicModel LJI() {
        return this.LIZIZ;
    }

    public final Activity LJII() {
        return getActivity();
    }

    public final boolean LJIIIIZZ() {
        return aq_();
    }

    public 0E3 LJIIIZ() {
        OVA<T> ova = this.LJIIIZ;
        if (ova != null) {
            return ova.LIZLLL();
        }
        return null;
    }

    public void onDestroyView() {
        BaseMusicListFragment.super.onDestroyView();
        OSE ose = this.LIZ;
        if (ose != null) {
            ose.LIZ();
            this.LIZ.LIZLLL();
        }
    }

    public void onResume() {
        BaseMusicListFragment.super.onResume();
        OSE ose = this.LIZ;
        if (ose != null) {
            ose.LJIIJ = false;
        }
    }

    private boolean LJIIJJI() {
        if (getActivity().getIntent() != null) {
            return getActivity().getIntent().getBooleanExtra("extra_beat_music_sticker", false);
        }
        return false;
    }

    public void LIZ() {
        this.LIZLLL = new C5418OUr(getContext(), this.LJ);
    }

    public DataCenter LJ() {
        if (this.LJ == null) {
            this.LJ = DataCenter.LIZ(Cmx.LIZIZ(this), this);
        }
        DataCenter dataCenter = this.LJ;
        dataCenter.LIZ("music_collect_status", this);
        dataCenter.LIZ("play_compeleted", this);
        return this.LJ;
    }

    public void onPause() {
        BaseMusicListFragment.super.onPause();
        OSE ose = this.LIZ;
        if (ose != null) {
            ose.LIZ();
            this.LIZ.LJIIJ = true;
        }
        OWT LJIIIZ2 = LJIIIZ();
        if (LJIIIZ2 instanceof OWT) {
            LJIIIZ2.LIZ();
        }
    }

    public final void LIZ(C5344ORv oRv) {
        this.LIZ.LJII = oRv;
    }

    public final OVA<T> LIZ(View view) {
        if (this.LJIIIZ == null) {
            this.LJIIIZ = LIZIZ(view);
        }
        return this.LJIIIZ;
    }

    public final void LIZIZ(MusicModel musicModel) {
        this.LIZ.LJIIIIZZ = LJIIL();
        this.LIZ.LIZJ(musicModel, LJIIJ(), LJIIJJI());
    }

    public void onCreate(Bundle bundle) {
        BaseMusicListFragment.super.onCreate(bundle);
        int i = 1;
        if (getArguments() != null) {
            i = getArguments().getInt("com.ss.android.ugc.aweme.intent.extra.EXTRA_MUSIC_TYPE", 1);
        }
        this.LJIIJ = i;
    }

    public final void LIZ(MusicModel musicModel) {
        OSE ose = this.LIZ;
        if (ose != null) {
            ose.LIZ();
        }
    }

    @ISm(LIZIZ = true)
    public void onMusicCollectEvent(C5431OVe oVe) {
        if (this.LJ != null && oVe != null && "music_detail".equals(oVe.LIZJ)) {
            this.LJ.LIZ("music_collect_status", new OVU(0, oVe.LIZ, -1, -1, oVe.LIZIZ));
        }
    }

    /* renamed from: LIZ */
    public void onChanged(33g r8) {
        int i;
        int i2;
        MusicModel.CollectionType collectionType;
        if (r8 != null) {
            String str = r8.LIZ;
            str.hashCode();
            if (str.equals("music_collect_status")) {
                OVU ovu = (OVU) r8.LIZ();
                OVA<T> ova = this.LJIIIZ;
                if (!(ova == null || ova.LIZLLL() == null)) {
                    List<MusicModel> data = this.LJIIIZ.LIZLLL().getData();
                    if (!878.LIZ(data)) {
                        if (ovu.LIZ == 1) {
                            if (data.size() > ovu.LIZJ) {
                                this.LJIIIZ.LIZLLL().notifyItemChanged(ovu.LIZJ);
                            }
                        } else if (ovu.LIZJ == -1) {
                            String musicId = ovu.LJ.getMusicId();
                            if (!878.LIZ(data)) {
                                for (MusicModel musicModel : data) {
                                    if (musicModel != null && 86a.LIZ(musicModel.getMusicId(), musicId)) {
                                        if (ovu.LIZLLL == 1) {
                                            collectionType = MusicModel.CollectionType.COLLECTED;
                                        } else {
                                            collectionType = MusicModel.CollectionType.NOT_COLLECTED;
                                        }
                                        musicModel.setCollectionType(collectionType);
                                        int indexOf = data.indexOf(musicModel);
                                        if (indexOf >= 0 && indexOf < data.size() && LJFF()) {
                                            this.LJIIIZ.LIZLLL().notifyItemChanged(indexOf);
                                            return;
                                        }
                                        return;
                                    }
                                }
                                return;
                            }
                            return;
                        }
                    } else {
                        return;
                    }
                }
                if (getUserVisibleHint()) {
                    if (ovu.LIZ == 1) {
                        if (ovu.LIZLLL == 1) {
                            i2 = 2131822729;
                        } else {
                            i2 = 2131822319;
                        }
                        3BB r1 = new 3BB(getActivity());
                        r1.LIZIZ(i2);
                        r1.LIZIZ();
                        return;
                    }
                    if (ovu.LIZLLL == 1) {
                        i = 2131822734;
                    } else {
                        i = 2131822320;
                    }
                    3BB r12 = new 3BB(getActivity());
                    r12.LIZIZ(i);
                    r12.LIZIZ();
                }
            } else if (str.equals("play_compeleted") && (LJIIIZ() instanceof OWT)) {
                LJIIIZ().LIZ();
            }
        }
    }

    public final /* synthetic */ void LIZ(Object obj) {
        C5433OVg oVg = (C5433OVg) obj;
        String str = oVg.LIZIZ;
        MusicModel musicModel = oVg.LIZ;
        if ("follow_type".equals(str)) {
            this.LIZLLL.LIZ(musicModel, musicModel.getMusicId(), 1, oVg.LIZJ, oVg.LIZLLL);
        } else if ("unfollow_type".equals(str)) {
            this.LIZLLL.LIZ(musicModel, musicModel.getMusicId(), 0, oVg.LIZJ, oVg.LIZLLL);
        }
    }

    public void onViewCreated(View view, Bundle bundle) {
        BaseMusicListFragment.super.onViewCreated(view, bundle);
        if (this.LJIIJJI == null) {
            this.LJIIJJI = new C5421OUu(this);
        }
        this.LJIIJJI.LIZ(view);
        OSE ose = new OSE(this, new OSM() {
            static {
                Covode.recordClassIndex(89278);
            }

            public final void LIZ() {
                if (BaseMusicListFragment.this.LIZIZ != null) {
                    BaseMusicListFragment.this.LJ.LIZ("play_compeleted", BaseMusicListFragment.this.LIZIZ.getMusicId());
                }
            }

            public final void LIZIZ() {
                if (BaseMusicListFragment.this.LIZIZ != null) {
                    BaseMusicListFragment.this.LJ.LIZ("play_error", BaseMusicListFragment.this.LIZIZ.getMusicId());
                }
            }
        });
        this.LIZ = ose;
        ose.LIZJ();
        this.LIZ.LIZIZ(this.LJIIJ);
        OWT LJIIIZ2 = LJIIIZ();
        if (LJIIIZ2 instanceof OWT) {
            this.LIZ.LIZ = LJIIIZ2.LJ;
        }
    }

    public final void LIZ(MusicModel musicModel, OSY osy) {
        this.LIZIZ = musicModel;
        if (this.LIZJ) {
            this.LIZ.LIZ = osy;
            this.LIZ.LIZ(musicModel, LJIIJ(), false);
            return;
        }
        this.LIZ.LIZJ(musicModel, LJIIJ(), LJIIJJI());
    }

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

    public final void LIZ(String str, MusicModel musicModel, String str2) {
        final 1hK activity = getActivity();
        if (activity != null && !activity.isFinishing()) {
            ONE.LIZ(musicModel);
            AVExternalServiceImpl.LIZ().publishService().setCurMusic(musicModel, true, false);
            if (this.LJIIJ == 1) {
                final RecordConfig.Builder builder = new RecordConfig.Builder();
                builder.musicOrigin(str2);
                builder.musicModel(musicModel);
                builder.musicPath(str);
                AVExternalServiceImpl.LIZ().asyncService("BaseMuiscList", new SimpleServiceLoadCallback() {
                    static {
                        Covode.recordClassIndex(89279);
                    }

                    public final void onLoad(AsyncAVService asyncAVService, long j) {
                        asyncAVService.uiService().recordService().startRecord((Context) activity, builder.build());
                        activity.finish();
                    }
                });
                return;
            }
            Intent intent = new Intent();
            intent.putExtra("path", str);
            intent.putExtra("music_model", musicModel);
            intent.putExtra("music_origin", str2);
            activity.setResult(-1, intent);
            activity.finish();
        }
    }
}
