package com.xixun.xixunplayer;

import android.annotation.SuppressLint;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.view.Choreographer;
import android.view.Surface;
import android.view.TextureView;

import androidx.annotation.NonNull;

@SuppressLint("ViewConstructor")
public class SrcVideo2 extends TextureView implements TextureView.SurfaceTextureListener, Choreographer.FrameCallback {

    float vol;
    int dur;
    MediaPlayer player;
    boolean isLive;

    public SrcVideo2(Prog prog, String path, float vol, int dur, boolean isLive) {
        super(prog.getContext());
        this.vol = vol;
        this.dur = dur;
        this.isLive = isLive;
        player = new MediaPlayer();
        try {
            setSurfaceTextureListener(this);
            player.setDataSource(path);
            player.setLooping(true);
            player.setVolume(vol, vol);
            player.setOnPreparedListener((MediaPlayer player)->{
                player.setOnPreparedListener(null);
                var diff = dur - player.getDuration();
                if(diff>0 && diff<=1000) player.setLooping(false);
                if(isShown()) player.start();
                //prog.calls.add(this);
            });
            player.setOnErrorListener((MediaPlayer mp, int what, int extra)->{
                Util.println("Media Error: "+getErrorName(what)+". "+getErrorName(extra));
                return true;
            });
            player.prepareAsync();
        } catch (Exception e) {
            Util.printStackTrace(e);
            Util.makeText(getContext(), Util.toStr(e)).show();
        }
    }
    @Override
    public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
        if(player!=null) player.setSurface(new Surface(surface));
    }
    @Override
    public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {}
    @Override
    public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
        return false;
    }
    @Override
    public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {}

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

    void release() {
        if(player!=null) {
            player.release();
            player = null;
        }
    }
    @Override
    public void doFrame(long ms) {
//        if(isShown()) return;
//        if(player!=null) {
//            player.pause();
//            player.seekTo(0);
//        }
    }
    static String getErrorName(int code) {
        if(code==MediaPlayer.MEDIA_ERROR_UNKNOWN) return "UNKNOWN";
        if(code==MediaPlayer.MEDIA_ERROR_SERVER_DIED) return "SERVER_DIED";
        if(code==MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) return "NOT_VALID_FOR_PROGRESSIVE_PLAYBACK";
        if(code==MediaPlayer.MEDIA_ERROR_IO) return "IO";
        if(code==MediaPlayer.MEDIA_ERROR_MALFORMED) return "MALFORMED";
        if(code==MediaPlayer.MEDIA_ERROR_UNSUPPORTED) return "UNSUPPORTED";
        if(code==MediaPlayer.MEDIA_ERROR_TIMED_OUT) return "TIMED_OUT";
        if(code==-2147483648) return "SYSTEM";
        return "Unknown ("+code+")";
    }
}