package com.example.exportmedia

import io.reactivex.Observable
import java.io.File

class AudioEditor(
        private val mediaHelper: MediaHelper,
        private val editAudioFile: File,
        private val startTime: String,
        private val endTime: String,
        private val volume: Float) {

    private constructor(builder: AudioEditor.Builder) : this(
            builder.mediaHelper,
            builder.editAudioFile,
            builder.startTime,
            builder.endTime,
            builder.volume
    )

    fun output(file: File): Observable<String> {
        return Observable.create {
            mediaHelper.execute(arrayOf(
                    "-i", editAudioFile.path,
                    "-ss", startTime,
                    "-t", endTime,
                    "-filter:a", "volume=$volume",
                    "-y", file.path
            ), object : MediaCallBack {
                override fun onExecuteSuccess(message: String?) {
                    message?.let { it1 -> it.onNext(it1) }
                }

                override fun onExecuteFailed(message: String?) {
                    it.onError(Throwable(message))
                }

                override fun onExecuteFinish() {
                    it.onComplete()
                }
            })
        }
    }

    class Builder(internal val mediaHelper: MediaHelper) {

        lateinit var editAudioFile: File

        lateinit var startTime: String

        lateinit var endTime: String

        var volume: Float = 0.0f

        fun build() = AudioEditor(this)
    }
}