<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 class="body-custom">

    <GameHeader></GameHeader>

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

    <!-- Действия -->
    <div class="messages-block" ref="scrollContainer">
      <div class="center-block-custom">
        <div v-if="!team || !team.actions.length">
          <div class="center-message">
            <WelcomeGameBlock :qrurl="qrurl" :team="team.name"></WelcomeGameBlock>
          </div>
        </div>
        <div v-else>
          <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>

</template>

<style scoped>
.body-custom {
  font-size: medium;
  height: calc(100vh - 100px);
}

.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: 95px;
  height: calc(100dvh - 100px - 76px);
  overflow-y: auto;
  scrollbar-width: none;
  position: relative;
  padding: 5px 0 15px 0;
}

.center-message {
  height: calc(100dvh - 140px);
}

.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>
