package com.fgodt.mixplayer;

import android.content.Context;

import java.util.ArrayList;


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

public class Player extends Render {


    public final int STATUS_ERROR = 0;
    public final int STATUS_STOP = 1;
    public final int STATUS_START = 2;
    public final int STATUS_PAUSE = 3;
    public final int STATUS_BUFFING = 4;
    public final int STATUS_RUNNING = 5;

    private Core mixCore;
    private Core enMixCore;
    private String url ;
    private mixPlayerCallBack callBack;
    private boolean needSelectAudioSource = false;

    public Player(Context context){
        super(context);
    }

    ArrayList<mixSource> mixSourceName = new ArrayList<mixSource>();
    private void addMixList(){
        if(mixCore == null){
            return;
        }
        for (mixSource source :mixSourceName){
            mixCore.addMixSource(source.name, source.startTime, source.duration);
        }
    }

    private void selectAudioSource(){
        if(mixCore == null){
            return;
        }
        for (mixSource source :mixSourceName){
            if(source.select){
                mixCore.selectAudioSource(source.name);
            }
        }
    }

    private void createCore(){
        if(mixCore != null){
            mixCore.mixClose();
            this.renderClose();
        }
        mixCore = new Core();
        mixCore.init(this);
        mixCore.setMixCall(callBack);
        addMixList();
        if(needSelectAudioSource){
            selectAudioSource();
        }
    }

    private void createEnCore(){
        if(enMixCore != null){
            enMixCore.mixClose();
        }
        enMixCore = new Core();
    }

    public void open(String url){
        createCore();
        this.url = url;
        mixCore.setUrl(url);
        new Thread(new Runnable() {
            @Override
            public void run() {
                mixCore.open();
            }
        }).start();
    }


    public void resume(){
        if(mixCore!=null){
            mixCore.resume();
        }
        ispause = false;
    }
    public void stop(){
        if(mixCore!=null){
            mixCore.mixClose();
            this.renderClose();
        }
    }
    public void pause(){
        if(mixCore!=null){
            mixCore.pause();
        }
        ispause = true;
    }
    public void seek(double time){
        if(mixCore != null){
            mixCore.seek(time);
        }
    }
    public void setMixCall(mixPlayerCallBack call){
        this.callBack = call;
    }
    public double getDuration(){
        if(mixCore!=null){
            return mixCore.mixGetDuration();
        }
        return -1;
    }
    public double getPlayTime(){
        double time = super.getPlaytime();
        return time;
    }

    public double getSaveTime(){
        return -1;
    }

    public void addMixSource(String name ,double stime, double duration){
        mixSource source = new mixSource();
        source.name = name;
        source.startTime = stime;
        source.duration = duration;
        source.select  = false;
        mixSourceName.add(source);
    }

    public void saveFile(String input, String output){
        createCore();
        this.url = input;
        mixCore.setUrl(url);
        mixCore.setSavePath(output);
        mixCore.save();
    }

    public void selectAudioSource(String name){
        needSelectAudioSource = true;
        for(mixSource source : mixSourceName){
            if(source.name.equals(name)){
                source.select = true;
            }
        }
    }

    public int getStatus(){
        if(mixCore != null){
            return  mixCore.getStatus();
        }
        return STATUS_STOP;
    }

    public void setVolume(float volume){
        this.volume = volume;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        if(mixCore!= null)
            mixCore.mixClose();
    }

    class mixSource{
        public String name;
        public double startTime;
        public double duration;
        public boolean select;
    }
}
