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

import X.0Bt;
import X.0HH;
import X.1Bn;
import X.2nK;
import X.2yO;
import X.7WW;
import X.7he;
import X.878;
import X.AVG;
import X.AhQ;
import X.Bc3;
import X.BcJ;
import X.BcK;
import X.C0273MOg;
import X.C0274MOh;
import X.C5599Oag;
import X.C5600Oah;
import X.C5601Oai;
import X.C5602Oaj;
import X.C5603Oak;
import X.C5604Oal;
import X.C5605Oam;
import X.C5606Oan;
import X.C5607Oao;
import X.C5608Oap;
import X.C5627Ob8;
import X.C5628Ob9;
import X.Crj;
import X.DaP;
import X.DaU;
import X.LbH;
import X.LeC;
import X.LeS;
import X.Lea;
import X.Luu;
import X.M12;
import X.MP8;
import X.OSU;
import X.OSX;
import X.OZY;
import X.OZZ;
import X.PgV;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.choosemusic.api.SearchMusicApi;
import com.ss.android.ugc.aweme.commercialize.media.impl.service.CommerceMediaServiceImpl;
import com.ss.android.ugc.aweme.discover.model.DynamicMusicModel;
import com.ss.android.ugc.aweme.discover.model.GlobalDoodleConfig;
import com.ss.android.ugc.aweme.feed.model.Aweme;
import com.ss.android.ugc.aweme.feed.model.Extra;
import com.ss.android.ugc.aweme.feed.model.LogPbBean;
import com.ss.android.ugc.aweme.music.model.AwemeSearchMusicList;
import com.ss.android.ugc.aweme.music.model.Music;
import com.ss.android.ugc.aweme.music.model.SearchMusic;
import com.ss.android.ugc.aweme.search.SearchServiceImpl;
import com.ss.android.ugc.aweme.shortvideo.model.MusicModel;
import com.ss.android.ugc.aweme.utils.GsonHolder;
import com.ss.android.ugc.aweme.utils.GsonProvider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import kotlin.h.b.n;
import org.json.JSONObject;

public final class SearchMusicResultViewModel extends 0Bt {
    public static final MP8 LJIIIZ = new MP8((byte) 0);
    public String LIZ;
    public OSX LIZIZ = new OSX("search_music", "", "", OSU.LIZ);
    public final HashSet<String> LIZJ = new HashSet<>();
    public BcJ LIZLLL;
    public C5628Ob9 LJ;
    public 1Bn<Integer> LJFF = new 1Bn<>();
    public C5602Oaj LJI = new C5602Oaj();
    public GlobalDoodleConfig LJII;
    public long LJIIIIZZ;
    public String LJIIJ;
    public final Crj LJIIJJI = 7he.LIZ(C5599Oag.LIZ);
    public final Crj LJIIL = 7he.LIZ(C5603Oak.LIZ);
    public final Crj LJIILIIL = 7he.LIZ(C5604Oal.LIZ);
    public final Crj LJIILJJIL = 7he.LIZ(new C5627Ob8(this));

    static {
        Covode.recordClassIndex(56269);
    }

    private C5601Oai LIZJ() {
        return (C5601Oai) this.LJIIL.getValue();
    }

    private 7WW LIZLLL() {
        return (7WW) this.LJIILIIL.getValue();
    }

    public final DaU<AwemeSearchMusicList> LIZ() {
        return (DaU) this.LJIILJJIL.getValue();
    }

    private final String LJ() {
        if (CommerceMediaServiceImpl.LJI().LJ()) {
            return "ad_music";
        }
        return "video_music";
    }

    public final void LIZIZ() {
        LIZ().LIZLLL.LIZLLL();
    }

