/*! phoscon-app 2023-07-31 */

export const getSwitchListView=c=>{const ItemView=Backbone.View.extend({tagName:"li",className:"list-group-item",template:_.template($("#switch-item-template").html()),initialize:function(){this.listenTo(this.model,"change",this.render),this.listenTo(this.model,"change",this.changeBtn),this.listenTo(this.model,"destroy",this.remove);var iconId=getDeviceIconId(this.model.attributes),html=this.template(this.model.attributes),d=app.devices.findWhere({mac:this.model.id}),btnMap=d?d.attributes:void 0;if(-1!==iconId.indexOf("generic"))this.el.innerHTML=html,getGenericSwIcon(btnMap).then(svg=>{svg&&this.el.replaceChild(svg,this.el.querySelector("svg"))}).catch(console.log);else{var sg=Snap(iconId)||Snap("#empty-frame-icon");html=html.replace("{icon}",sg.clone().outerSVG()),this.el.innerHTML=html}this.el.setAttribute("data-swid",this.model.id),this.el.setAttribute("data-btn-ev",c.Events.SW_PRESSED),this.itemName=this.el.querySelector(".item-name")},render:function(){let name=this.model.changed&&this.model.changed.name?this.model.changed.name:null;name&&this.itemName.innerText!==name&&(this.itemName.innerText=name)},changeBtn:function(){let change=this.model.changed||{},changeSize=_.size(change);if(change.buttonevent||1==changeSize&&change.lastupdated){if(7e3===this.model.get("buttonevent"))return;this.el.classList.add("btnevent-sw"),setTimeout(()=>{this.el.classList.remove("btnevent-sw")},1e3)}}}),ListView=Backbone.View.extend({el:"#switchNavList",initialize:function(){this.views=[],this.service=null,this.listenTo(app.resourcelinks,"add",this.render),app.resourcelinks.models.forEach(rl=>{this.render(rl)})},render:function(rl){let classid=rl.get("classid")||-1;if(classid<c.SW_CLASSID_MIN||classid>c.SW_CLASSID_MAX)return;let links=rl.get("links")||[],group=links.find(l=>-1!==l.indexOf("/groups/"));if(!group||group.split("/")[2]!==app.session.gid||"0"==group.split("/")[2])return;let sensors=links.filter(l=>-1!==l.indexOf("/sensors/"));(sensors=sensors.map(s=>s.split("/")[2])).forEach(sid=>{let sn=app.sensors.find(s=>s.get("id")===sid||s.get("uniqueid")===sid);if(!sn||-1!==sn.get("type").indexOf("CLIP"))return;let uniqueid=sn.get("uniqueid")||-1,sw=app.switches.find(x=>-1!==uniqueid.indexOf(x.get("id")));sw&&this.addSwitch(sw)})},events:{"click [data-btn-ev]:not(.disabled)":function(ev){var elem=ev.currentTarget,dataEv=elem.getAttribute("data-btn-ev"),id=null;elem.hasAttribute("data-swid")&&(id=elem.getAttribute("data-swid")),this.service.send({type:dataEv,id:id})}},addSwitch:function(model){let view=this.views.find(v=>v.model===model);view||(view=new ItemView({model:model}),this.el.appendChild(view.el),this.views.push(view))},setActiveSwitch:function(swid){this.views.forEach(v=>{let el=v.el;el.classList.remove("active-switch"),el.getAttribute("data-swid")==swid&&el.classList.add("active-switch")})},checkInitialActive:function(swid,service){if(this.service||(this.service=service),swid&&screenMin(SCREEN_MAX_WIDTH_PHONE_PORTRAIT)){let swid=this.views[0].el.getAttribute("data-swid");this.service.send({type:c.Events.SW_PRESSED,id:swid})}}});return new(Backbone.View.extend({el:"#navigation",template:_.template(document.getElementById("navigation-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.service=null,this.c=c,this.gid=app.session.gid;let group=app.groups.get(this.gid);group&&(this.el.querySelector(".group-name").innerText=group.get("name")),this.subView=new ListView,this.cBox=this.el.querySelector("#changedBox")},events:{"click [data-btn-ev]:not(.disabled):not(.list-group-item)":function(ev){var elem=ev.currentTarget,dataEv=elem.getAttribute("data-btn-ev");elem.classList.contains("btn-add-switch")?this.service.send({type:dataEv,page:"PAGE_SWITCHES"}):this.service.send(dataEv)}},showSaveDialog:function(){this.cBox.classList.add("active-box")},hideSaveDialog:function(){this.cBox.classList.remove("active-box")}}))};