import QtQuick 2.0
import QtQuick.XmlListModel 2.0

Item {
    property string filter: ""
    property bool clear: false
    property bool running: false
    property alias tagsListModel: tagModel
    property int maxCount: 0
//    property alias tagsListModel: xmlModel
    property alias source: xmlModel.source

    onClearChanged: if (clear===true) {xmlModel.xml="";clear=false}

    ListModel {id: tagModel}

    XmlListModel {
        id: xmlModel
        query: "/result/tag"
        XmlRole { name: "tag"; query: "@name/string()" }
        XmlRole { name: "stationcount"; query: "@stationcount/string()" }
        onStatusChanged: {
            if (status === XmlListModel.Ready) {
                addtomodel()
              //  console.log("TAGSLISTMODEL DONE: "+xmlModel.count)
                tagCount = xmlModel.count
            }
        }
    }

    function addtomodel(){
        tagModel.clear()
        var cname
        var count
        var filt
        for(var i = 0; i < xmlModel.count; i++) {
            cname = xmlModel.get(i).tag
            count = xmlModel.get(i).stationcount
            if (count > 99) {
                if (count > maxCount) maxCount = count
                filt = cname.toLowerCase(); if (filt.indexOf(filter) !== -1) tagModel.append({"tag" : cname, "stationcount" : xmlModel.get(i).stationcount})
            }
            //  cname = getCountryName(xmlModel.get(i).name)
            //  if (cname) {filt = cname.toLowerCase(); if (filt.indexOf(filter) !== -1) countryModel.append({"name" : cname, "alpha_2" : xmlModel.get(i).name,"stationcount" : xmlModel.get(i).stationcount})}
        }
        //countryModel.listModelSort(countryModel, compareElements)
        //numberOfCountries = i
        running = false
    }
}
