import QtQuick 2.0
import QtQuick.XmlListModel 2.0

XmlListModel {
    property bool finished: true
    property bool clear: false
    property ListModel stationsModel: ListModel {id: stationsModel}

 //   onFinishedChanged: console.log("*** FINISHED: "+finished+" ***")

    //onClearChanged: if (clear) {stationsModel.clear()}

    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) {addToModel()}
    if (status === XmlListModel.Loading) finished = false;
    if (status === XmlListModel.Error) finished = true
    if (status === XmlListModel.Null) finished = true
    }

    function addToModel() {
        for(var i = 0; i < count; i++) {
            stationsModel.append({
                                     "stationuuid" : get(i).stationuuid,
                                     "name" : get(i).name,
                                     "homepage" : get(i).homepage,
                                     "favicon" : get(i).favicon,
                                     "tags" : get(i).tags,
                                     "codec" : get(i).codec,
                                     "bitrate" : get(i).bitrate,
                                     "hls" : get(i).hls,
                                     "url_resolved" : get(i).url_resolved,
                                     "country" : get(i).country,
                                     "countrycode" : get(i).countrycode,
                                     "votes" : get(i).votes,
                                     "clicktrend" : get(i).clicktrend,
                                     "clickcount" : get(i).clickcount,
                                     "lastcheckok" : get(i).lastcheckok
                                 })
        }
        finished = true
    //    console.log(" ***** APPENDED ******")
    }
}