    private final void LIZ(MusicModel musicModel) {
        if (musicModel.getDynamicPatch() != null) {
            LeC dynamicPatch = musicModel.getDynamicPatch();
            n.LIZIZ(dynamicPatch, "");
            if (dynamicPatch.getAwemeListRaw() != null) {
                ArrayList arrayList = new ArrayList();
                LeC dynamicPatch2 = musicModel.getDynamicPatch();
                n.LIZIZ(dynamicPatch2, "");
                for (Object next : dynamicPatch2.getAwemeListRaw()) {
                    GsonProvider LIZJ2 = GsonHolder.LIZJ();
                    n.LIZIZ(LIZJ2, "");
                    String LIZIZ2 = LIZJ2.LIZIZ().LIZIZ(next);
                    GsonProvider LIZJ3 = GsonHolder.LIZJ();
                    n.LIZIZ(LIZJ3, "");
                    Object LIZ2 = LIZJ3.LIZIZ().LIZ(LIZIZ2, new C5605Oam().type);
                    n.LIZIZ(LIZ2, "");
                    arrayList.add(LIZ2);
                }
                LeC dynamicPatch3 = musicModel.getDynamicPatch();
                n.LIZIZ(dynamicPatch3, "");
                dynamicPatch3.setAwemeList(arrayList);
            }
        }
    }

    private ArrayList<MusicModel> LIZ(AwemeSearchMusicList awemeSearchMusicList) {
        List<SearchMusic> list;
        String str;
        Aweme aweme;
        ArrayList<MusicModel> arrayList = new ArrayList<>();
        if (!(awemeSearchMusicList == null || (list = awemeSearchMusicList.searchMusicList) == null)) {
            for (SearchMusic next : list) {
                MusicModel convertToMusicModel = next.convertToMusicModel();
                if (convertToMusicModel != null) {
                    convertToMusicModel.setLogPb(awemeSearchMusicList.logPb);
                    Integer valueOf = Integer.valueOf(next.getCardType());
                    int ordinal = MusicModel.CardType.AWESearchMusicCardOrdinary.ordinal();
                    if (valueOf != null && valueOf.intValue() == ordinal) {
                        convertToMusicModel.setCardType(MusicModel.CardType.AWESearchMusicCardOrdinary);
                    }
                    int ordinal2 = MusicModel.CardType.AWESearchMusicCardDuplicated.ordinal();
                    if (valueOf != null && valueOf.intValue() == ordinal2) {
                        convertToMusicModel.setCardType(MusicModel.CardType.AWESearchMusicCardDuplicated);
                    }
                    int ordinal3 = MusicModel.CardType.AWESearchMusicCardSinger.ordinal();
                    if (valueOf != null && valueOf.intValue() == ordinal3) {
                        convertToMusicModel.setCardType(MusicModel.CardType.AWESearchMusicCardSinger);
                    }
                    int ordinal4 = MusicModel.CardType.AWESearchMusicCardMusicWithVideo.ordinal();
                    if (valueOf != null && valueOf.intValue() == ordinal4) {
                        convertToMusicModel.setCardType(MusicModel.CardType.AWESearchMusicCardMusicWithVideo);
                    }
                    List<Aweme> awemeList = next.getAwemeList();
                    if (awemeList == null || (aweme = (Aweme) Lea.LJIIIIZZ(awemeList)) == null || (str = aweme.getAid()) == null) {
                        str = "0";
                    }
                    convertToMusicModel.setAwemeId(str);
                    arrayList.add(convertToMusicModel);
                }
                if ((convertToMusicModel instanceof DynamicMusicModel) && convertToMusicModel.getDynamicPatch() != null) {
                    LeC dynamicPatch = convertToMusicModel.getDynamicPatch();
                    n.LIZIZ(dynamicPatch, "");
                    if (dynamicPatch.getOriginType() == 1) {
                        LIZ(convertToMusicModel);
                    }
                }
            }
        }
        return arrayList;
    }

