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

export const getResourceListView=c=>{const ItemView=Backbone.View.extend({tagName:"a",className:"resourcelink hidden list-group-item",template:_.template($("#resourcelinks-item-template").html()),initialize:function(){let rl=app.resourcelinks.get(this.model.get("rlid"));this.listenTo(rl,"change",this.render),this.listenTo(rl,"destroy",this.remove);var iconId=this.model.get("iconId"),html=this.template(this.model.attributes),d=app.devices.findWhere({mac:this.model.get("swid")}),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"))});else{var sg=Snap(iconId)||Snap("#empty-frame-icon");html=html.replace("{icon}",sg.clone().outerSVG()),this.el.innerHTML=html}this.el.setAttribute("data-rlid",this.model.get("rlid")),this.el.setAttribute("data-btn",this.model.get("btn")),this.el.setAttribute("data-swid",this.model.get("swid"))},render:function(){return this}}),ListView=Backbone.View.extend({el:"#activeSwRlList",initialize:function(){this.views=[],this.service=null,this.listenTo(app.resourcelinks,"add",this.render),app.resourcelinks.models.forEach(rl=>{let group=rl.get("links").find(r=>-1!==r.indexOf("group"));group.split("/")[2]!==app.session.gid&&0!=group.split("/")[2]||this.render(rl)})},events:{"click .list-group-item":function(ev){let rlid=ev.currentTarget.getAttribute("data-rlid");this.service.send({type:c.Events.RL_PRESSED,id:rlid})}},render:function(rl){let classid=rl.get("classid")||-1;if(classid<c.SW_CLASSID_MIN||classid>c.SW_CLASSID_MAX)return;if(classid==c.CLASSIDBRIDIMUP||classid==c.CLASSIDBRIDIMDOWN)return;let clObj=c.ClassIds.find(cl=>-1!==cl.options.indexOf(classid));if(!clObj)return;let model={rlid:rl.get("id"),swid:null,iconId:null,name:translate(clObj.translateId),info:translate(rl.get("name")),btn:null},cond=rl.get("links").filter(r=>-1!==r.indexOf("rule")).map(r=>r.split("/")[2]).map(rid=>app.rules.get(rid)).find(r=>r.get("conditions").find(c=>-1!==c.address.indexOf("buttonevent"))).get("conditions").find(c=>-1!==c.address.indexOf("buttonevent")),uId=cond.address.split("/")[2],sw=app.sensors.findWhere({uniqueid:uId});model.iconId=getDeviceIconId(sw.attributes),model.swid=sw.get("mac"),model.btn=(cond.value/1e3).toFixed(0)+"000";var ruleModel=new Backbone.Model(model);this.addRule(ruleModel)},addRule: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))},showResourcelinks:function(swid){this.views.forEach(view=>{view.el.getAttribute("data-swid")==swid?view.el.classList.remove("hidden"):view.el.classList.add("hidden")})},setActiveResource:function(rlid){this.views.forEach(v=>{let el=v.el;el.classList.remove("active-resource"),el.getAttribute("data-rlid")==rlid&&(el.classList.add("active-resource"),el.scrollIntoView())})},hideActiveResource:function(){this.views.forEach(v=>{v.el.classList.remove("active-resource")})}});return new(Backbone.View.extend({el:"#resourcelinks",template:_.template(document.getElementById("resourcelink-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.service=null,this.c=c,this.elHead=this.el.querySelector(".resourcelinks-head"),this.subView=new ListView,this.swModel=null},events:{"click [data-btn-ev]:not(.disabled)":function(ev){var dataEv=ev.currentTarget.getAttribute("data-btn-ev");this.service.send(dataEv)}},setActiveSwitch:function(swid){let sw=app.switches.get(swid);this.elHead.querySelector(".item-name").innerText=sw.get("name"),this.stopListening(this.swModel),this.swModel=sw,this.listenTo(this.swModel,"change",this.swChanged);var iconId=getDeviceIconId(sw.attributes),d=app.devices.findWhere({mac:sw.id}),btnMap=d?d.attributes:void 0;if(-1!==iconId.indexOf("generic"))getGenericSwIcon(btnMap).then(svg=>{svg&&this.elHead.replaceChild(svg,this.elHead.querySelector("svg"))});else{var sg=Snap(iconId)||Snap("#empty-frame-icon");this.elHead.querySelector("svg").innerHTML=sg.clone().outerSVG()}},swChanged:function(){let el=this.elHead.querySelector(".item-name"),name=this.swModel.changed&&this.swModel.changed.name?this.swModel.changed.name:null;name&&el.innerText!==name&&(el.innerText=this.swModel.get("name"))}}))};