/**
 * NullpodConfig
 *
 * Copyright 2017 Joel Braun
 *
 * This file is part of nullPod.
 *
 * nullPod is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nullPod is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with nullPod.  If not, see <http://www.gnu.org/licenses/>.

 */

package com.cyrix.nullpod.data

import com.cyrix.util.CxLogger
import com.cyrix.util.warn
import org.json.JSONArray
import org.json.JSONObject

data class NullpodConfig(val playlist: MutableList<PodcastTrack> = mutableListOf(), var maxDownloadedTracks: Int = 5) {

    private val log = CxLogger<NullpodConfig>()

    // todo: observable pattern for immediate commits?

    companion object {
        const val PLAYLIST_ARRAY = "playlist"
        const val MAX_DOWNLOAD_TRACKS = "maxdownloadedtracks"
    }

    // initially, wanted to store playlist in objectbox as part of the serialized NullpodConfig class.. however, while objectbox does store
    // the mutable list in the data store, the sort order is nuked on reloading the database (order comes back as ID asc).. which defeats the
    // purpose..
    // option would be to store a separate order of track IDs, but at that point might as well just use sharedPreferences for the list, storing them as
    // a delimited string; loading and storing as necessary

    // todo: other fields:
    // todo: download limit
    // todo: bool download wifi only

    // todo: need to handle exceptions in serialization/deserialization

    //---------------------------------------------------------------------------
    fun serialize(): JSONObject {

        val json = JSONObject()

        // playlist
        val jsonPlaylist = JSONArray()
        playlist.forEach { track ->
            jsonPlaylist.put(track.id)
        }
        json.put(PLAYLIST_ARRAY, jsonPlaylist)
        json.put(MAX_DOWNLOAD_TRACKS, maxDownloadedTracks)

        return json
    }

    //---------------------------------------------------------------------------
    fun deserialize(json: JSONObject, getTrack: (Long) -> PodcastTrack?) {

        playlist.clear()
        val jsonPlaylist = json.optJSONArray(PLAYLIST_ARRAY)

        if (jsonPlaylist == null) {
            log.warn { "json playlist is null; cannot deserialize current playlist!" }
            return  // todo: shouldn't return here; need to init other fields
        }

        for (idx in 0 until jsonPlaylist.length()) {
            //log.debug { "idx:$idx, val: ${jsonPlaylist.getInt(idx)}"}
            val track = getTrack(jsonPlaylist.getLong(idx))
            if (track != null) {
                playlist.add(track)
            } else {
                log.warn { "couldn't add track to playlist with id ${jsonPlaylist.getInt(idx)}!!" }
            }
        }

        maxDownloadedTracks = json.optInt(MAX_DOWNLOAD_TRACKS, 5)
    }
}




