<script setup>

import { ref, onMounted } from 'vue';
import { Button } from 'pankow';
import moment from 'moment-timezone';
import { prettyDecimalSize } from 'pankow/utils';
import Section from '../components/Section.vue';
import DiskUsage from '../components/DiskUsage.vue';
import MemoryUsage from '../components/MemoryUsage.vue';
import CpuUsage from '../components/CpuUsage.vue';
import SystemModel from '../models/SystemModel.js';
import DashboardModel from '../models/DashboardModel.js';

const systemModel = SystemModel.create();
const dashboardModel = DashboardModel.create();

const config = ref({});
const info = ref({});
const memory = ref({});
const cpus = ref({});
const uptime = ref('');
const activeSince = ref('');

function onReboot() {
  // TODO ask the user!
  systemModel.reboot();
}

onMounted(async () => {
  let [error, result] = await systemModel.memory();
  if (error) return console.error(error);
  memory.value = result;

  [error, result] = await systemModel.cpus();
  if (error) return console.error(error);
  cpus.value = result;

  [error, result] = await systemModel.info();
  if (error) return console.error(error);
  info.value = result;

  uptime.value = moment.duration(info.value.uptimeSecs, 'seconds').locale(navigator.language).humanize();
  activeSince.value = info.value.activationTime ? moment(info.value.activationTime).fromNow() : 'unknown';

  [error, result] = await dashboardModel.config();
  if (error) return console.error(error);
  config.value = result;
});

</script>

<template>
  <div class="content">
    <Section :title="$t('system.title')">
      <template #header-buttons>
        <Button href="/logs.html?id=box" target="_blank">{{ $t('main.action.logs') }}</Button>
        <Button @click="onReboot()">{{ $t('main.action.reboot') }}</Button>
      </template>

      <div class="info-row">
        <div class="info-label">{{ $t('system.info.platformVersion') }}</div>
        <div class="info-value">v{{ config.version }} ({{ config.ubuntuVersion }})</div>
      </div>
      <div class="info-row">
        <div class="info-label">{{ $t('system.info.vendor') }}</div>
        <div class="info-value">{{ info.sysVendor }}</div>
      </div>
      <div class="info-row">
        <div class="info-label">{{ $t('system.info.product') }}</div>
        <div class="info-value">{{ info.productName }}</div>
      </div>
      <div class="info-row">
        <div class="info-label">CPU</div>
        <div class="info-value">{{ cpus.length ? `${cpus.length} Core "${cpus[0].model}"` : '' }}</div>
      </div>
      <div class="info-row">
        <div class="info-label">{{ $t('system.info.memory') }}</div>
        <div class="info-value">{{ prettyDecimalSize(memory.memory) }} RAM <span v-show="memory.swap">&amp; {{ prettyDecimalSize(memory.swap) }} Swap</span></div>
      </div>
      <div class="info-row">
        <div class="info-label">{{ $t('system.info.uptime') }}</div>
        <div class="info-value">{{ uptime }}</div>
      </div>
      <div class="info-row" ng-show="info.activationTime">
        <div class="info-label">{{ $t('system.info.activationTime') }}</div>
        <div class="info-value">{{ activeSince }}</div>
      </div>
    </Section>

    <div class="graphs">
      <MemoryUsage style="flex-grow: 1;" />
      <CpuUsage style="flex-grow: 1;" />
    </div>

    <DiskUsage />
  </div>
</template>

<style scoped>

.graphs {
  display: flex;
  gap: 60px;
}

</style>
