import { NativeModules } from 'react-native';
import { mkdir, exists } from 'react-native-fs'

const { AudioMixer: Mixer } = NativeModules;

export type EffectObject = {
    id: string,
    name: string,
    pitch: number,
    rate: number,
    isPlaying?: boolean,
}

function AudioMixer() {
}

AudioMixer.prototype.init = function (path: string) {
    Mixer.setPath(path)
    Mixer.createDBMedia()
}

AudioMixer.prototype.setEffect = function (effect: EffectObject) {
    Mixer.setEffect(effect)
}

AudioMixer.prototype.playEffect = function () {
    Mixer.playEffect()
}

AudioMixer.prototype.saveEffect = async function (path: string) {
    const files = path.split('/')
    var pathToVerify = '/' + files[1]

    for (var i = 2; i < files.length - 1; i++) {
        if (! await exists(pathToVerify)) {
            await mkdir(pathToVerify)
        }
        pathToVerify += '/' + files[i]
    }

    return Mixer.saveEffect(path)
}

export default AudioMixer