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 favorites: ListModel {id: favorites}
    property real showP: 0.0
    property int tab: 0//allradioSettings.value("favoriteTab",0)
    SilicaListView {

        id: view
    //    enabled: !splashItem.visible
        opacity: enabled ? 1 : 0
        anchors.fill: parent
        anchors.bottomMargin: mediaPlayerPanel.visibleSize
        clip: mediaPlayerPanel.expanded

        onVisibleChanged: if (visible) updateFav()


        header: Column {
            width: parent.width
            spacing: Theme.paddingLarge
            PageHeader{
            title: qsTr("Favorites")
            description: qsTr("Radio stations: ")+view.count
            }

        Row {
            id: row

        //   width: parent.width
            //anchors.verticalCenter: parent.verticalCenter
            width: parent.width
             TabButton {
                id: home
                //anchors.verticalCenter: parent.verticalCenter
                down: tab === 0
                width: parent.width / 3
                fontSize: Theme.fontSizeMedium
                fontColor: down ? Theme.secondaryColor : Theme.secondaryColor
                iconVisible: false
          //      icon: "image://theme/icon-m-home" //"image://theme/icon-m-video"
                text: qsTr("By name")
                onButtonClick: {showP=0.0;tab = 0;Favorites.getFavorites(favorites,"name",10000)}
            }
            TabButton {
                id: search
               // anchors.verticalCenter: parent.verticalCenter
                down: tab === 1
                width: parent.width / 3
                fontSize: Theme.fontSizeMedium
                fontColor: down ? Theme.secondaryColor : Theme.secondaryColor
                iconVisible: false
            //    icon: "image://theme/icon-m-search"
                text: qsTr("Most played")
                onButtonClick: {showP=0.0;tab = 1;Favorites.getFavorites(favorites,"myclickcount",10000)}
            }
            TabButton {
                id: favorite
               // anchors.verticalCenter: parent.verticalCenter
                down: tab === 2
                width: parent.width / 3
                fontSize: Theme.fontSizeMedium
                fontColor: down ? Theme.secondaryColor : Theme.secondaryColor
                iconVisible: false
             //   icon: "image://theme/icon-m-favorite"
                text: qsTr("Last played")
                onButtonClick: {showP=0.0;tab = 2;Favorites.getFavorites(favorites,"myclicktimestamp",10000);console.log("CLICK")}
            }
        }
        Item {width: parent.width;height: Theme.paddingLarge}
        }


        ViewPlaceholder {
            anchors.centerIn: allRadio.center
            enabled: view.count == 0
            text: qsTr("Favorites")
            hintText: qsTr("add favorites here")
        }

        VerticalScrollDecorator {}
        model: favorites
        delegate: StationsDelegate{
            onClicked: {
                if (model.url_resolved !== radioPlayer._url_resolved) {
                    radioPlayer._favicon = model.favicon
                    radioPlayer._name = model.name
                    radioPlayer._cc = 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
                    radioPlayer.fav
                    radioPlayer.playlistIndex = index
                } else radioPlayer.resumeStream()
            }
        }
        function updateFav() {
            var sort
            switch (tab){
            case 0: sort = "name";break
            case 1: sort = "myclickcount";break
            case 2: sort = "myclicktimestamp";break
        }
            Favorites.getFavorites(favorites,sort,10000)
        }

        Component.onCompleted: {

            updateFav()
        }
        Component.onDestruction: {
            //allradioSettings.setValue("favoriteTab",tab)
        }
    }
}
