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

export function initDetailsView(injectSwitchWizzardSvg){return Backbone.View.extend({el:"#details",template:_.template(document.getElementById("blind-details-template").innerHTML),initialize:function(model){this.model=model,this.views=[],app.session.blid=this.model.id,this.listenTo(this.model,"change",this.render),this.el.innerHTML=this.template(model),this.nameLabel=this.el.querySelector("h1 span"),this.vendorLabel=this.el.querySelector(".vendor"),this.productLabel=this.el.querySelector(".product"),this.stateLabel=this.el.querySelector(".state .bri:nth-child(2)"),this.batteryLabel=this.el.querySelector(".state .battery:nth-child(4)"),this.dragBar=this.el.querySelector(".range-slider .bar"),setTimeout(()=>{dragElement(this.dragBar,this.model)},200);let iconImg=this.el.querySelector(".main-head .svg-icon");injectSwitchWizzardSvg([iconImg]),this.render(),this.show(),loadLanguage()},events:{"click [data-btn-ev]":function(e){let el=e.currentTarget;if(!el)return;let ev=el.getAttribute("data-btn-ev");this.service.send(ev)}},render:function(){let bat=this.model.get("state").battery,lift=this.model.get("state").lift;return bat&&this.el.classList.add("battery"),lift&&this.el.classList.add("bri"),setPosByLift(this.dragBar,this.model),this.model.get("name")!=this.nameLabel.innerHTML&&(this.nameLabel.innerHTML=this.model.get("name")),lift!=this.stateLabel.innerHTML&&(this.stateLabel.innerHTML=lift+" %"),bat!=this.batteryLabel.innerHTML&&(this.batteryLabel.innerHTML=bat+" %"),this},show:function(){this.el.classList.remove("hide")},hide:function(){this.el.classList.add("hide"),this.stopListening(this.model),this.undelegateEvents()}})};function dragElement(elmnt,model){var events=is_touch_enabled()?["touchstart","touchmove","touchend"]:["mousedown","mousemove","mouseup"],parent=elmnt.parentNode;if(parent&&elmnt){var con=parent.querySelector(".slider-container"),info=parent.querySelector(".touch-info"),infotxt=info.querySelector(".touch-infotext"),pos2=0,pos4=0;elmnt.addEventListener(events[0],function(e){(e=e||window.event).preventDefault();let cY=e.touches?e.touches[0].clientY:e.clientY;pos4=cY,document.addEventListener(events[2],closeDragElement,{passive:!1}),document.addEventListener(events[1],elementDrag,{passive:!1}),infotxt.innerHTML=model.get("state").lift+" %",info.classList.add("show-info")},{passive:!1})}function elementDrag(e){(e=e||window.event).preventDefault();let pOffHeight=parent.offsetHeight-elmnt.offsetHeight,cY=e.touches?e.touches[0].clientY:e.clientY;pos2=pos4-cY,pos4=cY;var newPos=elmnt.offsetTop-pos2;newPos<0&&(newPos=0),newPos>pOffHeight&&(newPos=pOffHeight),elmnt.style.top=newPos+"px",info.style.top=newPos+"px",con.style.height=newPos+elmnt.offsetHeight+"px";var posN=con.offsetHeight/parent.offsetHeight,lift=posN<.25?0:100*posN;lift=Math.round(lift),infotxt.innerHTML=lift+" %"}function closeDragElement(){var posN,lift;document.removeEventListener(events[2],closeDragElement),document.removeEventListener(events[1],elementDrag),info.classList.remove("show-info"),posN=con.offsetHeight/parent.offsetHeight,lift=posN<.25?0:100*posN,lift=Math.round(lift),console.log("lift: "+lift),console.log("posN: "+posN),updateBlindHeight(lift=lift>0?lift:1)}}const setPosByLift=_.debounce((elmnt,model)=>{var lift=model.get("state").lift,parent=elmnt.parentNode,con=parent.querySelector(".slider-container"),info=parent.querySelector(".touch-info"),pos=(parent.offsetHeight-elmnt.offsetHeight)/100*lift;info.style.top=pos+"px",elmnt.style.top=pos+"px",con.style.height=pos+elmnt.offsetHeight+"px"},200);function updateBlindHeight(val){if(!val)return;const xhr=new XMLHttpRequest,url="//"+app.session.gw+"/api/"+app.apikey+"/lights/"+app.session.blid+"/state";xhr.open("PUT",url,!0),xhr.setRequestHeader("Content-type","application/json; charset=utf-8"),xhr.setRequestHeader("Accept",app.apiversion),xhr.send(JSON.stringify({lift:val})),xhr.onreadystatechange=(e=>{if((4!==xhr.readyState||200!==xhr.status)&&4===xhr.readyState&&200!==xhr.status)throw new Error("slide blind request: "+xhr.status)})}function is_touch_enabled(){return"ontouchstart"in window||navigator.maxTouchPoints>0||navigator.msMaxTouchPoints>0}