<alarm-notifications>
  <hlpopup title="Alarm" icon="fa-solid fa-bell" each={alarm, i in expired_alarms}  buttons={buttons} onclose={remove} preventoverlay={true} >
    <div class="alarm_msg">
      <h4>{alarm.name}</h4>
    </div>
  </hlpopup>

  <script>
    // test data {"uid":"960cc36d-4520-4991-ac9a-80b1f1670287","cid":"d1ef34c0-a555-4963-922f-f029cfca48da","aid":"ed56c03a-1580-4f57-b917-7d4cfa793805","name":"John","start":1509810600631,"details":"","repeat":"never","run":null}
    this.expired_alarms = [];

    this.buttons = [{
      label: '5 min',
      value: 5,
      icon: 'fa-solid fa-plus',
      onclick: function(e){
        this.snooze(this.alarm, e.item.button.value)
      }
    }, {
      label: '10 min',
      value: 10,
      icon: 'fa-solid fa-plus',
      onclick: function(e){
        this.snooze(this.alarm, e.item.button.value)
      }
    }, {
      label: '15 min',
      value: 15,
      icon: 'fa-solid fa-plus',
      onclick: function(e){
        this.snooze(this.alarm, e.item.button.value)
      }
    }]

    this.snooze = async (alarm, value)=>{
      try{
        let time = Number(value) * 60; // turn into seconds

        let res = await harmony.post(`/alarm/${encodeURIComponent(alarm.aid)}/snooze`, {
          token: this.store.token,
          seconds: time,
        })
        if(res.code != 200) throw res.errors;

        let a = res.alarms[0]
        // will get removed in pubsub
        this.update();
      }catch(e){
        this.show_errors(e)
      }
    }

    // when an alarm is showing and they don't want it to show anymore, close it
    this.remove = (e)=>{
      let a = e.item.alarm;
      // remove alarms that are showing
      for(let i = 0; i<this.expired_alarms.length; i++){
        if(this.expired_alarms[i].aid == a.aid){
          this.expired_alarms.splice(i, 1);
          i--;
        }
      }
      this.update();
    }

    this.audio = new Audio('/music/blip.mp3');

    // When alarms go off
    harmony.on('notify /alarm', (alarm)=>{
      console.log('alarm', alarm)
      this.expired_alarms.push(alarm);
      this.audio.play();
      this.update();

      if(this.allowDesktopNotifications){
        let desktop = new Notification(alarm.name, {
          // can put icons here which would be cool
          //body: '',
        })
        // desktop.onclick = ()=>{}
      }

    })

    // When alarm is snoozed
    harmony.on('notify /alarm/snooze', (alarm)=>{
      for(let i = 0; i<this.expired_alarms.length; i++){
        if(this.expired_alarms[i].aid == alarm.uuid){
          this.expired_alarms.splice(i, 1);
          i--;
        }
      }
      this.update();
    })

    this.allowDesktopNotifications = false
    this.on('mount', async ()=>{
      try{
        let permission = await Notification.requestPermission()
        if(permission == 'granted'){
          this.allowDesktopNotifications = true;
        }
      }catch(e){
        console.log('User denied desktop notifications')
      }
    })
  </script>

  <style>
    .alarm_msg{
      position: relative;
      padding: 20px 30px;
      max-width: 400px;
      background: #FFF1C4;
      border-radius: 15px;
    }
    .alarm_msg h4{
      text-align: center;
    }
  </style>

</alarm-notifications>
