/*! phoscon-app 2025-05-20 */

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=[],this.addDisabledSwitchTxt()},show:function(){this.el.classList.add("active-view")},hide:function(){this.sensors.forEach(sn=>{this.stopListening(sn)}),this.el.classList.remove("active-view")},addDisabledSwitchTxt:function(){const switchInUseElement=document.querySelector(".switchInUse");switchInUseElement&&(switchInUseElement.style.display="none")},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 findSwitchBtnUsed=app.rules.models,swid=app.session.m_swid,collectSwitchNumber=[];findSwitchBtnUsed.forEach(fsb=>{fsb.attributes.conditions.forEach(condition=>{let switchAddress=condition.address,buttonValue=condition.value;if(-1===switchAddress.indexOf("/sensors/"))return;const startIndexSwitch=switchAddress.indexOf("/sensors/")+"/sensors/".length,endIndexSwitch=switchAddress.lastIndexOf("-01"),switchNumberUsed=switchAddress.substring(startIndexSwitch,endIndexSwitch),matchMacAddress=switchAddress.match(/\b(?:[\da-f]{2}(?::[\da-f]{2}){7}|(?:\d+))\b/i);if(matchMacAddress){const number=matchMacAddress[0];console.log("Extracted number:",number)}else console.log("Number not found in the string.");switchNumberUsed==swid&&void 0!==buttonValue&&collectSwitchNumber.push(buttonValue),matchMacAddress&&matchMacAddress[0]==swid&&void 0!==buttonValue&&collectSwitchNumber.push(buttonValue)})});let that=this;setTimeout(()=>{let usedSwitchButtons=[];document.querySelector(".switchInUse").style.display="none",collectSwitchNumber.forEach(switchNumber=>{const matchSwitchNumber=switchNumber.match(/\d+/),btnNumber=matchSwitchNumber?matchSwitchNumber[0]:null;let interactiveSwitchbuttons=this.$el.find("[data-buttonEvent]");Object.entries(interactiveSwitchbuttons).forEach(key=>{let button=key[1];if(button.dataset&&button.dataset.buttonEvent&&-1!==button.dataset.buttonEvent.indexOf(btnNumber)){let selectorSwButton="";selectorSwButton='[data-buttonEvent="'+button.dataset.buttonEvent+'"]',button.classList.add("disabled"),usedSwitchButtons.push(button.dataset.buttonEvent)}})}),localStorage.setItem("app.usedSwitchButtons",JSON.stringify(usedSwitchButtons));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)});