/*! phoscon-app 2024-02-26 */

export const getView=constants=>{let tableView,emptyView,bottomView;const OverviewView=Backbone.View.extend({el:"#overview",template:_.template(document.getElementById("plug-overview-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.service=null,tableView=new TableView,emptyView=new EmptyView,bottomView=new BottomView},render:function(e){return this},events:{'click [data-btn-ev="SCAN"]':function(ev){this.service.send({type:constants.events.SCAN})},'click [data-btn-ev="STOP_SCAN"]':function(event){this.service.send({type:constants.events.SCAN,time:0})}},show:function(){this.el.classList.remove("hide")},hide:function(){this.el.classList.add("hide")}}),EmptyView=Backbone.View.extend({el:"#empty",template:_.template(document.getElementById("plug-empty-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.listenTo(this.collection,"add",this.addOne),this.listenTo(this.collection,"remove",this.removeOne)},show:function(){this.el.classList.remove("hide")},hide:function(){this.el.classList.add("hide")},addOne:function(){this.hide},events:{'click [data-btn-ev="SCAN"]':function(ev){this.service.send({type:constants.events.SCAN})},'click [data-btn-ev="STOP_SCAN"]':function(ev){this.service.send({type:constants.events.SCAN,time:0})}}}),BottomView=Backbone.View.extend({el:"#bottom",template:_.template(document.getElementById("search-plugs-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.searchText1=this.el.querySelector("#c_newLi_la"),this.emptyDiv=document.querySelector(".empty-div"),this.time=this.el.querySelector("#time"),this.timeLabels=this.time.querySelectorAll("span"),this.progressBar=this.el.querySelector("#progressBar"),this.listenTo(this.collection,"add",this.addOne),this.listenTo(this.collection,"remove",this.removeOne)},setSearchActive:function(){this.searchText1.textContent=translate("txt_searchPlugs"),document.body.classList.add("search-active"),this.emptyDiv.classList.add("search")},setSearchInactive:function(){document.body.classList.remove("search-active"),this.emptyDiv.classList.remove("search"),this.time.style.opacity=0},updateProgressBar:function(width){this.progressBar.style.width=width+"%"},setTimelabelOneMinute:function(){this.timeLabels[0].textContent=" ",this.timeLabels[1].textContent=translate("text_1min_remaining")},setTimelabelMinutesRemaining:function(min){this.time.classList.remove("hidden"),this.time.style.opacity=1,this.timeLabels[0].textContent=min+" ",this.timeLabels[1].textContent=translate("text_min_remaining")},setTimeLabelMinutes:function(min){this.timeLabels[0].textContent=min+" "},events:{'click [data-btn-ev="STOP_SCAN"]':function(ev){this.service.send({type:constants.events.SCAN,time:0})}}}),TableView=Backbone.View.extend({el:"#plugTable",collection:app.plugs,initialize:function(){this.listenTo(this.collection,"add",this.addOne),this.listenTo(this.collection,"remove",this.removeOne),this.views=[],this.service=null,this.collection.forEach(this.addOne,this)},addOne:function(model){var view=new TableRowView({model:model});this.$el.append(view.render().el),this.views.push(view)},removeOne:function(model){let views=this.views.filter(v=>v.model.get("id")!==model.get("id"));this.views=views,0!==this.views.length&&-1===window.location.hash.indexOf(model.get("id"))||app.router.navigate("table",{trigger:!0})}}),TableRowView=Backbone.View.extend({tagName:"tr",template:_.template($("#plug-item-template").html()),initialize:function(){this.model.on("change",this.render,this),this.model.on("destroy",this.remove,this),this.el.setAttribute("data-btn-ev","ROW_CLICKED"),this.el.innerHTML=this.template(this.model.attributes),this.nameLabel=this.el.querySelector("td:nth-child(2)"),this.vendorLabel=this.el.querySelector("td:nth-child(3)"),this.productLabel=this.el.querySelector("td:nth-child(4)"),this.listenTo(app.plugs.get(this.model.attributes.id),"change:name",this.renderName);let injectOptions=SVGInject.setOptions({useCache:!1,copyAttributes:!0,makeIdsUnique:!1}),iconImg=this.el.querySelector(".item-icon");SVGInject(iconImg,injectOptions),this.service=null,this.render()},events:{click:function(ev){const elem=ev.currentTarget;if(!elem.classList.contains("disabled")){var dataEv=elem.getAttribute("data-btn-ev");this.service.send({type:dataEv,mId:this.model.id})}}},render:function(){return this.model.get("state").reachable?this.el.classList.remove("not-reachable"):this.el.classList.add("not-reachable"),this.model.get("state").on?this.el.classList.remove("li-off"):this.el.classList.add("li-off"),this.nameLabel.textContent=this.model.attributes.name,this.vendorLabel.textContent=this.model.attributes.manufacturer||this.model.attributes.manufacturername,this.productLabel.textContent=this.model.attributes.modelid,this}});return{overviewView:new OverviewView,emptyView:emptyView,tableView:tableView,bottomView:bottomView}};