package com.hh.lib_record;


import android.annotation.SuppressLint;
import android.app.Application;
import android.media.projection.MediaProjection;
import android.os.Build;

import com.hh.lib_base.BaseApplication;
import com.hh.lib_record.recorder.RecordConfig;
import com.hh.lib_record.recorder.RecordHelper;
import com.hh.lib_record.recorder.RecordService;
import com.hh.lib_record.recorder.listener.RecordDataListener;
import com.hh.lib_record.recorder.listener.RecordFftDataListener;
import com.hh.lib_record.recorder.listener.RecordResultListener;
import com.hh.lib_record.recorder.listener.RecordSoundSizeListener;
import com.hh.lib_record.recorder.listener.RecordStateListener;
import com.hh.lib_record.utils.Logger;


/**
 * @author zhaolewei on 2018/7/10.
 */
public class RecordManager {
    private static final String TAG = RecordManager.class.getSimpleName();
    @SuppressLint("StaticFieldLeak")
    private volatile static RecordManager instance;

    private MediaProjection mediaProjection;

    private RecordManager() {
    }

    public static RecordManager getInstance() {
        if (instance == null) {
            synchronized (RecordManager.class) {
                if (instance == null) {
                    instance = new RecordManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     *
     * @param application Application
     * @param showLog     是否开启日志
     */
    public void init(Application application, boolean showLog) {
        Logger.IsDebug = showLog;
    }

    public void start() {
        Logger.i(TAG, "start...");
        RecordService.startRecording(BaseApplication.getContext());
    }

    public void stop() {

        RecordService.stopRecording(BaseApplication.getContext());
    }

    public void resume() {

        RecordService.resumeRecording(BaseApplication.getContext());
    }

    public void pause() {

        RecordService.pauseRecording(BaseApplication.getContext());
    }

    public void release() {
        instance = null;
        RecordService.stopRecording(BaseApplication.getContext());
    }

    /**
     * 录音状态监听回调
     */
    public void setRecordStateListener(RecordStateListener listener) {
        RecordService.setRecordStateListener(listener);
    }

    /**
     * 录音数据监听回调
     */
    public void setRecordDataListener(RecordDataListener listener) {
        RecordService.setRecordDataListener(listener);
    }

    /**
     * 录音可视化数据回调，傅里叶转换后的频域数据
     */
    public void setRecordFftDataListener(RecordFftDataListener recordFftDataListener) {
        RecordService.setRecordFftDataListener(recordFftDataListener);
    }

    /**
     * 录音文件转换结束回调
     */
    public void setRecordResultListener(RecordResultListener listener) {
        RecordService.setRecordResultListener(listener);
    }

    /**
     * 录音音量监听回调
     */
    public void setRecordSoundSizeListener(RecordSoundSizeListener listener) {
        RecordService.setRecordSoundSizeListener(listener);
    }


    public boolean changeFormat(RecordConfig.RecordFormat recordFormat) {
        return RecordService.changeFormat(recordFormat);
    }


    public boolean changeRecordConfig(RecordConfig recordConfig) {
        return RecordService.changeRecordConfig(recordConfig);
    }

    /**
     * RecordConfig.SOURCE_MIC
     * RecordConfig.SOURCE_SYSTEM
     *
     * @param source 音源
     */
    public boolean setSource(int source) {
        if (Build.VERSION.SDK_INT >= 29) {
            RecordService.getRecordConfig().setSource(source);
            return true;
        }
        return false;
    }

    public RecordConfig getRecordConfig() {
        return RecordService.getRecordConfig();
    }

    /**
     * 修改录音文件存放路径
     */
    public void changeRecordDir(String recordDir) {
        RecordService.changeRecordDir(recordDir);
    }

    /**
     * 获取当前的录音状态
     *
     * @return 状态
     */
    public RecordHelper.RecordState getState() {
        return RecordService.getState();
    }

    public void setMediaProjection(MediaProjection mediaProjection) {
        this.mediaProjection = mediaProjection;
    }

    public MediaProjection getMediaProjection() {
        return mediaProjection;
    }
}
