<script setup>

import { ref, useTemplateRef } from 'vue';
import { Dialog } from 'pankow';
import { REGIONS_CONTABO, REGIONS_VULTR, REGIONS_IONOS, REGIONS_OVH, REGIONS_LINODE, REGIONS_SCALEWAY, REGIONS_WASABI } from '../constants.js';
import { mountlike, s3like } from  '../utils.js';
import BackupProviderForm from './BackupProviderForm.vue';
import BackupsModel from '../models/BackupsModel.js';

const emit = defineEmits([ 'success' ]);

const backupsModel = BackupsModel.create();

const dialog = useTemplateRef('dialog');
const form = useTemplateRef('form');
const config = ref({});
const formError = ref({});
const busy = ref(false);
const provider = ref('');
const oldProvider = ref('');
const oldFormat = ref('');
const providerConfig = ref({
  limits: {},
  mountOptions: {},
});

async function onSubmit() {
  if (!form.value.reportValidity()) return;

  const data = {
    provider: provider.value,
    format: providerConfig.value.format,
  };

  // required for api call to provide all fields
  data.schedulePattern = config.value.schedulePattern;
  data.retentionPolicy = config.value.retentionPolicy;

  if (providerConfig.value.encryptionPassword) {
    data.encryptedFilenames = providerConfig.value.encryptedFilenames;
    data.password = providerConfig.value.encryptionPassword;
  }

  if (s3like(data.provider)) {
    data.endpoint = providerConfig.value.endpoint;
    data.prefix = providerConfig.value.prefix;
    data.bucket = providerConfig.value.bucket;
    data.accessKeyId = providerConfig.value.accessKeyId;
    data.secretAccessKey = providerConfig.value.secretAccessKey;

    if (data.provider === 's3') {
      data.region = providerConfig.value.region || undefined;
      delete data.endpoint;
    } else if (data.provider === 'minio' || data.provider === 's3-v4-compat') {
      data.region = providerConfig.value.region || 'us-east-1';
      data.acceptSelfSignedCerts = providerConfig.value.acceptSelfSignedCerts;
      data.s3ForcePathStyle = true;
    } else if (data.provider === 'exoscale-sos') {
      data.region = 'us-east-1';
      data.signatureVersion = 'v4';
    } else if (data.provider === 'wasabi') {
      data.region = REGIONS_WASABI.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
    } else if (data.provider === 'scaleway-objectstorage') {
      data.region = REGIONS_SCALEWAY.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
    } else if (data.provider === 'linode-objectstorage') {
      data.region = REGIONS_LINODE.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
    } else if (data.provider === 'ovh-objectstorage') {
      data.region = REGIONS_OVH.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
    } else if (data.provider === 'ionos-objectstorage') {
      data.region = REGIONS_IONOS.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
    } else if (data.provider === 'vultr-objectstorage') {
      data.region = REGIONS_VULTR.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
    } else if (data.provider === 'contabo-objectstorage') {
      data.region = REGIONS_CONTABO.find(function (x) { return x.value === data.endpoint; }).region;
      data.signatureVersion = 'v4';
      data.s3ForcePathStyle = true;
    } else if (data.provider === 'upcloud-objectstorage') { // the UI sets region and endpoint
      const m = /^.*\.(.*)\.upcloudobjects.com$/.exec(data.endpoint);
      data.region = m ? m[1] : 'us-east-1'; // let it fail in validation phase if m is not valid
      data.signatureVersion = 'v4';
    } else if (data.provider === 'digitalocean-spaces') {
      data.region = 'us-east-1';
    } else if (data.provider === 'hetzner-objectstorage') {
      data.region = 'us-east-1';
      data.signatureVersion = 'v4';
    }
  } else if (mountlike(data.provider)) {
    data.prefix = providerConfig.value.prefix;
    data.noHardlinks = !providerConfig.value.useHardlinks;
    data.mountOptions = {};

    if (data.provider === 'cifs' || data.provider === 'sshfs' || data.provider === 'nfs') {
      data.mountOptions.host = providerConfig.value.mountOptionHost;
      data.mountOptions.remoteDir = providerConfig.value.mountOptionRemoteDir;

      if (data.provider === 'cifs') {
        data.mountOptions.username = providerConfig.value.mountOptionUsername;
        data.mountOptions.password = providerConfig.value.mountOptionPassword;
        data.mountOptions.seal = !!providerConfig.value.mountOptionSeal;
        data.preserveAttributes = !!providerConfig.value.preserveAttributes;
      } else if (data.provider === 'sshfs') {
        data.mountOptions.user = providerConfig.value.mountOptionUser;
        data.mountOptions.port = parseInt(providerConfig.value.mountOptionPort);
        data.mountOptions.privateKey = providerConfig.value.mountOptionPrivateKey;
        data.preserveAttributes = true;
      }
    } else if (data.provider === 'ext4' || data.provider === 'xfs' || data.provider === 'disk') {
        data.mountOptions.diskPath = providerConfig.value.mountOptionDiskPath;
        data.preserveAttributes = true;
    } else if (data.provider === 'mountpoint') {
        data.mountPoint = providerConfig.value.mountPoint;
        data.chown = !!providerConfig.value.chown;
        data.preserveAttributes = !!providerConfig.value.preserveAttributes;
    }
  } else if (data.provider === 'filesystem') {
    data.backupFolder = providerConfig.value.backupFolder;
    data.noHardlinks = !providerConfig.value.useHardlinks;
    data.preserveAttributes = true;
  } else if (data.provider === 'gcs') {
    data.bucket = providerConfig.value.bucket;
    data.prefix = providerConfig.value.prefix;
    data.projectId = providerConfig.value.projectId;
    data.credentials = providerConfig.value.credentials;
  }

  const limits = {
    memoryLimit: parseInt(providerConfig.value.limits.memoryLimit),
    syncConcurrency: parseInt(providerConfig.value.limits.syncConcurrency),
    copyConcurrency: parseInt(providerConfig.value.limits.copyConcurrency),
    downloadConcurrency: parseInt(providerConfig.value.limits.downloadConcurrency),
    uploadPartSize: parseInt(providerConfig.value.limits.uploadPartSize),
    // deleteConcurrency: parseInt(providerConfig.value.limits.deleteConcurrency),
  };

  formError.value = {};
  busy.value = true;
  const [error] = await backupsModel.setConfig(data, limits);
  if (error) {
    formError.value.generic = error.body ? error.body.message : 'Internal error';
    busy.value = false;
    return console.error(error);
  }

  emit('success');
  dialog.value.close();
}

