
import { fetcher } from '@cloudron/pankow';
import { API_ORIGIN } from '../constants.js';

const mountTypes = [
  { name: 'CIFS', value: 'cifs' },
  { name: 'EXT4', value: 'ext4' },
  { name: 'Filesystem', value: 'filesystem' },
  { name: 'Filesystem (Mount point)', value: 'mountpoint' },
  { name: 'NFS', value: 'nfs' },
  { name: 'SSHFS', value: 'sshfs' },
  { name: 'XFS', value: 'xfs' },
];

function filterConfigForMountType(mountType, config) {
  let props = [];
  switch (mountType) {
    case 'filesystem':
      props = ['hostPath'];
      break;
    case 'mountpoint':
      props = ['hostPath'];
      break;
    case 'ext4':
      props = ['diskPath'];
      break;
    case 'xfs':
      props = ['diskPath'];
      break;
    case 'cifs':
      props = ['host', 'seal', 'remoteDir', 'username', 'password'];
      break;
    case 'nfs':
      props = ['host', 'remoteDir'];
      break;
    case 'sshfs':
      props = ['host', 'port', 'remoteDir', 'user', 'privateKey'];
      break;
  }

  const ret = {};
  for (const p of props) ret[p] = config[p];
  return ret;
}

function create() {
  const accessToken = localStorage.token;

  return {
    async list() {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/volumes`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error | result];

      return [null, result.body.volumes];
    },
    async getStatus(id) {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/volumes/${id}/status`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body];
    },
    async remount(id) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/volumes/${id}/remount`, {}, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 202) return [error || result];
      return [null, result.body];
    },
    async add(name, mountType, mountOptions) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/volumes`, { name, mountType, mountOptions: filterConfigForMountType(mountType, mountOptions) }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 201) return [error || result];
      return [null, result.body];
    },
    async update(id, mountType, mountOptions) {
      let error, result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/volumes/${id}`, { mountOptions: filterConfigForMountType(mountType, mountOptions) }, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 204) return [error || result];
      return [null, result.body];
    },
    async remove(id) {
      let error, result;
      try {
        result = await fetcher.del(`${API_ORIGIN}/api/v1/volumes/${id}`, null, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 204) return [error || result];
      return [null, result.body];
    },
    async getBlockDevices() {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/system/block_devices`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body.devices];
    },
  };
}

export default {
  create,
  mountTypes,
};
