/*! phoscon-app 2024-05-27 */

import{ButtonEvents}from"./state-constants.js";const WifiView=Backbone.View.extend({tagName:"tr",template:_.template($("#wifi-item-template").html()),initialize:function(){this.$el.html(this.template(this.model.attributes)),this.listenTo(this.model,"change",this.render),this.icon=this.el.querySelector("td:nth-child(3) svg use")},events:{click:function(){app.stateEventHandler(ButtonEvents.CONNECT_TO_CLIENT_PRESSED,{model:this.model})}},render:function(){if(!this.rAF)return this.rAF=requestAnimationFrame(()=>{this.rAF=null;let icon=null,rssi=this.model.get("rssi")||-90;icon=rssi<=-86?"#wifi-lock-0-icon-sm":rssi<=-76?"#wifi-lock-1-icon-sm":rssi<=-66?"#wifi-lock-2-icon-sm":"#wifi-lock-3-icon-sm",this.icon.getAttribute("xlink:href")!==icon&&this.icon.setAttribute("xlink:href",icon)}),this}});export const ClientWifiListHandlerView=Backbone.View.extend({initialize:function(){this.views=[],this.listenTo(app.wifiStations,"add",this.addOne),this.listenTo(app.wifiStations,"remove",this.removedOne),app.wifiStations.forEach(this.addOne,this),this.listenTo(app.wifi,"change",this.checkConnected),this.listenTo(app.wifiStations,"sync",this.checkConnected)},addOne:function(model){let view=this.views.find(x=>x.model===model);view||(document.body.classList.remove("empty"),view=new WifiView({model:model}),this.views.push(view),this.el.appendChild(view.render().el))},removedOne:function(model){let view=this.views.find(x=>x.model===model);this.views=this.views.filter(x=>x.model!==model),view&&view.remove(),0===this.views.length&&document.body.classList.add("empty")},checkConnected:function(){let active=!1;if("client"!==app.wifi.get("workingtype"));else{const ssid=app.wifi.get("workingname"),view=this.views.find(v=>v.model.get("ssid")===ssid);view&&(active=!0,this.el.firstChild!==view.el&&this.el.insertBefore(view.el,this.el.firstChild)),this.el.classList.toggle("active",active)}}});