{"size": 1401, "ext": "js", "max_stars_repo_path": "src/store/reducers/audio.js", "max_stars_repo_name": "bsinou/vitrnx", "max_stars_repo_stars_event_min_datetime": "2018-04-16T22:26:37.000Z", "max_stars_repo_stars_event_max_datetime": "2018-10-18T16:16:03.000Z", "max_issues_repo_path": "src/store/reducers/audio.js", "max_issues_repo_name": "bsinou/vitrnx", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store/reducers/audio.js", "max_forks_repo_name": "bsinou/vitrnx", "max_forks_repo_forks_event_min_datetime": "2018-04-16T22:30:54.000Z", "max_forks_repo_forks_event_max_datetime": "2018-10-18T16:16:35.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 26.9423076923, "max_line_length": 96, "alphanum_fraction": 0.6559600286}
import * as actionTypes from '../actions/actionTypes';
import { updateObject } from '../utility';

import { PLTracks } from "../../assets/conf/playList.jsx";

// function getURI(fName) {
//     return 'musicRepo/Radio/' + encodeURI(fName);
// }

const initialState = {
    comingDialogOpen: false,
    tracks: PLTracks, 
    currTrack: PLTracks[0],
    currPlaying: 0,
    playing: false,
};

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case actionTypes.AUDIO_TOGGLE_PLAYING_STATUS: return togglePlayingStatus(state, action);
        case actionTypes.AUDIO_SKIP_TRACK: return loadTrack(state, action);
        case actionTypes.AUDIO_FORCE_PAUSE: return forcePause(state, action);    
        default:
            return state;
    }
};

const togglePlayingStatus = (state, action) => {
    return updateObject(state, {
        playing: !state.playing,
    });
};

const forcePause = (state, action) => {
    return updateObject(state, {
        playing: false,
    });
};

const loadTrack = (state, action) => {
    let newIndex = state.currPlaying + action.direction;
    // Not very elegant way of working around negative numbers.
    newIndex += state.tracks.length;
    newIndex = newIndex % state.tracks.length
    return updateObject(state, {
        currPlaying: newIndex,
        currTrack: state.tracks[newIndex]
    });
};

export default reducer;
