<template>
  <BulmaDrawer header="Konfiguration" @open="on_open" refreshable>
    <div class="card-content">
      <div class="columns">
        <div class="column is-one-third">
          <div class="content">
            <h3>Lösung</h3>
            <dl>
              <dt>Wert</dt>
              <dd>
                Eingabe:
                <span class="is-family-monospace">
                  "{{ config_model.solution.value }}"
                </span>
              </dd>
              <dd>
                Ausgabe:
                <span class="is-family-monospace">
                  "{{ config_model.solution.clean }}"
                </span>
              </dd>

              <dt>Transformation</dt>
              <dd>
                Whitespace:
                <span class="is-uppercase is-family-monospace">
                  {{ config_model.solution.whitespace }}
                </span>
              </dd>
              <dd>
                Buchstaben:
                <span class="is-uppercase is-family-monospace">
                  {{ config_model.solution.case }}
                </span>
              </dd>
            </dl>

            <h3>Rätsel</h3>
            <dl>
              <dt>Titel</dt>
              <!-- TODO -->
              <dd>Advent22</dd>

              <dt>Offene Türchen</dt>
              <dd>{{ num_user_doors }}</dd>

              <dt>Zeit zum nächsten Türchen</dt>
              <dd v-if="next_door === null">Kein nächstes Türchen</dd>
              <dd v-else><CountDown :millis="next_door" /></dd>

              <dt>Fußzeile</dt>
              <dd class="is-family-monospace">{{ footer }}</dd>

              <dt>Erstes Türchen</dt>
              <dd>{{ fmt_puzzle_date("first") }}</dd>

              <dt>Nächstes Türchen</dt>
              <dd>{{ fmt_puzzle_date("next") }}</dd>

              <dt>Letztes Türchen</dt>
              <dd>{{ fmt_puzzle_date("last") }}</dd>

              <dt>Rätsel schließt nach</dt>
              <dd>{{ fmt_puzzle_date("end") }}</dd>

              <dt>Zufalls-Seed</dt>
              <dd class="is-family-monospace">
                "{{ config_model.puzzle.seed }}"
              </dd>
            </dl>
          </div>
        </div>
        <div class="column is-one-third">
          <div class="content">
            <h3>Kalender</h3>
            <dl>
              <dt>Definition</dt>
              <dd>{{ config_model.calendar.config_file }}</dd>

              <dt>Hintergrundbild</dt>
              <dd>{{ config_model.calendar.background }}</dd>

              <dt>Türchen ({{ doors.length }} Stück)</dt>
              <dd>
                <template v-for="(door, index) in doors" :key="`door-${index}`">
                  <span>
                    <template v-if="index > 0">, </template>
                    {{ door.day }}
                  </span>
                </template>
              </dd>
            </dl>

            <h3>Bilder</h3>
            <dl>
              <dt>Größe</dt>
              <dd>{{ config_model.image.size }} px</dd>

              <dt>Rand</dt>
              <dd>{{ config_model.image.border }} px</dd>

              <dt>Schriftarten</dt>
              <dd
                v-for="(font, index) in config_model.image.fonts"
                :key="`font-${index}`"
              >
                {{ font.file }} (Größe {{ font.size }})
              </dd>
            </dl>
          </div>
        </div>
        <div class="column is-one-third">
          <div class="content">
            <h3>WebDAV</h3>
            <dl>
              <dt>URL</dt>
              <dd>{{ config_model.webdav.url }}</dd>

              <dt>Zugangsdaten</dt>
              <dd class="is-family-monospace">
                <BulmaSecret @load="load_dav_credentials">
                  <span class="tag is-danger">user</span>
                  {{ dav_credentials[0] }}
                  <br />
                  <span class="tag is-danger">pass</span>
                  {{ dav_credentials[1] }}
                </BulmaSecret>
              </dd>

              <dt>Cache-Dauer</dt>
              <dd>{{ config_model.webdav.cache_ttl }} s</dd>

              <dt>Konfigurationsdatei</dt>
              <dd>{{ config_model.webdav.config_file }}</dd>
            </dl>
          </div>

          <div class="content">
            <h3>Sonstige</h3>
            <dl>
              <dt>Redis</dt>
              <dd>{{ config_model.redis_url }}</dd>

              <dt>UI-Admin</dt>
              <dd class="is-family-monospace">
                <BulmaSecret @load="load_ui_credentials">
                  <span class="tag is-danger">user</span>
                  {{ ui_credentials[0] }}
                  <br />
                  <span class="tag is-danger">pass</span>
                  {{ ui_credentials[1] }}
                </BulmaSecret>
              </dd>
            </dl>
          </div>
        </div>
      </div>
    </div>
  </BulmaDrawer>
</template>

<script lang="ts">
import { ConfigModel, Credentials, DoorsSaved } from "@/lib/api";
import { DateTime } from "luxon";
import { Options, Vue } from "vue-class-component";

import BulmaDrawer from "../bulma/Drawer.vue";
import BulmaSecret from "../bulma/Secret.vue";
import CountDown from "../CountDown.vue";

@Options({
  components: {
    BulmaDrawer,
    BulmaSecret,
    CountDown,
  },
})
export default class extends Vue {
  public config_model: ConfigModel = {
    solution: {
      value: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
      whitespace: null,
      case: null,
      clean: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    },
    puzzle: {
      first: "2023-12-01",
      next: "2023-12-01",
      last: "2023-12-24",
      end: "2024-04-01",
      seed: "",
    },
    calendar: {
      config_file: "lorem ipsum",
      background: "dolor sit amet",
    },
    image: {
      size: 500,
      border: 0,
      fonts: [{ file: "consetetur", size: 0 }],
    },
    redis_url: "sadipscing elitr",
    webdav: {
      url: "sadipscing elitr",
      cache_ttl: 0,
      config_file: "sed diam nonumy",
    },
  };
  public doors: DoorsSaved = [];
  public footer = "";
  public num_user_doors = 0;
  public next_door: number | null = null;
  public dav_credentials: Credentials = ["", ""];
  public ui_credentials: Credentials = ["", ""];

  public fmt_puzzle_date(name: keyof ConfigModel["puzzle"]): string {
    const iso_date = this.config_model.puzzle[name];
    if (iso_date === null) return "-";

    return DateTime.fromISO(iso_date).toLocaleString(DateTime.DATE_SHORT);
  }

  public on_open(ready: () => void, fail: () => void): void {
    Promise.all([
      this.$advent22.api_get<ConfigModel>("admin/config_model"),
      this.$advent22.api_get<DoorsSaved>("admin/doors"),
      this.$advent22.api_get<DoorsSaved>("user/doors"),
      this.$advent22.api_get<string>("user/footer"),
      this.$advent22.api_get<number | null>("user/next_door"),
    ])
      .then(([config_model, doors, user_doors, footer, next_door]) => {
        this.config_model = config_model;
        this.doors = doors;
        this.num_user_doors = user_doors.length;
        this.footer = footer;
        this.next_door = next_door;

        ready();
      })
      .catch(fail);
  }

  public load_dav_credentials(): void {
    this.$advent22
      .api_get<Credentials>("admin/dav_credentials")
      .then((creds) => (this.dav_credentials = creds))
      .catch(() => {});
  }

  public load_ui_credentials(): void {
    this.$advent22
      .api_get<Credentials>("admin/ui_credentials")
      .then((creds) => (this.ui_credentials = creds))
      .catch(() => {});
  }
}
</script>

<style scoped>
dd {
  overflow-x: auto;
}
</style>