    public final PgV<AwemeSearchMusicList> LIZ(int i) {
        int i2;
        int i3;
        int i4;
        int i5;
        String str;
        String str2;
        long j;
        AVG avg = this.LJI.LJI;
        int i6 = 0;
        if (avg != null) {
            i2 = avg.getFilterBy();
        } else {
            i2 = 0;
        }
        AVG avg2 = this.LJI.LJI;
        if (avg2 != null) {
            i3 = avg2.getSortType();
        } else {
            i3 = 0;
        }
        if (!this.LJI.LJFF || i != 0) {
            i4 = 0;
        } else {
            i4 = 1;
        }
        if (this.LJI.LJIIIIZZ > 0) {
            i5 = 1;
        } else {
            i5 = 0;
        }
        if (!(i2 == 0 && i3 == 0)) {
            i6 = 1;
        }
        if (this.LJI.LJIIIZ == 0) {
            str = "tiktok_music_create";
        } else {
            str = "tiktok_music_prolong";
        }
        String str3 = "";
        if (i == 0) {
            C5608Oap LJIILLIIL = SearchServiceImpl.LJJIIZ().LJIILLIIL();
            if (LJIILLIIL != null) {
                str2 = LJIILLIIL.LIZ();
            } else {
                str2 = null;
            }
        } else {
            str2 = str3;
            str3 = this.LJIIJ;
        }
        SearchMusicApi searchMusicApi = OZY.LIZ;
        Integer valueOf = Integer.valueOf(i);
        String str4 = this.LJI.LIZJ;
        String LJ2 = LJ();
        String LJ3 = LJ();
        Integer valueOf2 = Integer.valueOf(i4);
        Integer valueOf3 = Integer.valueOf(i2);
        Integer valueOf4 = Integer.valueOf(i3);
        Integer valueOf5 = Integer.valueOf(i6);
        C5602Oaj oaj = this.LJI;
        if (oaj.LJIIIIZZ > 0) {
            j = oaj.LJIIIIZZ;
        } else {
            j = oaj.LJII;
        }
        return searchMusicApi.getSearchResultList(valueOf, 20, str4, LJ2, str, LJ3, valueOf2, valueOf3, valueOf4, valueOf5, Long.valueOf(j), Integer.valueOf(i5), str2, str3, BcK.LIZ.LIZ());
    }

