package com.fgodt.mixplayer;

import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;


import java.util.Arrays;

/**
 * Created by rt-zl on 2019/2/15.
 */

public class Core {
    static {
        System.loadLibrary("mixplayer");
    }

    private native int  setJNI(long mix);
    private native long createMix();
    private native void closeMix(long mix,Render render);
    private native int setPlayUrl(long mix,String url);
    private native int open(long mix);
    private native int setMixCall(long mix);
    private native int setSurface(long mix, Surface surface);
    private native int selectAudio(long mix, String name);
    private native int addMixSource(long mix, String name,double stime, double duration);
    private native double getMediaDuration(long mix);
    private native int save(long mix, String output);
    private native void pause(long mix);
    private native void resume(long mix);
    private native void seek(long mix, double time);
    private native int getStatus(long mix);

    private String url;

    private Render render;

    private String output;

    private mixPlayerCallBack callBack;



    private long mixHandler;

    public void setUrl(String url){
        this.url = url;
    }

    public void setSavePath(String output){
        this.output = output;
    }

    public void setSurface(Surface surface){
        // setSurface(mixHandler,surface);
    }

    public String getUrl(){
        return this.url;
    }


    public boolean init(Render render){
        this.render = render;
        this.mixHandler = createMix();
        int ret = setJNI(this.mixHandler);
        if(ret!=0){
            return false;
        }
        this.render.init(this.mixHandler);
        setMixCall(mixHandler);
        return true;
    }


    public boolean open(){
        int ret =setPlayUrl(mixHandler,this.url);
        if(ret!=0){
            return false;
        }
        ret = open(mixHandler);
        if(ret!=0){
            return false;
        }
        return true;
    }

    public boolean save(){
        int ret = setPlayUrl(mixHandler,this.url);
        if(ret != 0){
            return false;
        }
        ret = save(mixHandler,this.output);
        if(ret != 0){
            return false;
        }
        return true;
    }

    public  long getMixCtx(){
        return mixHandler;
    }

    public void mixClose(){
        render.mixClose();
        closeMix(mixHandler,render);
        mixHandler = 0;
    }

    public void nativeClose(){
        Log.d("MIXPLAYER","CLOSE");
        if(this.callBack != null){
            this.callBack.closeMsg("CLOSE");
        }
    }

    public void nativeCall(){

    }

    public void nativeMsg(String msg){
        Log.d("MIXPLAYER",msg);
    }


    public int mixDataCall(String name,byte[] data,int len){
        if(name == null || data == null || len <= 0){
            return 0;
        }
        if(this.callBack!=null){
            int ret = this.callBack.mix_call(name,data,len);
            return ret;
        }
        return 0;
    }

    public double mixGetDuration() {
        if (mixHandler > 0)
            return getMediaDuration(mixHandler);
        else
            return -1;
    }

    public int addMixSource(String name,double stime, double duration){
        if(mixHandler >0) {
            return addMixSource(mixHandler,name,stime,duration);
        }
        return -1;
    }

    public void selectAudioSource(String name){
        selectAudio(mixHandler,name);
    }

    public void pause(){
        if(mixHandler>0){
            pause(mixHandler);
        }
    }

    public void resume(){
        if(mixHandler>0){
            resume(mixHandler);
        }
    }

    public void seek(double time){
        if(mixHandler>0){
            seek(mixHandler,time);
        }
    }

    void setMixCall(mixPlayerCallBack call){
        this.callBack = call;
    }

    public int getStatus(){
        if(mixHandler>0){
            return getStatus(mixHandler);
        }
        return 1;//stop
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        //make sure close
        if(mixHandler!=0)
            mixClose();
    }

}
