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

function initLightControl(){app.sendQueue=[],app.allowSend=!0,app.$groupControlToggle=$("#grp-toggle:checkbox"),app.$groupControlToggle.change(function(){setGroupControl(this.checked)}),$(".hdr-toggle").prev().on("click",function(e){setGroupControl("btn-group-control"===e.currentTarget.id)}),$(".hdr-toggle").next().on("click",function(e){setGroupControl("btn-group-control"===e.currentTarget.id)}),app.on("active-item-changed",function(e){e.model&&e.model.collection!==app.scenes&&(e.model.collection===app.lights?app.$groupControlToggle.prop("checked",!1):app.$groupControlToggle.prop("checked",!0))})}function setGroupControl(active){app.$groupControlToggle.prop("checked",active),active?($(".main").addClass("group-control"),app.lightListView.setActive(void 0),setTimeout(()=>{updateSliders()},100)):(app.lightListView.setLastActive(),$(".main").removeClass("group-control"))}function processSending(){if(app.sendBusy)return;if(0===app.sendQueue.length)return;let queueItem=app.sendQueue.shift();app.sendBusy=!0,sendQueuedControlState(queueItem).catch(err=>console.log(err)).then(()=>{app.sendBusy=!1,setTimeout(()=>processSending(),100)})}function queueSendState(model,state){let queue=[],now=Date.now(),stateCopy=JSON.parse(JSON.stringify(state));if(model.collection===app.groups){let g1=model,g2=model;if((model.get("lights")||[]).map(x=>app.lights.get(x)).filter(x=>x).find(x=>"FLS-PP3"===x.get("modelid"))){let hexgid=parseInt(model.id).toString(16).padStart(4,"0"),uniqueid1="10:00:"+(hexgid=hexgid.substr(0,2)+":"+hexgid.substr(2,2)),uniqueid2="20:00:"+hexgid;g1=app.groups.find(x=>x.get("uniqueid")===uniqueid1)||model,g2=app.groups.find(x=>x.get("uniqueid")===uniqueid2)||model}"white"in state?(stateCopy.bri=stateCopy.white,delete stateCopy.white,queue.push({id:g2.id,url:"/groups/"+g2.id+"/action",data:stateCopy,t:now})):queue.push({id:g1.id,url:"/groups/"+g1.id+"/action",data:stateCopy,t:now})}else model.collection===app.lights&&("white"in state&&isFlsPpWhite(model)&&(stateCopy.bri=stateCopy.white,delete stateCopy.white),queue.push({id:model.id,url:"/lights/"+model.id+"/state",data:stateCopy,t:now}));queue.forEach(x=>app.sendQueue.push(x)),processSending()}function sendQueuedControlState(queueItem){return new Promise((resolve,reject)=>{app.lastSend=Date.now(),$.ajax({url:"//"+app.session.gw+"/api/"+app.apikey+queueItem.url,dataType:"json",type:"PUT",cache:!1,data:JSON.stringify(queueItem.data),contentType:"application/json; charset=utf-8",headers:{Accept:apiversion},success:function(json){resolve(json)},error:function(jqXHR,textStatus,errorThrown){reject(jqXHR.status),console.log("error put state of "+queueItem.url+": "+errorThrown)}})})}function sendControlState(model,state){return new Promise((resolve,reject)=>{let resource,suffix;if(model.collection===app.groups)resource="groups",suffix="action";else{if(model.collection!==app.lights)throw"unsupported resource";resource="lights",suffix="state"}$.ajax({url:"//"+app.session.gw+"/api/"+app.apikey+"/"+resource+"/"+model.id+"/"+suffix,dataType:"json",type:"PUT",cache:!1,data:JSON.stringify(state),contentType:"application/json; charset=utf-8",headers:{Accept:apiversion},success:function(json){resolve(json)},error:function(jqXHR,textStatus,errorThrown){reject(jqXHR.status),console.log("error put state of "+resource+"/"+model.id+": "+errorThrown)}})})}function isGroupControlActive(){return!(!app.session.gid||app.session.lid)}function isLightControlActive(id){return!(app.currentSite!==GROUP_LIGHTS&&app.currentSite!==SCENE_LIGHTS||!app.session.lid)}function allOff(){$.ajax({url:"//"+app.session.gw+"/api/"+app.apikey+"/groups/"+app.alloffgroup.id+"/action",dataType:"json",type:"PUT",cache:!1,data:JSON.stringify({on:!1}),contentType:"application/json; charset=utf-8",headers:{Accept:apiversion},success:function(json){}})}