<script setup>
import { watch, ref } from "vue";

import COLORS from "../../colors";
import PEOPLE from "../../people";
import TEMPLATES from "../../templates";
import DEFAULT_CONTRACTOR from "../../contractors";
import {
  generateDefaultLogos,
  LOGO_POSITIONS,
  generateLogoPositions,
} from "../../logos";
import { SOURCE_IMAGES } from "../utils/newspaper_quotes";
import {
  loadFonts,
  loadCanvasStorage,
  setCanvasStorage,
  updateAutoRedrawStorage,
} from "../../utils";

import Canvas from "../../components/canvas/Canvas.vue";
import redraw from "./canvas";

import Navbar from "../../components/Navbar.vue";
import MainContainer from "../../components/MainContainer.vue";
import ImageInput from "../../components/inputs/ImageInput.vue";
import LongTextInput from "../../components/inputs/text/LongTextInput.vue";
import ShortTextInput from "../../components/inputs/text/ShortTextInput.vue";
import InputSeparator from "../../components/inputs/InputSeparator.vue";
import SelectInput from "../../components/inputs/SelectInput.vue";
import MultipleColorPicker from "../../components/inputs/colors/MultipleColorPicker.vue";
import ReloadButton from "../../components/reload/ReloadButton.vue";
import AutoReloadCheckbox from "../../components/reload/AutoReloadCheckbox.vue";
</script>

<script>
await loadFonts([
  "700 12px Glegoo",
  "12px Roboto Condensed",
  "bold 12px Roboto Condensed",
]);

export default {
  components: {
    Canvas,
    Navbar,
    MainContainer,
    ImageInput,
    LongTextInput,
    ShortTextInput,
    InputSeparator,
    MultipleColorPicker,
  },
  data() {
    const predefinedColors = {
      base: {
        name: "Základní barvy",
        colors: {
          background: COLORS.black,
          highlight: COLORS.yellow1,
          quotes: COLORS.yellow1,
          baseText: COLORS.white,
          highlightedText: COLORS.black,
          contractedByText: COLORS.gray1,
        },
      },
      gray: {
        name: "Šedé pozadí",
        colors: {
          background: COLORS.gray1,
          highlight: COLORS.yellow1,
          quotes: COLORS.yellow1,
          baseText: COLORS.black,
          highlightedText: COLORS.black,
        },
      },
    };

    return {
      mainImage: null,
      sourceImage: null,
      mainText: null,
      personName: null,
      personPosition: null,
      contractedBy: DEFAULT_CONTRACTOR,
      logoImage: null,
      logoPosition: LOGO_POSITIONS.top_right,
      logoOptions: generateLogoPositions(["top_right", "top_left"]),
      colorLabels: {
        background: "Pozadí",
        highlight: "Zvýraznění",
        quotes: "Uvozovky",
        baseText: "Text",
        highlightedText: "Zvýrazněný text",
      },
      predefinedColors: predefinedColors,
      colors: predefinedColors.base.colors,
      predefinedLogoImages: generateDefaultLogos("defaultLight"),
      predefinedSourceImages: SOURCE_IMAGES,
      autoRedraw: false,
    };
  },
  async created() {
    await loadCanvasStorage(this);
  },
  methods: {
    async reloadCanvasProperties() {
      const canvasProperties = {
        mainImage: this.mainImage,
        sourceImage: this.sourceImage,
        mainText: this.mainText,
        personName: this.personName,
        personPosition: this.personPosition,
        contractedBy: this.contractedBy,
        logoImage: this.logoImage,
        logoPosition: this.logoPosition,
        colors: this.colors,
      };

      if (canvasProperties.mainText) {
        window.fileName = canvasProperties.mainText;
      }

      await this.$refs.canvas.redraw(canvasProperties);

      delete canvasProperties.colors;
      setCanvasStorage(canvasProperties);
    },
  },
  mounted() {
    this.$watch(
      (vm) => [
        vm.mainImage,
        vm.sourceImage,
        vm.mainText,
        vm.personName,
        vm.personPosition,
        vm.contractedBy,
        vm.logoImage,
        vm.logoPosition,
        vm.colors,
      ],
      async (value) => {
        if (this.autoRedraw) {
          await this.reloadCanvasProperties();
        }
      },
      {
        immediate: true,
        deep: true,
      },
    );

    this.$watch(
      (vm) => [vm.autoRedraw],
      async (value) => {
        updateAutoRedrawStorage(this.autoRedraw);

        if (this.autoRedraw) {
          await this.reloadCanvasProperties();
        }
      },
    );
  },
};
</script>

<template>
  <header>
    <Navbar :defaultTemplate="TEMPLATES.newspaper_quote_bottom"></Navbar>
  </header>
  <main>
    <MainContainer>
      <template v-slot:left>
        <Canvas
          ref="canvas"
          :redrawFunction="redraw"
          width="2000"
          height="2000"
        />
      </template>

      <template v-slot:right>
        <ReloadButton :parentRefs="$refs" @click="reloadCanvasProperties" />
        <AutoReloadCheckbox v-model="autoRedraw" />
        <ImageInput
          name="Obrázek"
          v-model="mainImage"
          :important="true"
          zIndex="10"
        />
        <LongTextInput
          name="Hlavní text"
          v-model="mainText"
          :important="true"
          :highlightable="true"
          zIndex="9"
        />
        <ShortTextInput
          name="Jméno osoby"
          v-model="personName"
          v-model:relatedModel="personPosition"
          :predefinedValues="[
            {
              self: 'Ivan Bartoš',
              related: 'ministr pro místní rozvoj',
            },
            {
              self: 'Olga Richterová',
              related: 'místopředsedkyně\nPoslanecké sněmovny',
            },
            {
              self: 'Jan Lipavský',
              related: 'ministr zahraničních věcí',
            },
            {
              self: 'Klára Kocmanová',
              related: 'pirátská poslankyně',
            },
            {
              self: 'Jakub Michálek',
              related: 'pirátský poslanec',
            },
            {
              self: 'Michal Šalamoun',
              related: 'ministr pro legislativu',
            },
          ]"
          :important="true"
          zIndex="8"
        />
        <LongTextInput
          name="Pozice osoby"
          v-model="personPosition"
          :important="false"
          zIndex="7"
        />
        <ImageInput
          name="Obrázek zdroje"
          v-model="sourceImage"
          :important="true"
          :predefinedImages="predefinedSourceImages"
          :mustSelectPredefinedImage="false"
          zIndex="6"
        />

        <InputSeparator />

        <ImageInput
          name="Obrázek loga"
          v-model="logoImage"
          :important="false"
          :predefinedImages="predefinedLogoImages"
          :mustSelectPredefinedImage="true"
          :disableImageInput="true"
          zIndex="5"
        />
        <SelectInput
          name="Pozice loga"
          :options="logoOptions"
          v-model="logoPosition"
          zIndex="4"
        />

        <MultipleColorPicker
          name="Barvy"
          v-model="colors"
          :important="false"
          :colorLabels="colorLabels"
          :predefinedColors="predefinedColors"
          :defaultPredefinedColors="predefinedColors.base"
          zIndex="3"
        ></MultipleColorPicker>

        <ShortTextInput
          name="Zadavatel a zpracovatel"
          v-model="contractedBy"
          :defaultValue="DEFAULT_CONTRACTOR"
          :important="false"
          zIndex="2"
        />
      </template>
    </MainContainer>
  </main>
</template>

<style>
@import "vue-select/dist/vue-select.css";
@import "../../assets/fonts/glegoo/style.css";
</style>
