<script setup lang="ts">
import { ref, nextTick, watch, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import GameHeader from './GameHeader.vue';
import type { Action, Door, Team } from './models';
import { apiGetGame, apiGetTeam } from './client';
import { UnauthorizedError } from './UnauthorizedError';
import WelcomeGameBlock from './WelcomeGameBlock.vue';
import MessageCloud from './MessageCloud.vue';
import GameInputForm from './GameInputForm.vue';

const router = useRouter();
const route = useRoute();

const isPlaceFilled = ref(false)
const login = ref("")
const password = ref("")
const team = ref<Team>({ name: "", actions: [] })
const actions = ref<Action[]>([])
const scrollContainer = ref<HTMLDivElement | null>();
const gameState = ref("STOP")
const gameStateText = ref("")

const qrurl = ref("-")

async function getTeam() {
  let data: Team
  try {
    data = await apiGetTeam(login.value, password.value)
  } catch (error: unknown) {
    if (error instanceof UnauthorizedError) {
      // Действия при 401:
      // Сделать редирект на страницу логина
      router.push('/login');
    } else {
      console.error('Неизвестная ошибка:', error);
    }
    return
  }

  const oldActions = team.value.actions
  team.value = data
  const newActions = team.value?.actions
  newActions.forEach(item => {
    item.isOpen = true
  })
  for (let i = 0; i < actions.value.length; i++) {
    newActions[i].isOpen = oldActions[i].isOpen
  }
  if (actions.value.length !== newActions?.length) {
    actions.value = newActions
  }
  for (let i = 0; i < team.value.actions.length; i++) {
    const element = team.value.actions[i];
    team.value.actions[i].buttons = element.doors.filter((door: Door) => { return door.show })
  }
}

const scrollToBottom = async (behavior: ScrollBehavior = 'smooth'): Promise<void> => {
  await nextTick();
  if (scrollContainer.value) {
    scrollContainer.value.scrollTo({
      top: scrollContainer.value.scrollHeight,
      behavior
    });
  }
};

async function getGame() {
  qrurl.value = location.href
  const data = await apiGetGame(login.value, password.value)
  gameState.value = data.state
  if (data.state === "NEW") {
    gameStateText.value = "Игра ещё не началась"
  }
  if (data.state === "RUN") {
    gameStateText.value = ""
  }
  if (data.state === "STOP") {
    gameStateText.value = "Игра остановлена"
  }
}

// Автоматическая прокрутка при изменении items
watch(actions, () => {
  if (isPlaceFilled.value === false) {
    return
  }
  scrollToBottom();
  isPlaceFilled.value = false
}, { deep: true });

let intervalId = 0
onMounted(() => {
  login.value = sessionStorage.getItem("teamId") || ""
  password.value = sessionStorage.getItem("password") || ""
  if (login.value == "") {
    login.value = route.query["name"]?.toString() || ""
    password.value = route.query["password"]?.toString() || ""
    sessionStorage.setItem("teamId", login.value)
    sessionStorage.setItem("password", password.value)
  }

  getTeam()

  intervalId = setInterval(() => {
    getTeam()
    getGame()
  }, 2000);

  router.beforeEach((to, from, next) => {
    clearInterval(intervalId);
    next();
  });
});

</script>

<template>
  <div>
    <GameHeader></GameHeader>

    <GameInputForm v-model="isPlaceFilled" :gameState="gameState" :login="login" :password="password"></GameInputForm>

    <!-- Qr Код -->
    <div v-if="!team || !team.actions.length">
      <div class="messages-block center-container">
        <WelcomeGameBlock :qrurl="qrurl" :team="team.name"></WelcomeGameBlock>
      </div>
    </div>
    <!-- Действия -->
    <div v-else>
      <div class="messages-block" ref="scrollContainer">
        <div class="center-block-custom">
          <div v-for="(action, index) in team.actions" :key="action.id">
            <MessageCloud v-model="isPlaceFilled" :action="action" :gameState="gameState" :login="login"
              :password="password" :index="index" :count="team.actions.length"></MessageCloud>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div v-if="gameState == 'STOP'" class="modal-overlay">
    <div class="modal-content">
      <div class="modal-body"></div>
    </div>
  </div>
</template>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.7);
  /* Полупрозрачный черный цвет */
  display: flex;
  justify-content: center;
  /* Центрируем по горизонтали */
  align-items: center;
  /* Центрируем по вертикали */
  z-index: 9999;
  /* Поверх всех остальных элементов */
  backdrop-filter: blur(3px);
  /* Эффект размытия заднего фона (по желанию) */
}

.modal-content {
  position: relative;
  width: 90%;
  max-width: 420px;
  min-height: 250px;
  background-position: center;
  /* Картинка центрируется */
  background-repeat: no-repeat;
  overflow: hidden;

  background-image: url("@/assets/stop.png");
  background-size: cover;
}

.modal-body {
  position: relative;
  z-index: 2;
  padding: 30px;
}

.game-input-form-shadow {
  height: 90px;
  width: 120%;
  left: -10%;
  top: 3px;
  position: absolute;
  box-shadow: 0px -5px 10px black;
  z-index: 9;
  background-color: black;
}

.messages-block {
  top: 90px;
  /* height: calc(100dvh - 100px - 76px); */
  /* 90px от верхнего края экрана до второго ремня */
  /* 100px от верхнего края экрана до края иконки */
  /* 76px от нижнего края экрана до края ремня */
  height: calc(100dvh - 90px - 76px);
  overflow-y: auto;
  scrollbar-width: none;
  position: relative;
  padding: 15px 10px 15px 10px;
}

.team-name-block {
  margin-right: 10px;
  width: 50px;
  height: 40px;
  font-family: a_OldTyper;
}

.text-middle-wrapper {
  position: relative;
  height: 100%;
}

.text-middle-wrapper p {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-right: -50%;
  transform: translate(-50%, -50%)
}

.text-truncate {
  text-align: center;
  white-space: nowrap;
  /* Запрещаем перенос текста */
  overflow: hidden;
  /* Обрезаем все, что не помещается */
  text-overflow: ellipsis;
  /* Добавляем троеточие */
  font-size: medium;
}
</style>
