import QtQuick 2.2
import Sailfish.Silica 1.0
import QtQuick.LocalStorage 2.0
import "../delegates"
import "../helpers/db.js" as Favorites

Page {
    property ListModel history: ListModel {id: history}
    SilicaListView {
        id: view

        enabled: !splashItem.visible
        //visible: enabled
        opacity: enabled ? 1 : 0
        anchors.fill: parent
        anchors.bottomMargin: mediaPlayerPanel.visibleSize
        clip: mediaPlayerPanel.expanded
        onVisibleChanged: if (visible) updateHist()
        ViewPlaceholder {
            anchors.centerIn: allRadio.center
            enabled: view.count == 0
            text: "History"
            hintText: "see you play history here"
        }

        VerticalScrollDecorator {}
        header: PageHeader{title: "Play history";description: "Radio stations: "+view.count}


       /* onAtYEndChanged: {
            if (atYEnd && view.count >= getCountryStations.limit) {
            //    console.log("AT BOTTOM: "+view.count)
                if (getCountryStations.offset + getCountryStations.limit < stationCount) {
                    getCountryStations.offset = getCountryStations.offset + getCountryStations.limit + 1
                    getCountryStations.getStations()
                }
            }
        } */
        model: history
        delegate: StationsDelegate{
            onClicked: {
                if (model.url_resolved !== radioPlayer._url_resolved) {
                    radioPlayer._favicon = model.favicon
                    radioPlayer._name = model.name
                    radioPlayer._countrycode = model.countrycode
                    radioPlayer._tags = model.tags
                    radioPlayer._codec = model.codec//getCodec(model.codec)
                    radioPlayer._bitrate = model.bitrate //getBitrate(model.bitrate)
                    radioPlayer._hls = model.hls
                    radioPlayer._url_resolved = model.url_resolved
                    radioPlayer._homepage = model.homepage
                    radioPlayer._stationuuid = model.stationuuid
                    radioPlayer._favorite = model.favorite ? true : false
                    //pageStack.push("RadioPlayerPage.qml")
                }
            }
        }
        function updateHist() {
            Favorites.getHistory(history,10000)
        }

        Component.onCompleted: updateHist()

    }
}
