// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/_base/declare dojo/_base/array dojo/_base/lang dojo/_base/Color dojo/dom dojo/dom-class dojo/dom-construct dojo/dom-style dojo/number dojo/on dojo/has dijit/form/Button jimu/dijit/Popup jimu/CSVUtils jimu/utils esri/config esri/geometry/geometryEngine esri/geometry/mathUtils esri/geometry/Point esri/geometry/webMercatorUtils esri/graphic esri/layers/FeatureLayer esri/symbols/SimpleMarkerSymbol esri/symbols/SimpleLineSymbol esri/symbols/Font esri/symbols/TextSymbol esri/tasks/query".split(" "),
function(w,f,g,B,l,h,m,C,x,p,D,E,F,y,z,G,r,H,I,J,K,A,L,M,N,O,t){return w("SummaryInfo",null,{summaryLayer:null,summaryFields:[],summaryIds:[],summaryFeatures:[],constructor:function(b,a,c){this.tab=b;this.container=a;this.parent=c;this.config=c.config;this.buffer=this.incident=this.summaryLayer=null},updateForIncident:function(b,a){this.container.innerHTML="";h.add(this.container,"loading");this.incident=b;this.buffer=a;this.summaryIds=[];this.summaryFeatures=[];if(this.summaryLayer)this._queryFeatures(a.geometry);
else{var c=new A(this.tab.tabLayers[0].url);p(c,"load",g.hitch(this,function(){c.capabilities&&-1<c.capabilities.indexOf("Query")?(this.summaryLayer=c,this.summaryFields=this._getFields(this.summaryLayer),this._queryFeatures(a.geometry)):this._processError()}));p(this.parent.opLayers,"layerInfosFilterChanged",g.hitch(this,this._layerFilterChanged))}},_layerFilterChanged:function(b){if(null!==this.summaryLayer&&null!==this.incident&&null!==this.buffer){var a=this.tab.tabLayers[0].id;f.forEach(b,g.hitch(this,
function(c){a===c.id&&this.updateForIncident(this.incident,this.buffer)}))}},_queryFeatures:function(b){var a=this.tab.tabLayers[0].id,c="";this.parent.opLayers.traversal(function(e){if(a===e.id&&e.getFilter())return c=e.getFilter(),!0});var d=new t;d.geometry=b;d.where=c;this.summaryLayer.queryIds(d,g.hitch(this,function(e){e?(this.summaryIds=e,0<this.summaryIds.length?this._queryFeaturesByIds():this._processResults()):this._processResults()}))},_queryFeaturesByIds:function(){var b=this.summaryLayer.maxRecordCount||
1E3,a=this.summaryIds.slice(0,b);this.summaryIds.splice(0,b);b=new t;var c=!1;f.some(this.summaryFields,function(e){if("area"===e.type||"length"===e.type)return c=!0});b.returnGeometry=c;var d=[];f.forEach(this.summaryFields,function(e){d.push(e.field)});b.outFields=d;b.objectIds=a;this.summaryLayer.queryFeatures(b,g.hitch(this,function(e){this.summaryFeatures=this.summaryFeatures.concat(e.features);this._processResults();0<this.summaryIds.length?(l.byId("IMT_download")&&h.replace(l.byId("IMT_download"),
"processing","download"),setTimeout(g.hitch(this,this._queryFeaturesByIds),900)):l.byId("IMT_download")&&h.replace(l.byId("IMT_download"),"download","processing")}))},_prepResults:function(){for(var b=0;b<this.summaryFields.length;b++){var a=this.summaryFields[b],c=a.field,d=a.value;switch(a.type){case "count":d=this.summaryFeatures.length;break;case "area":d=this._getArea();break;case "length":d=this._getLength();break;case "sum":d=this._getSum(c);break;case "avg":d=this._getSum(c)/this.summaryFeatures.length;
break;case "min":d=this._getMin(c);break;case "max":d=this._getMax(c)}a.value=d}},_sortResults:function(b){return function(a,c){return a.attributes[b]<c.attributes[b]?-1:a.attributes[b]>c.attributes[b]?1:0}},_getSum:function(b){var a=0;f.forEach(this.summaryFeatures,function(c){a+=c.attributes[b]});return a},_getMin:function(b){this.summaryFeatures.sort(this._sortResults(b));return this.summaryFeatures[0].attributes[b]},_getMax:function(b){this.summaryFeatures.sort(this._sortResults(b));this.summaryFeatures.reverse();
return this.summaryFeatures[0].attributes[b]},_getArea:function(){var b=0,a=g.clone(this.config.distanceSettings);a.miles=109413;a.kilometers=109414;a.feet=109405;a.meters=109404;a.yards=109442;a.nauticalMiles=109409;var c=a[this.config.distanceUnits];f.forEach(this.summaryFeatures,function(d){b+=r.geodesicArea(d.geometry,c)});return b},_getLength:function(){var b=0,a=this.config.distanceSettings[this.config.distanceUnits];f.forEach(this.summaryFeatures,function(c){b+=r.geodesicLength(c.geometry,
a)});return b},_processError:function(){this.container.innerHTML="";h.remove(this.container,"loading");this.container.innerHTML=this.parent.nls.noFeaturesFound},_processResults:function(){this._prepResults();this.container.innerHTML="";h.remove(this.container,"loading");var b=this.summaryFields,a=0,c=m.create("div",{id:"tpc",style:"width:"+220*(b.length+1)+"px;"},this.container);h.add(c,"IMT_tabPanelContent");var d=m.create("div",{},c);h.add(d,"IMTcol");d=m.create("div",{id:"IMT_download",innerHTML:this.parent.nls.downloadCSV},
d);h.add(d,["btnExport","download"]);p(d,"click",g.hitch(this,this._exportToCSV));for(d=0;d<b.length;d++){a=b[d];var e=z.sanitizeHTML(a.alias?a.alias:"")+"\x3cbr/\x3e";a=a.alias===this.parent.nls.area||a.alias===this.parent.nls.length?a.value:Math.round(a.value);isNaN(a)&&(a=0);e+="\x3cdiv class\x3d'colSummary'\x3e"+x.format(a)+"\x3c/div\x3e\x3cbr/\x3e";e=m.create("div",{id:"Demographics_"+d,innerHTML:e},c);h.add(e,"IMTcol")}},_exportToCSV:function(){if(0===this.summaryFeatures.length)return!1;console.log(this.tab);
var b=this.tab.layers,a=[],c=[];f.forEach(this.summaryFeatures,function(e){a.push(e.attributes)});for(var d in a[0])c.push(d);y.exportCSV(b,a,c)},_getFields:function(b){var a=[];if(this.tab.advConfig&&this.tab.advConfig.fields&&0<this.tab.advConfig.fields.length)return f.forEach(this.tab.advConfig.fields,g.hitch(this,function(k){var u="";"count"!==k.type&&"area"!==k.type&&"length"!==k.type&&(u=" (\x3cspan style\x3d'font-size:7pt;'\x3e"+this.parent.nls[k.type]+"\x3c/span\x3e)");a.push({field:k.expression,
alias:k.label+u,type:k.type,value:0})})),a;var c=b.infoTemplate?b.infoTemplate.info.fieldInfos:b.fields;for(var d=0;d<c.length;d++){var e=c[d],v=e.fieldName||e.name,q=this._getFieldType(b.fields,v),n=null;v===b.objectIdField||"esriFieldTypeDouble"!==q&&"esriFieldTypeInteger"!==q&&"esriFieldTypeSmallInteger"!==q||("undefined"!==typeof e.visible?e.visible&&(n={field:e.fieldName,alias:e.label,type:"sum",value:0}):n={field:e.name,alias:e.alias,type:"sum",value:0},n&&a.push(n))}return a},_getFieldType:function(b,
a){var c;f.some(b,function(d){if(d.name===a)return c=d.type,!0});return c}})});