<template>
  <q-dialog
    persistent
    v-model="show_dialog"
    @before-hide="resetData"
    @keydown="
      (evt) => {
        if (!loading && evt.keyCode == 27) {
          show_dialog = false;
        }
      }
    "
  >
    <q-card class="overflow-hidden" style="overflow-y: scroll; max-width: 35vw">
      <q-form @submit="onSubmit">
        <q-card-section class="q-ma-none q-pa-sm">
          <div class="row">
            <div class="col-auto text-h6">
              {{ $t("grid setting") }}
            </div>
            <q-space />
            <div>
              <q-btn
                :loading="loading"
                flat
                round
                icon="close"
                :disable="loading"
                color="red"
                v-close-popup
              >
                <q-tooltip>
                  {{ $t("close") }}
                </q-tooltip>
              </q-btn>
            </div>
          </div>
        </q-card-section>

        <q-separator />

        <q-card-section style="max-height: 50vh; width: 35vw" class="scroll">
          <q-list>
            <q-item>
              <q-item-section>
                <q-select
                  :label="$t('layout')"
                  :options="select_options"
                  v-model="layout"
                >
                </q-select>
              </q-item-section>
            </q-item>
            <q-item>
              <q-item-section>
                <q-select
                  :label="$t('rotation')"
                  :options="rotation_options"
                  v-model="rotation"
                >
                </q-select>
              </q-item-section>
            </q-item>
          </q-list>
        </q-card-section>

        <q-separator />

        <q-card-actions align="right">
          <q-btn
            :loading="loading"
            flat
            :label="$t('Cancel')"
            no-caps
            color="primary"
            v-close-popup
          />
          <q-btn
            ref="accept"
            flat
            :label="$t('Accept')"
            no-caps
            :loading="loading"
            type="submit"
            color="primary"
          />
        </q-card-actions>
      </q-form>
    </q-card>
  </q-dialog>
</template>

<style scoped></style>

<script lang="ts">
import { defineComponent, ref, watch, computed } from "vue";
import { useStore } from "src/store";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
import GlobalData from "src/common/GlobalData";

export default defineComponent({
  name: "ComponentSpecialVideoGridSettingDialog",

  setup() {
    let $store = useStore();
    let $q = useQuasar();
    let $t = useI18n();

    let show_dialog = ref(false);
    let loading = ref(false);
    let layout = ref("1X3");

    const select_options = ["1X3", "1X4", "3X1", "4X1"];
    const row_values = [1, 1, 3, 4];
    const col_values = [3, 4, 1, 1];
    const rotation_options = ref(["0", "90", "180"]);
    const rotation = ref("0");

    rotation.value =
      GlobalData.getInstance()?.applicationConfig
        ?.special_video_layout_rotation ?? "0";

    return {
      show_dialog,
      loading,
      layout,
      select_options,
      rotation_options,
      rotation,

      showDialog() {
        show_dialog.value = true;

        let col = parseInt(
          (GlobalData.getInstance().applicationConfig?.wall_col ?? 1).toString()
        );
        if (isNaN(col)) {
          col = 1;
        }

        let row = parseInt(
          (GlobalData.getInstance().applicationConfig?.wall_row ?? 4).toString()
        );
        if (isNaN(row)) {
          row = 4;
        }

        layout.value = row + "X" + col;
        const v = select_options.find(
          (element) => element && element == layout.value
        );
        if (!v) {
          layout.value = "1X4";
        }

        rotation.value =
          GlobalData.getInstance()?.applicationConfig
            ?.special_video_layout_rotation ?? "0";
      },
      resetData() {
        loading.value = false;
      },

      async onSubmit() {
        loading.value = true;
        try {
          const index = select_options.findIndex((e) => e && e == layout.value);
          if (index != -1) {
            // console.log(row_values[index] + "X" + col_values[index]);
            GlobalData.getInstance()
              .getCurrentClient()
              ?.setWallRowCol(row_values[index], col_values[index]);
            switch (rotation.value) {
              case "0":
              case "90":
              case "180":
                GlobalData.getInstance()
                  .getCurrentClient()
                  ?.setSpecialVideoLayoutRotation(rotation.value ?? "0");
                break;
              default:
                GlobalData.getInstance()
                  .getCurrentClient()
                  ?.setSpecialVideoLayoutRotation("0");
                break;
            }
          }

          show_dialog.value = false;
        } catch {}
        loading.value = false;
      },
    };
  },
});
</script>
