import QtQuick 2.0
import QtQuick.XmlListModel 2.0

XmlListModel {
    // radio-browser advanced:
    property string name: ""
    property bool nameExact: false
    property string countrycode: ""
    ////property string state: ""
    ////property bool stateExact: false
    ////property string language: ""
    ////property bool languageExact: false
    property string tag: ""
    property bool tagExcact: false
    ////property string tagList: "" // commaseparated string
    property string codec: ""
    ////property int bitrateMin: 0
    ////property int bitrateMax: 1000000
    property string order: "" // name, url, homepage, favicon, tags, country, state, language, votes, codec, bitrate, lastcheckok, lastchecktime, clicktimestamp, clickcount, clicktrend, random
    property bool reverse: false//order === name ? false : true // true if name
    property int offset: 0
    property int limit: 100000

    // Allradio specific:
   // property string filter: ""
    property string country: radioBrowser.getCountryName(countrycode)
   // property string searchby: "name"
   // property string searchorder: "clickcount"
    property bool running: false
    property string filterString

    //property AdvancedSearchModel searchModel : AdvancedSearchModel {id:searchModel}

    onRunningChanged: {console.log("RUNNING: "+running);if (running) getStations()}

    //onNameChanged: if (name !== "") tag = ""
    //onTagChanged: if (tag !=="") name = ""



    function getStations() {
        xml=""
        source=""
        filterString = ""
        //filterString = "&name="+name+"&nameExact="+nameExact+"&countrycode="+countrycode+"&tag="+tag+"&tagExact="+tagExcact+"&codec="+codec+"&order="+order+"&reverse="+reverse

        if (name !== "") {filterString = "&name="+name+"&nameExact="+nameExact}
        if (countrycode !== "") filterString = filterString + "&countrycode="+countrycode
        if (tag !== "") filterString = filterString + "&tag="+tag+"&tagExact="+tagExcact
        if (codec !== "") filterString = filterString + "&codec="+codec
        if (order !== "") filterString = filterString + "&order="+order
        if (order === "name") reverse = false; else reverse = true
        filterString = filterString + "&reverse="+reverse + "&offset="+offset+"&limit="+limit

        console.log(radioBrowser.serverUrl + "/xml/stations/search?hidebroken=true"+filterString)
        source = radioBrowser.serverUrl + "/xml/stations/search?hidebroken=true"+filterString
    }


    query: "/result/station"
    XmlRole { name: "stationuuid"; query: "@stationuuid/string()" }
    XmlRole { name: "name"; query: "@name/string()" }
    XmlRole { name: "homepage"; query: "@homepage/string()" }
    XmlRole { name: "favicon"; query: "@favicon/string()" }
    XmlRole { name: "tags"; query: "@tags/string()" }
    XmlRole { name: "codec"; query: "@codec/string()" }
    XmlRole { name: "bitrate"; query: "@bitrate/string()" }
    XmlRole { name: "hls"; query: "@hls/string()" }
    XmlRole { name: "url_resolved"; query: "@url_resolved/string()" }
    XmlRole { name: "country"; query: "@country/string()" }
    XmlRole { name: "countrycode"; query: "@countrycode/string()" }
    XmlRole { name: "votes"; query: "@votes/string()" }
    XmlRole { name: "clicktrend"; query: "@clicktrend/string()" }
    XmlRole { name: "clickcount"; query: "@clickcount/string()" }
    XmlRole { name: "lastcheckok"; query: "@lastcheckok/string()" }
    onStatusChanged: {
    if (status === XmlListModel.Ready) {running = false}
    //if (status === XmlListModel.Loading) running = true;
    if (status === XmlListModel.Error) running = false;
    if (status === XmlListModel.Null) running = false;
    }
}