defineExpose({
  async open() {
    const [error, result] = await backupsModel.getConfig();
    if (error) return console.error(error);

    config.value = result;
    formError.value = {};
    busy.value = false;
    oldProvider.value = result.provider;
    provider.value = result.provider;
    providerConfig.value = result;
    oldFormat.value = result.format;

    // ensure we have all required child objects
    if (!providerConfig.value.mountOptions) providerConfig.value.mountOptions = {};
    if (!providerConfig.value.limits) providerConfig.value.limits = {};

    // some sane defaults
    if (!providerConfig.value.limits.memoryLimit) providerConfig.value.limits.memoryLimit = 1024 * 1024 * 1024; // 1 GB
    if (!providerConfig.value.limits.uploadPartSize) providerConfig.value.limits.uploadPartSize = 10 * 1024 * 1024;
    if (!providerConfig.value.limits.syncConcurrency) providerConfig.value.limits.syncConcurrency = 10;
    if (!providerConfig.value.limits.downloadConcurrency) providerConfig.value.limits.downloadConcurrency = 10;
    if (!providerConfig.value.limits.copyConcurrency) providerConfig.value.limits.copyConcurrency = 10;

    // needs translation for UI
    providerConfig.value.useHardlinks = !result.noHardlinks;
    providerConfig.value.encryptionPassword = result.password;

    dialog.value.open();
  }
});

</script>

<template>
  <Dialog ref="dialog"
    :title="$t('backups.configureBackupStorage.title')"
    :confirm-label="$t('main.dialog.save')"
    :confirm-busy="busy"
    :reject-label="busy ? null :$t('main.dialog.cancel')"
     reject-style="secondary"
    @confirm="onSubmit()"
  >
    <div>
      <form @submit.prevent="onSubmit()" autocomplete="off" ref="form">
        <fieldset :disabled="busy">
          <input style="display: none;" type="submit"/>
          <div class="error-label" v-show="formError.generic">{{ formError.generic }}</div>
          <div class="warning-label" v-show="oldProvider !== provider || oldFormat !== providerConfig.format">{{ $t('backups.configureBackupStorage.formatChangeNote') }}</div>

          <BackupProviderForm v-model:provider="provider" v-model:provider-config="providerConfig" :form-error="formError"/>

        </fieldset>
      </form>
    </div>
  </Dialog>
</template>
