import song from "../../../assets/song.mp3"; // delete
import song2 from "../../../assets/little.mp3"; // delete

import store from "../../store";
import types from "../types";

const audio = new Audio(song);

export const togglePlay = (status) => {
    status ? audio.play() : audio.pause();
    return { type: types.TOGGLE_PLAY, payload: status };
};

export const setDuration = (e) => {
    const value = e.target.duration;

    store.dispatch({
        type: types.SET_DURATION,
        payload: value,
    });
};

export const setCurrentTime = (value) => {
    audio.currentTime = value;

    return { type: types.SET_CURRENT_TIME, payload: value };
};

export const setVolume = (value) => {
    audio.volume = value;

    return { type: types.SET_VOLUME, payload: value };
};

export const toggleRepeat = (status) => {
    audio.loop = status;

    return { type: types.TOGGLE_REPEAT, payload: status };
};

// audio listeners

const onEnded = () => {
    if (!audio.loop) {
        //next track if exists(if no toggle_play)
    }
};
const onTimeUpdate = (e) => {
    store.dispatch({
        type: types.SET_CURRENT_TIME,
        payload: e.target.currentTime,
    });
};

audio.addEventListener("ended", onEnded);
audio.addEventListener("timeupdate", onTimeUpdate);
audio.addEventListener("durationchange", setDuration);
