/*! phoscon-app 2024-08-14 */

import{checkGateway,checkAppType}from"./helper-functions.js";export var initOverviewView=injectSvg=>{const EmptyView=Backbone.View.extend({el:"#empty",template:_.template(document.getElementById("overview-empty-template").innerHTML),initialize:function(){this.el.innerHTML=this.template()},show:function(){this.el.classList.add("active-view")},hide:function(){this.el.classList.remove("active-view")}}),ListView=Backbone.View.extend({el:"#gwList",collection:app.gateways,initialize:function(){this.views=[],this.listenTo(this.collection,"add",this.addOne),this.listenTo(this.collection,"remove",this.removeOne),this.collection.forEach(this.addOne,this)},show:function(){this.el.classList.add("active-view")},hide:function(){this.el.classList.remove("active-view")},addOne:function(model){var view=new ListElemView({model:model});this.$el.prepend(view.render().el),this.views.push(view),1===this.views.length&&this.service.send("OVERVIEW_LIST")},removeOne:function(model){let views=this.views.filter(v=>v.model.get("id")!==model.get("id"));this.views=views}}),ListElemView=Backbone.View.extend({tagName:"div",className:"gw-list-item",template:_.template($("#login-overview-gwlist-item-template").html()),initialize:function(){this.model.on("change",this.render,this),this.model.on("destroy",this.remove,this);var html=this.template(this.model.attributes);this.$el.html(html),this.$el.attr("data-gwid",this.model.id),this.$el.attr("data-btn-ev","GW_PRESSED"),this.gwName=this.el.querySelector(".gw-name"),this.gwIp=this.el.querySelector(".gw-ip"),this.gw=app.gateways.get(this.model.id),this.connectedIcon=this.el.querySelector(".connected-svg");let images=this.el.querySelectorAll("img[data-svgInject='injectLoginSvg']")||[];images.forEach(img=>{let name=this.model.get("devicename"),src=null;"ConBee"===name?src="img/generals/conbee.svg":"ConBee II"===name?src="img/generals/conbee2-lg.svg":"ConBee III"===name&&(src="img/generals/conbee3-lg.png"),src&&img.setAttribute("src",src)}),injectSvg(images).catch(err=>{console.log(err)}),checkGateway(this.gw),this.render()},events:{},render:function(){return this.model.get("reachable")?(this.el.classList.remove("not-reachable"),this.el.classList.remove("disabled")):(this.el.classList.add("not-reachable"),this.el.classList.add("disabled")),this.model.get("apikey")?this.connectedIcon.classList.add("connected"):this.connectedIcon.classList.remove("connected"),this.gwName.innerHTML!==this.model.get("name")&&(this.gwName.innerHTML=this.model.get("name")),this.gwIp.innerHTML!==this.model.get("ip")&&(this.gwIp.innerHTML=this.model.get("ip")),this}});return{Overview:Backbone.View.extend({el:"#overview",template:_.template(document.getElementById("overview-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.progressbar=this.el.querySelector(".search-progress"),this.gwHelpText=this.el.querySelector("#gw-help-infos"),this.phosconLogo=this.el.querySelector("#overviewHead svg"),this.gwHelpBtn=this.el.querySelector("#gw-help"),app.dbgInfo=this.el.querySelector("#dbgInfo"),app.progressBar=this.progressbar.querySelector("#progressBar"),this.service=null;let images=this.el.querySelectorAll("img[data-svgInject='injectLoginSvg']")||[];injectSvg(images).catch(err=>{console.log(err)})},events:{"click [data-btn-ev]:not(.disabled)":function(ev){let sendEv=ev.currentTarget.getAttribute("data-btn-ev"),gwid=ev.currentTarget.getAttribute("data-gwid")||void 0;this.service&&sendEv&&(gwid?this.service.send({type:sendEv,gwid:gwid}):this.service.send(sendEv))},"click #gw-help":function(ev){this.gwHelpText.classList.toggle("hidden")}},show:function(){this.el.classList.add("active-view"),"APP_SMARTPHONE"===checkAppType()&&this.gwHelpBtn.classList.add("hidden")},hide:function(){this.el.classList.remove("active-view")},showProgress:function(){this.progressbar.classList.add("active-search")},hideProgress:function(){this.progressbar.classList.remove("active-search")}}),ListView:ListView,EmptyView:EmptyView}};