/*! phoscon-app 2024-10-07 */

export const getButtonPageView=c=>Backbone.View.extend({el:"#pageButtons",template:_.template(document.getElementById("pageButton-template").innerHTML),initialize:function(c){this.c=c,this.hf=this.c.helperFuncs,this.el.innerHTML=this.template(),this.service=null,this.sensors=[]},show:function(){this.el.classList.add("active-view")},hide:function(){this.sensors.forEach(sn=>{this.stopListening(sn)}),this.el.classList.remove("active-view")},setSwitch:function(sw){let el=this.el.querySelector(".interactive");if(!(sw=(sw=app.switches.where({uniqueid:sw.get("uniqueid")}))[0]))return;let mac=sw.get("uniqueid").split("-")[0];this.sensors=app.sensors.filter(sn=>sn.get("mac")==mac),this.sensors.forEach(sn=>{this.listenTo(sn,"change",this.swChanged)}),this.hf.setImage(el,app.session.m_swid,this.c);let that=this;setTimeout(()=>{let elem=that.el.querySelector(".interactive"),svgId=that.c.helperFuncs.getSvgId(elem);var targets=targets=svgId?elem.querySelectorAll(svgId+" .sw-target")||[]:elem.querySelectorAll(".generated-sw-svg.interactive .sw-target")||[];targets.forEach(t=>{t.style.display="block"})},500)},events:{"click .sw-target":function(ev){let el=ev.currentTarget,dataBtn=el.getAttribute("data-buttonEvent");"undefined"!==dataBtn&&((this.el.querySelectorAll(".interactive .sw-target")||[]).forEach(t=>{t.classList.remove("sw-active-target")}),el.classList.add("sw-active-target"),this.service.send({type:"SW_TARGET_PRESSED",dataBtn:dataBtn}))}},swChanged:_.debounce(function(model){let btn=-1!==model.get("modelid").indexOf("cube")?model.get("state").gesture:model.get("state").buttonevent,el=this.el.querySelector(".interactive"),svgId=this.c.helperFuncs.getSvgId(el);var targets=targets=svgId?el.querySelectorAll(svgId+" .sw-target")||[]:el.querySelectorAll(".generated-sw-svg.interactive .sw-target")||[];let dataBtn=null;targets.forEach(t=>{let tBtn=t.getAttribute("data-buttonEvent");-1!==tBtn.indexOf(btn)?(t.style.display="block",t.classList.add("sw-active-target"),dataBtn=tBtn):(t.style.display="none",t.classList.remove("sw-active-target"))}),this.service.send({type:"SW_TARGET_PRESSED",dataBtn:dataBtn})},300)});