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

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

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

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

      if (error || result.status !== 200) return [error || result];
      return [null, result.body.cpus];
    },
    async blockDevices() {
      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];
    },
    async filesystems() {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/system/filesystems`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body.filesystems];
    },
    async filesystemUsage(filesystem) {
      const es = new EventSource(`${API_ORIGIN}/api/v1/system/filesystem_usage?access_token=${accessToken}&filesystem=${filesystem}`);
      return [null, es];
    },
    async info() {
      let error, result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/system/info`, { access_token: accessToken });
      } catch (e) {
        error = e;
      }

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

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

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

      if (error || result.status !== 201) return [error || result];
      return [null, result.body.taskId];
    },
    async getMetrics(options) {
      let error, result;
      try {
        const query = [
          ['fromSecs', options.fromSecs],
          ['intervalSecs', options.intervalSecs],
          ['system', String(!!options.system)],
          ...options.appIds.map(id => ['appId', id]), // multiple appId=xx
          ...options.serviceIds.map(id => ['serviceId', id]), // multiple serviceId=xx
          ['access_token', accessToken]
        ];
        result = await fetcher.get(`${API_ORIGIN}/api/v1/system/metrics`, query);
      } catch (e) {
        error = e;
      }

      if (error || result.status !== 200) return [error || result];
      return [null, result.body];
    },
    async getMetricStream(options) {
      const query = [
          ['system', String(!!options.system)],
          ...options.appIds.map(id => ['appId', id]), // multiple appId=xx
          ...options.serviceIds.map(id => ['serviceId', id]), // multiple serviceId=xx
          ['access_token', accessToken]
        ];

      const queryString = new URLSearchParams(query).toString();
      return new EventSource(`${API_ORIGIN}/api/v1/system/metricstream?${queryString}`);
    }
  };
}

export default {
  create,
};