    private List<MusicModel> LIZIZ(AwemeSearchMusicList awemeSearchMusicList, boolean z) {
        GlobalDoodleConfig globalDoodleConfig;
        boolean z2;
        String str;
        String str2;
        String str3;
        long j;
        String newSource;
        boolean z3;
        String str4;
        boolean z4;
        String str5;
        M12.LIZ(awemeSearchMusicList);
        String str6 = "";
        if (!z) {
            LogPbBean logPbBean = awemeSearchMusicList.logPb;
            if (logPbBean == null || (str5 = logPbBean.getImprId()) == null) {
                str5 = str6;
            }
            this.LJIIJ = str5;
        }
        if (awemeSearchMusicList != null) {
            globalDoodleConfig = awemeSearchMusicList.globalDoodleConfig;
        } else {
            globalDoodleConfig = null;
        }
        if (globalDoodleConfig != null && !TextUtils.equals(awemeSearchMusicList.globalDoodleConfig.getRequestKeyword(), this.LIZ)) {
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("enter_from", "choose_music");
                jSONObject.put("search_type", "music");
                int i = this.LJI.LIZIZ;
                if (i == 1) {
                    str4 = "normal_search";
                } else if (i == 2) {
                    str4 = "search_history";
                } else if (i != 3) {
                    str4 = str6;
                } else {
                    str4 = "search_sug";
                }
                jSONObject.put("search_from", str4);
                if (!z) {
                    z4 = true;
                } else {
                    z4 = false;
                }
                jSONObject.put("is_refresh", z4);
                jSONObject.put("is_server_keyword_null", TextUtils.isEmpty(awemeSearchMusicList.globalDoodleConfig.getRequestKeyword()));
                2nK.LIZ("search_keyword_conflict", jSONObject);
            } catch (Exception e) {
                0HH.LIZ(e);
            }
        }
        this.LJII = awemeSearchMusicList.globalDoodleConfig;
        ArrayList<MusicModel> LIZ2 = LIZ(awemeSearchMusicList);
        if (!878.LIZ(LIZ2)) {
            C5601Oai LIZJ2 = LIZJ();
            if (!z && LIZJ2 != null) {
                LIZJ2.LIZIZ();
            }
            GlobalDoodleConfig globalDoodleConfig2 = awemeSearchMusicList.globalDoodleConfig;
            if (globalDoodleConfig2 == null || globalDoodleConfig2.getSoundsListType() != 2) {
                z2 = false;
            } else {
                z2 = true;
            }
            if (z2 && !z) {
                C5606Oan oan = new C5606Oan();
                oan.setLogPb(awemeSearchMusicList.logPb);
                LIZ2.add(0, oan);
                LIZLLL().LIZ();
            }
            int size = LIZ2.size();
            int i2 = 0;
            for (T next : LIZ2) {
                int i3 = i2 + 1;
                if (i2 < 0) {
                    LeS.LIZIZ();
                }
                MusicModel musicModel = (MusicModel) next;
                musicModel.setSearchKeyWords(this.LIZ);
                musicModel.setSearchId(this.LJIIJ);
                C5601Oai LIZJ3 = LIZJ();
                if (LIZJ3 != null) {
                    if (awemeSearchMusicList.isHasMore() || i2 != size - 1) {
                        z3 = false;
                    } else {
                        z3 = true;
                    }
                    LIZJ3.LIZ(z3, C0274MOh.LIZ, new C0273MOg(i2));
                }
                GlobalDoodleConfig globalDoodleConfig3 = awemeSearchMusicList.globalDoodleConfig;
                Music music = musicModel.getMusic();
                7WW LIZLLL2 = LIZLLL();
                if (!(music == null || globalDoodleConfig3 == null)) {
                    musicModel.setSoundsListType(globalDoodleConfig3.getSoundsListType());
                    music.setSoundsListType(globalDoodleConfig3.getSoundsListType());
                    if (LIZLLL2 != null) {
                        LIZLLL2.LIZ(new OZZ(music, musicModel));
                    }
                }
                i2 = i3;
            }
            C5601Oai LIZJ4 = LIZJ();
            if (LIZJ4 != null) {
                LIZJ4.LIZ(new C5600Oah(size, awemeSearchMusicList, LIZ2));
            }
            if (!z) {
                GlobalDoodleConfig globalDoodleConfig4 = awemeSearchMusicList.globalDoodleConfig;
                if (globalDoodleConfig4 == null || (str = globalDoodleConfig4.getRequestKeyword()) == null) {
                    str = str6;
                }
                LogPbBean logPbBean2 = awemeSearchMusicList.logPb;
                if (logPbBean2 == null || (str2 = logPbBean2.getImprId()) == null) {
                    str2 = str6;
                }
                GlobalDoodleConfig globalDoodleConfig5 = awemeSearchMusicList.globalDoodleConfig;
                if (globalDoodleConfig5 == null || (str3 = globalDoodleConfig5.getSearchChannel()) == null) {
                    str3 = str6;
                }
                GlobalDoodleConfig globalDoodleConfig6 = awemeSearchMusicList.globalDoodleConfig;
                if (!(globalDoodleConfig6 == null || (newSource = globalDoodleConfig6.getNewSource()) == null)) {
                    str6 = newSource;
                }
                Extra extra = awemeSearchMusicList.extra;
                if (extra != null) {
                    j = extra.now;
                } else {
                    j = 0;
                }
                LbH lbH = new LbH(str, str2, str3, str6, Long.valueOf(j));
                C5608Oap LJIILLIIL = SearchServiceImpl.LJJIIZ().LJIILLIIL();
                if (LJIILLIIL != null) {
                    LJIILLIIL.LIZ(lbH);
                }
            }
        }
        return LIZ2;
    }

    public final List<DaP> LIZ(AwemeSearchMusicList awemeSearchMusicList, boolean z) {
        OSX osx;
        int i;
        M12.LIZ(awemeSearchMusicList);
        ArrayList arrayList = new ArrayList();
        int i2 = 0;
        for (T next : LIZIZ(awemeSearchMusicList, z)) {
            int i3 = i2 + 1;
            if (i2 < 0) {
                LeS.LIZIZ();
            }
            MusicModel musicModel = (MusicModel) next;
            if (musicModel instanceof C5607Oao) {
                arrayList.add(new AhQ(musicModel));
            } else if (musicModel instanceof C5606Oan) {
                arrayList.add(new 2yO(2131820988));
            } else if (musicModel instanceof DynamicMusicModel) {
                LeC dynamicPatch = musicModel.getDynamicPatch();
                n.LIZIZ(dynamicPatch, "");
                Integer valueOf = Integer.valueOf(i2);
                HashMap hashMap = new HashMap();
                if (musicModel != null) {
                    if (musicModel.getDynamicPatch() != null) {
                        LeC dynamicPatch2 = musicModel.getDynamicPatch();
                        n.LIZIZ(dynamicPatch2, "");
                        List awemeListRaw = dynamicPatch2.getAwemeListRaw();
                        n.LIZIZ(awemeListRaw, "");
                        hashMap.put("aweme_list", awemeListRaw);
                    }
                    String str = OSU.LIZLLL;
                    n.LIZIZ(str, "");
                    hashMap.put("creationId", str);
                    hashMap.put("max_shoot_time", Long.valueOf(this.LJIIIIZZ));
                    String searchKeyWords = musicModel.getSearchKeyWords();
                    n.LIZIZ(searchKeyWords, "");
                    hashMap.put("keyword", searchKeyWords);
                    if (musicModel.getLogPb() != null) {
                        LogPbBean logPb = musicModel.getLogPb();
                        n.LIZIZ(logPb, "");
                        String imprId = logPb.getImprId();
                        n.LIZIZ(imprId, "");
                        hashMap.put("imprId", imprId);
                    }
                    String searchId = musicModel.getSearchId();
                    n.LIZIZ(searchId, "");
                    hashMap.put("searchId", searchId);
                    if (valueOf != null) {
                        i = valueOf.intValue();
                    } else {
                        i = 0;
                    }
                    hashMap.put("rank", Integer.valueOf(i));
                }
                arrayList.add(new Luu(dynamicPatch, hashMap, true, (byte) 0));
            } else if (musicModel.getMusic() != null) {
                Music music = musicModel.getMusic();
                n.LIZIZ(music, "");
                String mid = music.getMid();
                OSX osx2 = this.LIZIZ;
                if (osx2 == null) {
                    osx = null;
                } else {
                    osx = new OSX(osx2.LIZ, osx2.LIZIZ, osx2.LIZJ, osx2.LIZLLL);
                    osx.LIZ(osx2.LJ);
                    osx.LJI = osx2.LJI;
                    osx.LJII = osx2.LJII;
                    osx.LJFF = osx2.LJFF;
                    osx.LJIIIZ = osx2.LJIIIZ;
                    osx.LJIIJJI = osx2.LJIIJJI;
                    osx.LJIIL = osx2.LJIIL;
                }
                n.LIZIZ(osx, "");
                osx.LJIIIIZZ = musicModel.getLogPb();
                if (this.LIZJ.add(mid)) {
                    arrayList.add(new Bc3(this.LIZ, osx, musicModel, this.LIZLLL));
                }
            }
            i2 = i3;
        }
        return arrayList;
    }
}
