import QtQuick 2.0
import Sailfish.Silica 1.0
import "../JSONListModel"
import "../delegates"

Page {
    id: page
    //property string filter: ""
    //property string country: ""
    //property string _country: country ? "?country="+country : ""
    //property string jsonSource: "http://api.tvmaze.com/schedule"+_country //"date="+Qt.formatDateTime(new Date(), "yyyy-MM-dd")
    property string jsonSource: "http://api.tvmaze.com/shows"
    property bool showweight: false

    SilicaListView {
         id: listView
         anchors.fill: parent
         clip: true

         JSONListModel {
             id: jsonModel1
             sortby: showweight ? "weight" : "rating.average"
             source: jsonSource //"http://api.tvmaze.com/schedule?country=SE&date="+Qt.formatDateTime(new Date(), "yyyy-MM-dd") //2016-07-16" //+filter
             query: showweight ? "$[?(@.weight>7)]" : "$[?(@.rating.average>7.9)]"
         }

         model: jsonModel1.model

         BusyIndicator {
             id: busyIndicator
             running: !jsonModel1.jsonready
             size: BusyIndicatorSize.Large
             anchors.horizontalCenter: parent.horizontalCenter
             anchors.verticalCenter: parent.verticalCenter
        }

         header: PageHeader {
             id: pHeader
             title: showweight ? "Popular shows by clicks" : "Popular shows by rating"
         }

         delegate: ShowDelegate {}

         PullDownMenu {
             MenuItem {
                 visible: showweight
                 text: qsTr("Popular shows by rating")
                 onClicked: pageStack.replace(Qt.resolvedUrl("PopularPage.qml"),{"showweight": false},PageStackAction.Immediate)
             }
             MenuItem {
                 visible: !showweight
                 text: qsTr("Popular shows by clicks")
                 onClicked: pageStack.replace(Qt.resolvedUrl("PopularPage.qml"),{"showweight": true},PageStackAction.Immediate)
             }
             MenuItem {
                 text: qsTr("Favourites")
                 onClicked: pageStack.replace(Qt.resolvedUrl("Favorites.qml"),{},PageStackAction.Immediate)
             }
             MenuItem {
                 text: qsTr("Search shows")
                 onClicked: pageStack.replace(Qt.resolvedUrl("SearchPage.qml"),{},PageStackAction.Immediate)
             }
         }

         ViewPlaceholder {
             visible: listView.count === 0 && jsonModel1.jsonready
             text: "Unable to load TVmaze data!"
             hintText: "Check your connection"
         }
    }
}