<script setup>

import { ref, onMounted } from 'vue';
import { Button } from 'pankow';
import Section from './Section.vue';
import MailModel from '../models/MailModel.js';

const props = defineProps([ 'domain' ]);

const mailModel = MailModel.create();

const expectedDnsRecordsTypes = [
  { name: 'MX', value: 'mx' },
  { name: 'DKIM', value: 'dkim' },
  { name: 'SPF', value: 'spf' },
  { name: 'DMARC', value: 'dmarc' },
  { name: 'PTR4', value: 'ptr4' },
  { name: 'PTR6', value: 'ptr6' },
];

const expectedDnsRecords = ref({});
const busy = ref(false);
const mailConfig = ref({});
const mailStatus = ref({});
const relayStatusCollapsed = ref(true);
const cloudronSmtpCollapsed = ref(true);

function onToggleCollapsed(record) {
  record.collapsed = !record.collapsed;
}

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

  let [error, result] = await mailModel.status(props.domain);
  if (error) return console.error(error);

  mailStatus.value = result;

  for (const type of expectedDnsRecordsTypes) {
    expectedDnsRecords.value[type.value] = result.dns[type.value] || {};
    expectedDnsRecords.value[type.value].collapsed = expectedDnsRecords.value[type.value].status;
  }

  [error, result] = await mailModel.config(props.domain);
  if (error) return console.error(error);

  mailConfig.value = result;
  relayStatusCollapsed.value = true;
  cloudronSmtpCollapsed.value = true;

  busy.value = false;
}

onMounted(async () => {
  await refresh();
});

</script>

<template>
  <Section>

    <h4>{{ $t('email.dnsStatus.title') }} &nbsp; <Button @click="refresh()" small tool icon="fa fa-sync-alt" :disabled="busy" :loading="busy"/></h4>
    <div v-html="$t('email.dnsStatus.description', { emailDnsDocsLink:'https://docs.cloudron.io/email/#dns-records'})"></div>
    <br/>
    <div v-for="record in expectedDnsRecordsTypes" :key="record.value">
      <div v-if="expectedDnsRecords[record.value]?.expected">
        <div class="text-muted">
          <i v-if="!busy" class="fa-solid" :class="{ 'fa-check-circle text-success': expectedDnsRecords[record.value].status, 'fa-exclamation-triangle text-danger': !expectedDnsRecords[record.value].status }"></i>
          <i v-else class="fa-solid fa-circle-notch fa-spin"></i>
          &nbsp;
          <span class="actionable" @click="onToggleCollapsed(expectedDnsRecords[record.value])">{{ record.name }} record</span>
        </div>
        <div class="record-details" v-show="!expectedDnsRecords[record.value]?.collapsed">
          <div v-if="record.name === 'MX' && domain.provider === 'namecheap'">{{ $t('email.dnsStatus.namecheapInfo') }} <sup><a href="https://docs.cloudron.io/domains/#namecheap-dns" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></div>
          <div v-if="record.name === 'PTR4' || record.name === 'PTR6'">{{ $t('email.dnsStatus.ptrInfo') }} <sup><a href="https://docs.cloudron.io/email/#ptr-record" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></div>
          <table>
            <tr v-if="expectedDnsRecords[record.value].name">
              <td>{{ $t('email.dnsStatus.hostname') }}:</td>
              <td>{{ expectedDnsRecords[record.value].name }}</td>
            </tr>
            <tr v-if="!expectedDnsRecords[record.value].name">
              <td>{{ $t('email.dnsStatus.domain') }}:</td>
              <td>{{ expectedDnsRecords[record.value].domain }}</td>
            </tr>
            <tr>
              <td style="width: 10px;">{{ $t('email.dnsStatus.type') }}:</td>
              <td>{{ expectedDnsRecords[record.value].type }}</td>
            </tr>
            <tr>
              <td>{{ $t('email.dnsStatus.expected') }}:</td>
              <td>{{ expectedDnsRecords[record.value].expected }}</td>
            </tr>
            <tr>
              <td>{{ $t('email.dnsStatus.current') }}:</td>
              <td>{{ expectedDnsRecords[record.value].value ? expectedDnsRecords[record.value].value : ('['+('email.dnsStatus.recordNotSet' | tr)+']') }}</td>
            </tr>
          </table>
        </div>
      </div>
    </div>
    <br/>

    <h4>{{ $t('email.smtpStatus.title') }} <sup><a href="https://docs.cloudron.io/email/#smtp-status" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></h4>

    <div>
      <div class="text-muted">
        <i v-if="!busy" class="fa" :class="{ 'fa-check-circle text-success': mailStatus.relay?.status, 'fa-exclamation-triangle text-danger': !mailStatus.relay?.status }"></i>
        <i v-else class="fa-solid fa-circle-notch fa-spin"></i>
        &nbsp;
        <span class="actionable" @click="relayStatusCollapsed = !relayStatusCollapsed">{{ $t(mailConfig.relay?.provider === 'cloudron-smtp' ? 'email.smtpStatus.outboudDirect' : 'email.smtpStatus.outboudRelay') }}</span>
      </div>
      <div class="record-details" v-show="!relayStatusCollapsed">
        <b>{{ mailStatus.relay?.value }}</b>
      </div>
    </div>

    <div v-if="mailConfig.relay?.provider === 'cloudron-smtp'">
      <div class="text-muted">
        <i v-if="!busy" class="fa" :class="{ 'fa-check-circle text-success': mailStatus.rbl?.status, 'fa-exclamation-triangle text-danger': !mailStatus.rbl?.status }"></i>
        <i v-else class="fa-solid fa-circle-notch fa-spin"></i>
        &nbsp;
        <span class="actionable" @click="cloudronSmtpCollapsed = !cloudronSmtpCollapsed">{{ $t('email.smtpStatus.blacklistCheck') }}</span>
      </div>
      <div class="record-details" v-show="!cloudronSmtpCollapsed">
        <div v-if="mailStatus.rbl?.servers.length" v-html="$t('email.smtpStatus.blacklisted', { ip: mailStatus.rbl.ip })"></div>
        <div v-else v-html="$t('email.smtpStatus.notBlacklisted', { ip: mailStatus.rbl?.ip })"></div>

        <div v-for="server in mailStatus.rbl.servers" :key="server.name">
          <a :href="server.site" target="_blank">{{ server.name }}</a>
        </div>
      </div>
    </div>
  </Section>
</template>

<style scoped>

.record-details {
  padding: 10px 25px;
  overflow: hidden;
}

.record-details > table {
  table-layout: fixed;
  width: 100%;
}

.record-details > table > tr > td:nth-child(1) {
  width: 150px;
}

.record-details > table > tr > td:nth-child(2) {
  display: block;
  overflow: scroll;
  white-space: nowrap;
  text-overflow: auto;
  font-weight: bold;
}

</style>
