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: 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: "Favorites"
            description: "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: "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: "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: "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: "Favorites"
            hintText: "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._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 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)
        }
    }
}