<template>
  <v-app>
    <v-layout column fill-height>
      <TitleBar
        :logo_above="logo_above"
        :logo_below="logo_below"
        :title="title_html"
      />
      <Dashboard>
        <template slot="message">
          <div class="d-flex flex-column fill-height pb-sm-12">
            <Message :html="message_html" />
            <ImageCarousel
              class="mt-auto"
              v-if="image_urls.length > 0"
              :speed="image_speed"
              :height="image_height"
              :contain="image_contain"
              :urls="image_urls"
            />
          </div>
        </template>
        <template slot="calendars">
          <CalendarCarousel :speed="calendar_speed" :data="calendar_data" />
          <DashboardInfo
            class="pb-12 pb-sm-0"
            :server_host="server_host"
            :server_name="server_name"
            :version="dashboard_version"
            :lan_ip="dashboard_ip"
          />
        </template>
      </Dashboard>
      <TickerBar
        v-if="ticker_html !== ''"
        :content="ticker_html"
        :color="ticker_color"
      />
    </v-layout>
  </v-app>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";

import TitleBar from "./components/title/TitleBar.vue";
import Dashboard from "./components/Dashboard.vue";
import ImageCarousel from "./components/ImageCarousel.vue";
import Message from "./components/Message.vue";
import CalendarCarousel from "./components/calendar/CalendarCarousel.vue";
import DashboardInfo from "./components/DashboardInfo.vue";
import TickerBar from "./components/TickerBar.vue";

@Component({
  components: {
    TitleBar,
    Dashboard,
    ImageCarousel,
    Message,
    CalendarCarousel,
    DashboardInfo,
    TickerBar,
  },
})
export default class App extends Vue {
  private logo_above = "Technisches Hilfswerk";
  private logo_below = "OV Musterstadt";
  private title_html = "<h1>changeme</h1>";

  private image_urls = require("@/assets/image_testdata.json");
  private image_height = 300;
  private image_contain = false;
  private image_speed = 10000;
  private message_html = require("@/assets/lipsum.json");

  private calendar_data = require("@/assets/calendar_testdata.json");
  private calendar_speed = 10000;
  private server_host = "https://oekzident.de";
  private server_name = "OEKZident";
  private dashboard_version = "0.0.1";
  private dashboard_ip = "0.0.0.0";

  private ticker_html = "<p>changeme</p>";
  private ticker_color = "primary";

  private fail(name: string): (arg0: unknown) => void {
    return (reason: unknown) =>
      console.warn("Failed to query", name, "-", reason);
  }

  private api_query_simple(
    endpoint: string,
    on_success: (data: unknown) => void
  ): void {
    this.$axios
      .get(this.$ovdashboard.api_url(endpoint))
      .then((response) => on_success(response.data))
      .catch(this.fail(endpoint));
  }

  private api_query_simple_string(
    endpoint: string,
    on_success: (data: string) => void
  ): void {
    this.api_query_simple(endpoint, (data) => {
      if (typeof data !== "string") return;

      on_success(data);
    });
  }

  private api_query_simple_object(
    endpoint: string,
    on_success: (data: Record<string, string>) => void
  ): void {
    this.api_query_simple(endpoint, (data) => {
      if (typeof data !== "object") return;
      if (data === null) return;

      on_success(data as Record<string, string>);
    });
  }

  private update(): void {
    // Update Logo Config
    this.api_query_simple_object("misc/config/logo", (data) => {
      this.logo_above = data.above;
      this.logo_below = data.below;
    });

    // Update Title
    this.$axios
      .get(this.$ovdashboard.api_url("text/get/html/title"))
      .then((response) => {
        this.title_html = response.data;
      })
      .catch(this.fail("Title"));

    // Update Images

    // Update Image Config
    this.$axios
      .get(this.$ovdashboard.api_url("image/config"))
      .then((response) => {
        this.image_height = response.data.height;
        this.image_contain = response.data.contain;
        this.image_speed = response.data.speed;
      })
      .catch(this.fail("Image Config"));

    // Update Message
    this.$axios
      .get(this.$ovdashboard.api_url("text/get/html/message"))
      .then((response) => {
        this.message_html = response.data;
      })
      .catch(this.fail("Message"));

    // Update Calendars

    // Update Calendar Config
    this.$axios
      .get(this.$ovdashboard.api_url("calendar/config"))
      .then((response) => {
        this.calendar_speed = response.data.speed;
      })
      .catch(this.fail("Calendar Config"));

    // Update Server Config
    this.$axios
      .get(this.$ovdashboard.api_url("misc/config/server"))
      .then((response) => {
        this.server_host = response.data.host;
        this.server_name = response.data.name;
      })
      .catch(this.fail("Server Config"));

    // Update Version
    this.$axios
      .get(this.$ovdashboard.api_url("misc/version"))
      .then((response) => {
        this.dashboard_version = response.data;
      })
      .catch(this.fail("Version"));

    // Update IP
    this.$axios
      .get(this.$ovdashboard.api_url("misc/lanip"))
      .then((response) => {
        this.dashboard_ip = response.data;
      })
      .catch(this.fail("IP"));

    // Update Ticker
    this.$axios
      .get(this.$ovdashboard.api_url("ticker/html"))
      .then((response) => {
        this.ticker_html = response.data;
      })
      .catch(this.fail("Ticker"));

    // Update Ticker Config
    this.$axios
      .get(this.$ovdashboard.api_url("ticker/config"))
      .then((response) => {
        this.ticker_color = response.data.color;
      })
      .catch(this.fail("Ticker"));
  }

  public mounted(): void {
    this.update();
  }
}
</script>

<style>
/* Hide scrollbar for Chrome, Safari and Opera */
body::-webkit-scrollbar {
  display: none;
}

/* Hide scrollbar for IE, Edge and Firefox */
body {
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */
}
</style>
