const defaultScheds=[{name:Loc('No Change'),id:-1},{name:Loc('Unarmed 24/7'),id:0},{name:Loc('Armed 24/7'),id:1},{name:Loc('Armed Sunrise To Sunset'),id:2},{name:Loc('Armed Sunset To Sunrise'),id:3}];
class SchedulePresetPicker{
constructor(name,json,callback){
let containerDiv=AddElement(document.body,'div');
containerDiv.className='panelContainer';
let contentDiv=AddElement(containerDiv,'div');
contentDiv.className='panelContent';
let bodyDiv=AddElement(contentDiv,'div');
bodyDiv.className='panelBody';
{
let table=AddElement(AddElement(bodyDiv,'div'),'table');
table.className='setBody';
let tbody=AddElement(table,'tbody');
let tr=AddElement(tbody,'tr');
AddElement(AddElement(tr,'td'),'span').textContent=Loc('Name:');
this.nameInput=AddElement(AddElement(tr,'td'),'input');
this.nameInput.type='text';
this.nameInput.id='nameInput';
this.nameInput.className='widthF';
this.nameInput.value=name?name:Loc('Untitled Preset');
tr=AddElement(tbody,'tr');
AddElement(AddElement(tr,'td'),'span').textContent=Loc('Home trigger:');
let td=AddElement(tr,'td');
this.homeSel=document.getElementById('homeSel').cloneNode(true);
this.homeSel.id='homeSelClone';
this.homeSel.style='';
td.appendChild(this.homeSel);
if(IsLocalhost()){
let button=document.getElementById('configureHome').cloneNode(true);
button.id='';
button.style='';
td.appendChild(button);
}
tr=AddElement(tbody,'tr');
tr.className='paddingL';
AddElement(AddElement(tr,'td'),'span').textContent=Loc('Overrides:');
td=AddElement(tr,'td');
AddElement(td,'label').innerHTML='<input type="checkbox" id="clearCheck">'+Loc('Clear schedule overrides when applying this preset');
}
{
let table=AddElement(AddElement(bodyDiv,'div'),'table');
table.id='presetTable';
let tbody=AddElement(table,'tbody');
let tr=AddElement(tbody,'tr');
AddElement(tr,'th').textContent=Loc('Camera');
AddElement(tr,'th').textContent=Loc('Continuous Capture');
AddElement(tr,'th').textContent=Loc('Motion Capture');
AddElement(tr,'th').textContent=Loc('Actions');
for(let cam of _cams){
if(cam.demo)continue;
tr=AddElement(tbody,'tr');
AddElement(tr,'td').textContent=cam.name;
this.addSelect(AddElement(tr,'td'),cam.num+'.cc');
this.addSelect(AddElement(tr,'td'),cam.num+'.mc');
this.addSelect(AddElement(tr,'td'),cam.num+'.a');
}
}
{
let footerDiv=AddElement(contentDiv,'div');
footerDiv.className='panelFooter';
let button=AddElement(footerDiv,'button');
button.className='regButton';
button.textContent=Loc('Cancel');
button.onclick=()=>{
containerDiv.remove();
};
button=AddElement(footerDiv,'button');
button.className='regButton';
button.textContent=Loc('Save');
button.onclick=()=>{
callback(this.nameInput.value?this.nameInput.value:Loc('Untitled Preset'),this.json());
containerDiv.remove();
};
}
this.setJson(json);
containerDiv.style.display='flex';
}
addSelect(parent,id){
let sel=AddElement(parent,'select');
sel.className='sps';
sel.id=id;
for(let s of defaultScheds){
let opt=AddElement(sel,'option');
opt.textContent=s.name;
opt.value=s.id;
}
let schedSel=document.getElementById('schedules');
if(schedSel){
for(let s of schedSel.options){
let opt=AddElement(sel,'option');
opt.textContent=s.textContent;
opt.value=s.dataset.id;
}
}
}
setJson(json){
let d=JSON.parse(json);
if(d.which)for(let w of d.which){
let cc=document.getElementById(w[0]+'.cc');
let mc=document.getElementById(w[0]+'.mc');
let a=document.getElementById(w[0]+'.a');
if(cc)cc.value=w[1];
if(mc)mc.value=w[2];
if(a)a.value=w[3];
}
if(d.clearOverrides!=null)document.getElementById('clearCheck').checked=d.clearOverrides;
if(SelectValueIsValid(this.homeSel,d.homeTrigger))this.homeSel.value=d.homeTrigger;
}
json(){
let d={clearOverrides:document.getElementById('clearCheck').checked,homeTrigger:this.homeSel.value?parseInt(this.homeSel.value):0,which:[]};
for(let cam of _cams){
if(cam.demo)continue;
let cc=document.getElementById(cam.num+'.cc');
let mc=document.getElementById(cam.num+'.mc');
let a=document.getElementById(cam.num+'.a');
d.which.push([cam.num,parseInt(cc.value),parseInt(mc.value),parseInt(a.value)])
}
return JSON.stringify(d);
}
}
