package com.xixun.xixunplayer;

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

import androidx.annotation.NonNull;

public class SrcVideo2 extends TextureView implements TextureView.SurfaceTextureListener, Choreographer.FrameCallback {

    float vol;
    MediaPlayer player;
    char state;
    boolean isLive;

    public SrcVideo2(Prog prog, String path, float vol, boolean isLive) {
        super(prog.getContext());
        this.vol = vol;
        this.isLive = isLive;
        player = new MediaPlayer();
        setSurfaceTextureListener(this);
        player.setLooping(true);
        player.setVolume(vol, vol);
        player.setOnPreparedListener((MediaPlayer player)->{
            player.setOnPreparedListener(null);
            Util.println("  ------Prepared isShown"+isShown()+" "+this);
            if(isShown()) {
                player.start();
                state = 'S';
            } else state = 'P';
            prog.calls.add(this);
        });
        player.setOnErrorListener((MediaPlayer mp, int what, int extra)->{
            var err = "Media Error: "+getErrorName(what)+". "+getErrorName(extra);
            Util.makeText(getContext(), err).show();
            Util.println(err);
            return true;
        });
        try {
            player.setDataSource(path);
            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) {
        setSurfaceTextureListener(null);
        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(state==0) return;
        if(isVisible) {
            Util.println("  ------start "+this);
            player.seekTo(0);
            player.start();
            if(isLive) player.setVolume(vol, vol);
        } else if(isLive) player.setVolume(0, 0);
        else {
            Util.println("  ------pause "+this);
            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+")";
    }
}