<script setup>

import { ref, useTemplateRef } from 'vue';
import { Switch, Dialog } from '@cloudron/pankow';
import SettingsItem from '../components/SettingsItem.vue';
import ProfileModel from '../models/ProfileModel.js';

const profileModel = ProfileModel.create();

const dialogItem = useTemplateRef('dialogItem');
const busy = ref(false);
const appUpp = ref(false);
const appDown = ref(false);
const appOutOfMemory = ref(false);
const backupFailed = ref(false);
const appAutoUpdateFailed = ref(false);
const certificateRenewalFailed = ref(false);
const diskSpace = ref(false);
const cloudronUpdateFailed = ref(false);
const reboot = ref(false);

async function onSubmit() {
  busy.value = true;

  const config = [];
  if (appUpp.value) config.push('appUpp');
  if (appDown.value) config.push('appDown');
  if (appOutOfMemory.value) config.push('appOutOfMemory');
  if (backupFailed.value) config.push('backupFailed');
  if (appAutoUpdateFailed.value) config.push('appAutoUpdateFailed');
  if (certificateRenewalFailed.value) config.push('certificateRenewalFailed');
  if (diskSpace.value) config.push('diskSpace');
  if (cloudronUpdateFailed.value) config.push('cloudronUpdateFailed');
  if (reboot.value) config.push('reboot');

  const [error] = await profileModel.setNotificationConfig(config);
  if (error) return console.error(error);

  dialogItem.value.close();

  busy.value = false;
}

async function open() {
  const [error, result] = await profileModel.get();
  if (error) return console.error(error);

  const config = result.notificationConfig || [];

  appUpp.value = config.indexOf('appUpp') !== -1;
  appDown.value = config.indexOf('appDown') !== -1;
  appOutOfMemory.value = config.indexOf('appOutOfMemory') !== -1;
  backupFailed.value = config.indexOf('backupFailed') !== -1;
  appAutoUpdateFailed.value = config.indexOf('appAutoUpdateFailed') !== -1;
  certificateRenewalFailed.value = config.indexOf('certificateRenewalFailed') !== -1;
  diskSpace.value = config.indexOf('diskSpace') !== -1;
  cloudronUpdateFailed.value = config.indexOf('cloudronUpdateFailed') !== -1;
  reboot.value = config.indexOf('reboot') !== -1;

  dialogItem.value.open();
}

defineExpose({
  open
});

</script>

<template>
  <Dialog ref="dialogItem"
    :title="$t('notifications.settings.title')"
    :confirm-label="$t('main.dialog.save')"
     confirm-style="primary"
    :confirm-busy="busy"
    :confirm-active="!busy"
    :reject-label="$t('main.dialog.close')"
     reject-style="secondary"
    @confirm="onSubmit"
  >

    <div>{{ $t('notifications.settingsDialog.description') }}</div>
    <br/>

    <SettingsItem>
      <div>{{ $t('notifications.settings.appUp') }}</div>
      <Switch v-model="appUpp" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.appDown') }}</div>
      <Switch v-model="appDown" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.appOutOfMemory') }}</div>
      <Switch v-model="appOutOfMemory" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.backupFailed') }}</div>
      <Switch v-model="backupFailed" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.appAutoUpdateFailed') }}</div>
      <Switch v-model="appAutoUpdateFailed" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.certificateRenewalFailed') }}</div>
      <Switch v-model="certificateRenewalFailed" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.diskSpace') }}</div>
      <Switch v-model="diskSpace" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.cloudronUpdateFailed') }}</div>
      <Switch v-model="cloudronUpdateFailed" :disabled="busy"/>
    </SettingsItem>

    <SettingsItem>
      <div>{{ $t('notifications.settings.rebootRequired') }}</div>
      <Switch v-model="reboot" :disabled="busy"/>
    </SettingsItem>
  </Dialog>
</template>
