package com.bytedance.android.livesdk.newvideogift.controllers;

import X.0CB;
import X.0CH;
import X.0CN;
import X.0HH;
import X.6Is;
import X.C1095Tlf;
import X.C1096Tlg;
import X.C1098Tli;
import X.INZ;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.ugc.aweme.live.alphaplayer.controller.IPlayerController;
import com.ss.android.ugc.aweme.live.alphaplayer.listener.AlphaPlayerAction;
import com.ss.android.ugc.aweme.live.alphaplayer.listener.IMonitor;
import com.ss.android.ugc.aweme.live.alphaplayer.listener.IProgressListener;
import com.ss.android.ugc.aweme.live.alphaplayer.model.DataSource;
import com.ss.android.ugc.aweme.live.alphaplayer.model.MaskSrc;
import com.ss.android.ugc.aweme.live.alphaplayer.player.AbsPlayer;
import com.ss.android.ugc.aweme.live.alphaplayer.player.IMediaPlayer;
import java.io.File;

public class LocalPlayerControllerNormal implements 6Is, IPlayerController {
    public C1095Tlf LIZ;
    public AlphaPlayerAction LIZIZ;
    public IMediaPlayer<AbsPlayer<AbsPlayer>> LIZJ;
    public INZ LIZLLL;
    public int LJ;
    public int LJFF;
    public int LJI;
    public long LJII;
    public boolean LJIIIIZZ;
    public Context LJIIIZ;
    public IMonitor LJIIJ;
    public boolean LJIIJJI;
    public IMediaPlayer.OnPreparedListener<AbsPlayer<AbsPlayer>> LJIIL;
    public IMediaPlayer.OnErrorListener<AbsPlayer<AbsPlayer>> LJIILIIL;

    static {
        Covode.recordClassIndex(19798);
    }

    public void cancel() {
    }

    public int getCurFrame() {
        return 0;
    }

