package com.xixun.xixunplayer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;

import androidx.annotation.NonNull;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

@SuppressLint("ViewConstructor")
public class SrcVideo extends TextureView implements TextureView.SurfaceTextureListener {

    String path;
    float vol;
    int dur;
    IjkMediaPlayer ijkPlayer;
    long bitRate;
    long seekTo;
    boolean isLive;

    public SrcVideo(Context context, String path, float vol, int dur, boolean useSW, boolean isLive) {
        super(context);
        this.path = path;
        this.vol = vol;
        this.dur = dur;
        this.isLive = isLive;
        init(useSW);
    }

    void init(boolean useSW) {
        ijkPlayer = new IjkMediaPlayer();
        if(! useSW) {
            ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-avc", 1);
            ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-hevc", 1);
        }
        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 1);
        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 12);
        try {
            setSurfaceTextureListener(this);
            ijkPlayer.setDataSource(path);
            ijkPlayer.setLooping(true);
            ijkPlayer.setVolume(vol, vol);
            ijkPlayer.setOnPreparedListener((IMediaPlayer var1)->{
                ijkPlayer.setOnPreparedListener(null);
                bitRate = ijkPlayer.getBitRate();
                var diff = dur - ijkPlayer.getDuration();
                if(diff>0 && diff<=1000) ijkPlayer.setLooping(false);
                if(isShown()) start();
            });
            ijkPlayer.setOnErrorListener((IMediaPlayer var1, int var2, int var3)->{
                Util.println("  Video Error: "+var1+" "+var2+" "+var3);
                return true;
            });
            ijkPlayer.prepareAsync();
        } catch (Throwable e) {
            Util.makeText(getContext(), Util.toStr(e)).show();
            Util.printStackTrace(e);
            ijkPlayer = null;
        }
    }
    @Override
    public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
        Util.println("  SurfaceTexture Available "+(ijkPlayer==null?"ijkPlayer==null":""));
        if(ijkPlayer!=null) ijkPlayer.setSurface(new Surface(surface));
    }
    @Override
    public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {}
    @Override
    public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
        Util.println("  SurfaceTexture Destroyed");
        return false;
    }
    @Override
    public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {}

    void start() {
        if(ijkPlayer!=null) {
            ijkPlayer.seekTo(seekTo);
            ijkPlayer.start();
        }
    }
    void pause() {
        if(ijkPlayer!=null) {
            ijkPlayer.pause();
            ijkPlayer.seekTo(0);
        }
    }
    void release() {
        if(ijkPlayer!=null) {
            ijkPlayer.release();
            ijkPlayer = null;
        }
    }

    String getState() {
        return "null";
    }

    @Override
    public void onVisibilityAggregated(boolean isVisible) {
        super.onVisibilityAggregated(isVisible);
        if(isVisible) {
            start();
            if(isLive) ijkPlayer.setVolume(vol, vol);
        } else if(isLive) ijkPlayer.setVolume(0, 0);
        else {
            seekTo = 0;
            pause();
        }
    }
}