package com.ss.android.ugc.aweme.discover.helper;

import X.0Bt;
import X.0CH;
import X.0CM;
import X.1hK;
import X.2ce;
import X.2kf;
import X.3BB;
import X.7eC;
import X.7eD;
import X.7eE;
import X.7eI;
import X.9Y3;
import X.C5239ONu;
import X.Cwu;
import X.M12;
import X.OH7;
import X.OS3;
import X.OS4;
import X.OS5;
import X.OSP;
import X.SXC;
import android.content.Context;
import android.os.CountDownTimer;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.base.model.UrlModel;
import com.ss.android.ugc.aweme.component.music.MusicService;
import com.ss.android.ugc.aweme.music.model.Music;
import com.ss.android.ugc.aweme.shortvideo.model.MusicModel;
import java.util.List;
import kotlin.h.b.n;

public final class MusicPlayHelper extends 0Bt {
    public static final OS5 LJ = new OS5((byte) 0);
    public final 2ce<Cwu<Integer, Long>> LIZ = new 2ce<>();
    public Music LIZIZ;
    public CountDownTimer LIZJ;
    public final 2ce<OH7> LIZLLL = new 2ce<>();
    public final OSP LJFF = new OSP("MusicPlayHelper");
    public C5239ONu LJI;
    public String LJII = "";

    static {
        Covode.recordClassIndex(64591);
    }

    public final void onCleared() {
        CountDownTimer countDownTimer = this.LIZJ;
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        LIZ();
        this.LJFF.LJ();
        C5239ONu oNu = this.LJI;
        if (oNu != null) {
            oNu.LIZ();
        }
    }

    public final void LIZ() {
        CountDownTimer countDownTimer = this.LIZJ;
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        this.LJFF.LIZLLL();
        this.LIZIZ = null;
        this.LIZ.setValue(new Cwu(0, -1L));
    }

    public final boolean LIZIZ(long j) {
        Cwu cwu;
        Cwu cwu2;
        if (this.LIZ.getValue() == null || (cwu = (Cwu) this.LIZ.getValue()) == null || ((Number) cwu.getFirst()).intValue() != 1 || (cwu2 = (Cwu) this.LIZ.getValue()) == null || ((Number) cwu2.getSecond()).longValue() != j) {
            return false;
        }
        return true;
    }

    public final void LIZ(0CM<Cwu<Integer, Long>> r2) {
        M12.LIZ(r2);
        this.LIZ.removeObserver(r2);
    }

    public final boolean LIZ(long j) {
        Cwu cwu;
        Cwu cwu2;
        if (this.LIZ.getValue() == null || (cwu = (Cwu) this.LIZ.getValue()) == null || ((Number) cwu.getFirst()).intValue() != 2 || (cwu2 = (Cwu) this.LIZ.getValue()) == null || ((Number) cwu2.getSecond()).longValue() != j) {
            return false;
        }
        return true;
    }

    public final void LIZ(0CH r3, 0CM<Cwu<Integer, Long>> r4) {
        M12.LIZ(r3, r4);
        this.LIZ.LIZ(r3, r4, false);
    }

    private final void LIZ(Context context, Music music) {
        String offlineDesc = music.getOfflineDesc();
        if (TextUtils.isEmpty(offlineDesc)) {
            offlineDesc = context.getString(2131827384);
        }
        3BB r0 = new 3BB(context);
        r0.LIZ(offlineDesc);
        r0.LIZIZ();
    }

    public final void LIZ(1hK r17, Music music, String str, int i, boolean z, boolean z2, String str2) {
        List urlList;
        Music music2 = music;
        String str3 = str2;
        1hK r9 = r17;
        M12.LIZ(r9, music2, str, str3);
        this.LJFF.LIZLLL();
        this.LIZIZ = music2;
        this.LJII = str3;
        MusicModel convertToMusicModel = music2.convertToMusicModel();
        1hK r5 = r9;
        if (MusicService.LJIJ().LIZ(convertToMusicModel, (Context) r5, true)) {
            7eI r2 = new 7eI();
            n.LIZIZ(convertToMusicModel, "");
            r2.LJFF = convertToMusicModel.getMusicId();
            if (convertToMusicModel.getMusicType() == MusicModel.MusicType.ONLINE) {
                r2.LIZJ = 4;
            }
            if (9Y3.LIZ(9Y3.LIZ(), true, "remove_15s_cap_music", true)) {
                Integer auditionDuration = convertToMusicModel.getAuditionDuration();
                n.LIZIZ(auditionDuration, "");
                r2.LIZLLL = auditionDuration.intValue();
            } else {
                r2.LIZLLL = convertToMusicModel.getDuration();
            }
            this.LIZ.setValue(new Cwu(1, Long.valueOf(music2.getId())));
            this.LJFF.LIZ((7eE) new SXC(this, r9, music2, z2, convertToMusicModel, i, z, str3));
            this.LJFF.LIZ((7eC) new OS3(this));
            this.LJFF.LIZ((7eD) new OS4(this));
            UrlModel playUrl = music2.getPlayUrl();
            if (playUrl == null || (urlList = playUrl.getUrlList()) == null) {
                LIZ((Context) r5, music2);
            } else if (urlList.size() > 0) {
                r2.LIZIZ = urlList;
                this.LJFF.LIZ(r2);
            } else {
                LIZ((Context) r5, music2);
                2kf.LIZIZ(3, (String) null, "Music Url List size is zero, and music id:  " + music2.getId());
            }
            r9.getLifecycle().LIZ(new MusicPlayHelper$play$6(this));
        }
    }
}
