<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>Rätsel</h3>
            <dl>
              <dt>Titel</dt>
              <!-- TODO -->
              <dd>Advent22</dd>

              <dt>Lösung</dt>
              <dd>{{ config_model.puzzle.solution }}</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>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</dt>
              <dd>
                <template v-for="(part, day) in day_parts" :key="`door-${day}`">
                  <span>
                    <template v-if="Number(day) > 1">, </template>
                    <span v-if="part == ''" class="tag is-warning">_</span>
                    <template v-else>{{ part }}</template>
                  </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.username }}
                  <br />
                  <span class="tag is-danger">pass</span>
                  {{ dav_credentials.password }}
                </BulmaSecret>
              </dd>

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

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

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

<script lang="ts">
import { ConfigModel, Dictionary, 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";

interface Credentials {
  username: string;
  password: string;
}

@Options({
  components: {
    BulmaDrawer,
    BulmaSecret,
    CountDown,
  },
})
export default class extends Vue {
  public config_model: ConfigModel = {
    puzzle: {
      solution: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
      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 }],
    },
    webdav: {
      url: "sadipscing elitr",
      cache_ttl: 0,
      config_file: "sed diam nonumy",
    },
  };
  public day_parts: Dictionary = {};
  public num_user_doors = 0;
  public next_door: number | null = null;
  public dav_credentials: Credentials = { username: "", password: "" };
  public ui_credentials: Credentials = { username: "", password: "" };

  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<Dictionary>("admin/day_parts"),
      this.$advent22.api_get<DoorsSaved>("user/doors"),
      this.$advent22.api_get<number | null>("user/next_door"),
    ])
      .then(([config_model, day_parts, user_doors, next_door]) => {
        this.config_model = config_model;
        this.day_parts = day_parts;
        this.num_user_doors = user_doors.length;
        this.next_door = next_door;

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

  public load_dav_credentials(): void {
    this.$advent22
      .api_get<string[]>("admin/dav_credentials")
      .then(([username, password]) => {
        this.dav_credentials = {
          username: username,
          password: password,
        };
      })
      .catch(() => {});
  }

  public load_ui_credentials(): void {
    this.$advent22
      .api_get<string[]>("admin/ui_credentials")
      .then(([username, password]) => {
        this.ui_credentials = {
          username: username,
          password: password,
        };
      })
      .catch(() => {});
  }
}
</script>

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