import {backURL} from "../actions";
import { select, put, takeEvery } from "redux-saga/effects";
import {store} from "./store";


export const playerReducer = (
    state = {},
    {type,track,isPlaying=false,isPaused,duration,
        playlist,playlistIndex,currentTime=0,volume=1,}) => {

    if (type === "NEXT_TRACK") {
        return {
            ...state,
            playlistIndex,
        }

    }

    if (type === "PREVIOUS_TRACK") {
        return {
            ...state,
            playlistIndex,
        }

    }


    if (type === "TRACK_LOAD") {
        return {
            ...state,
            playlistIndex,
        }

    }

    if (type === "TRACK_PLAY") {
        return {
            ...state,
            isPlaying,
            isPaused: !isPlaying,
        };
    }

    if (type === "TRACK_STOP") {
        return {
            ...state,
            isPaused,
            isPlaying: !isPaused,
        };
    }

    if (type === "TRACK_VOLUME") {
        return {
            ...state,
            volume,
        };
    }

    if (type === "TRACK_DURATION") {
        return {
            ...state,
            duration,
        };
    }

    if (type === "TRACK_CURRENT_TIME") {
        return {
            ...state,
            currentTime,
        };
    }

    if (type === "SET_PLAYLIST") {
        console.log(playlist,'lol')
        return {
            ...state,
            playlist,
            track,
        };
    }
    return state;
};


export const audio = new Audio()
audio.onended = () => store.dispatch (actionNextTrack())



export const actionTrackLoad = (trackIndex) =>
    ({type:'ACTION_TRACK_LOAD', trackIndex})

export function* trackLoadWorker (action) {
    let {trackIndex} = action
    let {player:{playlist}} = yield select ()
    //  if (playlistIndex !== trackIndex) {
    audio.src = backURL + '/'+ playlist[trackIndex].url
    audio.load()
    console.log(trackIndex)
    audio.ondurationchange = function(){actionSetDuration()}
    audio.ontimeupdate = function(){actionTrackCurrentTime()}

    yield put({type:"TRACK_LOAD",playlistIndex:trackIndex})
    yield put (actionTrackPlay())

}
export function* trackLoadWatcher() {
    yield takeEvery ('ACTION_TRACK_LOAD',trackLoadWorker)
}






export const actionTrackPlay = () =>
    ({type:'ACTION_TRACK_PLAY'})
export function* trackPlayWorker () {
    yield audio.play()
    yield put({type:"TRACK_PLAY",isPlaying:true,isPaused:false})
    console.log('play')
}
export function* trackPlayWatcher() {
    yield takeEvery ('ACTION_TRACK_PLAY',trackPlayWorker)
}



export const actionNextTrack = () =>
    ({type:'ACTION_NEXT_TRACK'})

export function* nextTrackWorker () {
    let {player} = yield select ()
    let playlistIndex = player.playlistIndex
    let playlist = player.playlist
       if (playlist.length === playlistIndex + 1) {
           yield put({type:"NEXT_TRACK",playlistIndex:0})
           audio.src = backURL + '/'+ player.playlist[0].url
           audio.load()
           audio.ondurationchange = function(){actionSetDuration()}
           audio.ontimeupdate = function(){actionTrackCurrentTime()}
           audio.play()
       } else {
           yield put({type:"NEXT_TRACK",playlistIndex:playlistIndex+1})
           audio.src = backURL + '/'+ player.playlist[playlistIndex+1].url
           audio.load()
           audio.ondurationchange = function(){actionSetDuration()}
           audio.ontimeupdate = function(){actionTrackCurrentTime()}
           audio.play()
       }
    console.log(playlistIndex)
}
export function* nextTrackWatcher() {
    yield takeEvery ('ACTION_NEXT_TRACK',nextTrackWorker)
}




export const actionPreviousTrack = () =>
    ({type:'ACTION_PREVIOUS_TRACK'})

export function* previousTrackWorker () {
    let {player} = yield select ()
    let p = player.playlistIndex
    if (p < 1) {
        yield put({type:"NEXT_TRACK",playlistIndex:0})
        audio.src = backURL + '/'+ player.playlist[0].url
        audio.load()
        audio.ondurationchange = function(){actionSetDuration()}
        audio.ontimeupdate = function(){actionTrackCurrentTime()}
        audio.play()
    } else {
        yield put({type:"PREVIOUS_TRACK",playlistIndex:p-1})
        audio.src = backURL + '/'+ player.playlist[p-1].url
        audio.load()
        audio.ondurationchange = function(){actionSetDuration()}
        audio.ontimeupdate = function(){actionTrackCurrentTime()}
        audio.play()
    }


}
export function* previousTrackWatcher() {
    yield takeEvery ('ACTION_PREVIOUS_TRACK',previousTrackWorker)
}






export const actionTrackStop = () =>
    ({type: 'ACTION_TRACK_STOP'})

export function* trackStopWorker () {
    audio.pause()
    yield put({type:"TRACK_STOP",isPlaying:false,isPaused:true})
}
    export function* trackStopWatcher() {
        yield takeEvery ('ACTION_TRACK_STOP',trackStopWorker)
    }





export const actionTrackVolume = (e) =>
    ({type:'ACTION_TRACK_VOLUME', e})
export function* trackVolumeWorker (action) {
    let {e} = action
    audio.volume = e
    yield put({type:"TRACK_VOLUME", volume:e})
}
export function* trackVolumeWatcher() {
    yield takeEvery ('ACTION_TRACK_VOLUME',trackVolumeWorker)
}




export const actionSetDuration = () => {
    let e = audio.duration
    store.dispatch({type:"TRACK_DURATION",duration:e})
}


export const actionTrackCurrentTime = (e) => {
    if (e) {
        audio.currentTime = e
        store.dispatch({type:"TRACK_CURRENT_TIME", currentTime:e})
    } else {
        let e = audio.currentTime
        store.dispatch({type:"TRACK_CURRENT_TIME", currentTime:e})
    }

}


//
// export const actionTrackCurrentTime = (e) =>
//     ({type:'CURRENT_TIME', e})
//
//
// export function* trackCurrentTimeWorker (action) {
//     let {e} = action
//     if (e) {
//         audio.currentTime = e
//         yield put({type:"TRACK_CURRENT_TIME", currentTime:e})
//     } else {
//        e = audio.currentTime
//        yield put({type:"TRACK_CURRENT_TIME", currentTime:e})
//     }
//
// }
//
// export function* trackCurrentTimeWatcher() {
//     yield takeEvery('CURRENT_TIME', trackCurrentTimeWorker)
// }









export const actionSetPlaylist = (playlistP,trackIndex,track) =>
    ({type:'ACTION_SET_PLAYLIST', playlistP,trackIndex,track})

export function* setPlaylistWorker(action) {
    let {playlistP, trackIndex,track} = action
    yield put ({type:"SET_PLAYLIST", playlist:playlistP,track:track})
    yield put (actionTrackLoad(trackIndex))
}

export function* setPlaylistWatcher() {
    yield takeEvery ('ACTION_SET_PLAYLIST',setPlaylistWorker)
}