    public void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_PAUSE) {
            onPause();
        } else if (r3 == 0CB.ON_RESUME) {
            onResume();
        } else if (r3 == 0CB.ON_STOP) {
            onStop();
        } else if (r3 == 0CB.ON_DESTROY) {
            onDestroy();
        }
    }

    public void setMask(MaskSrc maskSrc) {
    }

    public void setPrepareListener(C1098Tli tli) {
    }

    public void setProgressListener(IProgressListener iProgressListener, long j) {
    }

    public void startWithLastFrameHold(DataSource dataSource, boolean z) {
    }

    public View getView() {
        return this.LIZLLL;
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public void onDestroy() {
        release();
    }

    @0CN(LIZ = 0CB.ON_PAUSE)
    public void onPause() {
        pause();
    }

    @0CN(LIZ = 0CB.ON_RESUME)
    public void onResume() {
        resume();
    }

    @0CN(LIZ = 0CB.ON_STOP)
    public void onStop() {
        stop();
    }

    private void LIZ() {
        this.LJIIIIZZ = false;
        this.LJII = 0;
        AlphaPlayerAction alphaPlayerAction = this.LIZIZ;
        if (alphaPlayerAction != null) {
            alphaPlayerAction.endAction();
        }
    }

    public int getDuration() {
        IMediaPlayer<AbsPlayer<AbsPlayer>> iMediaPlayer = this.LIZJ;
        if (iMediaPlayer == null) {
            return -1;
        }
        try {
            return iMediaPlayer.getVideoInfo().getDuration();
        } catch (Exception unused) {
            return -1;
        }
    }

    public String getPlayerType() {
        IMediaPlayer<AbsPlayer<AbsPlayer>> iMediaPlayer = this.LIZJ;
        if (iMediaPlayer != null) {
            return iMediaPlayer.getPlayerSimpleName();
        }
        return "unknown";
    }

    public boolean isPlaying() {
        IMediaPlayer<AbsPlayer<AbsPlayer>> iMediaPlayer = this.LIZJ;
        if (iMediaPlayer == null || !iMediaPlayer.isPlaying()) {
            return false;
        }
        return true;
    }

    public void reset() {
        IMediaPlayer<AbsPlayer<AbsPlayer>> iMediaPlayer = this.LIZJ;
        if (iMediaPlayer != null) {
            iMediaPlayer.reset();
            this.LIZ = C1095Tlf.NOT_PREPARED;
            this.LJIIIIZZ = false;
        }
    }

    public void pause() {
        if (this.LIZJ != null && this.LIZ == C1095Tlf.STARTED) {
            this.LIZJ.pause();
            this.LIZ = C1095Tlf.PAUSED;
        }
    }

    public void resume() {
        if (this.LJIIIIZZ) {
            startPlay();
        } else if (this.LJIIJJI) {
            this.LJIIJJI = false;
            LIZIZ();
        }
    }

    public void stop() {
        if (this.LIZJ == null) {
            return;
        }
        if (this.LIZ == C1095Tlf.STARTED || this.LIZ == C1095Tlf.PAUSED) {
            this.LIZJ.pause();
            this.LIZ = C1095Tlf.PAUSED;
        }
    }

    private void LIZIZ() {
        if (this.LIZJ != null) {
            if (this.LIZ == C1095Tlf.NOT_PREPARED || this.LIZ == C1095Tlf.STOPPED) {
                this.LIZJ.setOnPreparedListener(this.LJIIL);
                this.LIZJ.setOnErrorListener(this.LJIILIIL);
                this.LIZJ.prepareAsync();
            }
        }
    }

    public void release() {
        this.LIZLLL.onPause();
        if (this.LIZJ == null) {
            this.LIZ = C1095Tlf.NOT_PREPARED;
            return;
        }
        if (this.LIZ == C1095Tlf.STARTED) {
            this.LIZJ.pause();
            this.LIZ = C1095Tlf.PAUSED;
        }
        if (this.LIZ == C1095Tlf.PAUSED) {
            this.LIZJ.stop();
            this.LIZ = C1095Tlf.STOPPED;
        }
        this.LIZJ.release();
        this.LIZLLL.LIZLLL();
        this.LIZ = C1095Tlf.RELEASE;
    }

    public void startPlay() {
        if (this.LIZJ != null) {
            int i = C1096Tlg.LIZ[this.LIZ.ordinal()];
            if (i == 1) {
                this.LIZJ.start();
                this.LJIIIIZZ = true;
                this.LIZ = C1095Tlf.STARTED;
                AlphaPlayerAction alphaPlayerAction = this.LIZIZ;
                if (alphaPlayerAction != null) {
                    alphaPlayerAction.startAction();
                }
            } else if (i == 2) {
                this.LIZJ.start();
                this.LIZ = C1095Tlf.STARTED;
            } else if (i == 3 || i == 4) {
                try {
                    LIZIZ();
                } catch (Exception e) {
                    0HH.LIZ(e);
                    LIZ(false, "prepare and start MediaPlayer failure.");
                    LIZ();
                }
            }
        }
    }

    public void setMonitor(IMonitor iMonitor) {
        this.LJIIJ = iMonitor;
    }

    public IPlayerController withVideoAction(AlphaPlayerAction alphaPlayerAction) {
        this.LIZIZ = alphaPlayerAction;
        return this;
    }

    public void seekTo(int i) {
        IMediaPlayer<AbsPlayer<AbsPlayer>> iMediaPlayer = this.LIZJ;
        if (iMediaPlayer != null) {
            iMediaPlayer.seekTo(i);
        }
    }

    public void setSurface(Surface surface) {
        this.LIZJ.setSurface(surface);
    }

    public void setVisibility(int i) {
        this.LIZLLL.setVisibility(i);
        if (i == 0) {
            this.LIZLLL.bringToFront();
        }
    }

    public void detachAlphaView(ViewGroup viewGroup) {
        MethodCollector.i(3719);
        if (viewGroup == null) {
            MethodCollector.o(3719);
            return;
        }
        viewGroup.removeView(this.LIZLLL);
        MethodCollector.o(3719);
    }

    public void attachAlphaView(ViewGroup viewGroup) {
        MethodCollector.i(3715);
        if (viewGroup == null) {
            MethodCollector.o(3715);
            return;
        }
        ViewGroup viewGroup2 = (ViewGroup) this.LIZLLL.getParent();
        if (viewGroup2 != null) {
            viewGroup2.removeView(this.LIZLLL);
        }
        if (viewGroup.indexOfChild(this.LIZLLL) == -1) {
            viewGroup.addView(this.LIZLLL);
        }
        MethodCollector.o(3715);
    }

    public void start(DataSource dataSource) {
        String str;
        this.LJII = dataSource.getMessageId();
        if (dataSource.isValid()) {
            setVisibility(0);
            try {
                this.LIZJ.reset();
                this.LIZ = C1095Tlf.NOT_PREPARED;
                int i = this.LJIIIZ.getResources().getConfiguration().orientation;
                DataSource.DataInfo dataInfo = dataSource.getDataInfo(i);
                if (dataInfo != null && !TextUtils.isEmpty(dataInfo.getPath())) {
                    if (new File(dataInfo.getPath()).exists()) {
                        this.LIZLLL.setConfigParams(dataInfo);
                        this.LIZJ.setDataSource(dataInfo.getPath());
                        this.LJ = dataInfo.getActualWidth();
                        this.LJFF = dataInfo.getActualHeight();
                        this.LJI = dataInfo.getVersion();
                        if (this.LIZLLL.LIZ) {
                            LIZIZ();
                            return;
                        } else {
                            this.LJIIJJI = true;
                            return;
                        }
                    }
                }
                if (1 == i) {
                    StringBuilder sb = new StringBuilder("dataPath is empty or File is not exists. path: ");
                    if (dataInfo == null) {
                        str = "null";
                    } else {
                        str = dataInfo.getPath();
                    }
                    sb.append(str);
                    LIZ(false, sb.toString());
                }
                LIZ();
            } catch (Exception e) {
                0HH.LIZ(e);
                LIZ();
                LIZ(false, "alphaVideoView set dataSource failure:" + Log.getStackTraceString(e));
            }
        } else {
            LIZ();
            LIZ(false, "dataSource is invalid. ErrorInfo: " + dataSource.getErrorInfo());
        }
    }

    private void LIZ(boolean z, String str) {
        LIZ(false, 0, 0, str);
    }

    private void LIZ(boolean z, int i, int i2, String str) {
        String str2;
        IMonitor iMonitor = this.LJIIJ;
        if (iMonitor != null) {
            IMediaPlayer<AbsPlayer<AbsPlayer>> iMediaPlayer = this.LIZJ;
            if (iMediaPlayer != null) {
                str2 = iMediaPlayer.getPlayerSimpleName();
            } else {
                str2 = "unknown";
            }
            iMonitor.monitor(z, str2, 0, 0, str + ", messageId: " + this.LJII);
        }
    }
}
