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

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

const inputPlace = ref(false)
const login = ref("")
const password = ref("")
const place = 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 })
  }
}

async function addAction() {
  inputPlace.value = true
  const placeValue = place.value.trim()
  if (placeValue === "") {
    place.value = ""
    return
  }
  await apiLetsgo(login.value, password.value, placeValue)
  place.value = ""
}

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 (inputPlace.value === false) {
    return
  }
  scrollToBottom();
  inputPlace.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>

    <!-- Форма ввода -->
    <div class="form-custom">
      <BeltBlock class="game-input-form">
        <MetalPlate class="controller-metal controller-metal-left"></MetalPlate>
        <MetalPlate class="controller-metal controller-metal-right"></MetalPlate>
        <div class="center-block-custom">
          <form @submit.prevent="addAction">
            <div class="controller">
              <div class="game-input">
                <div class="game-input-run-left"></div>
                <input id="run" class="game-input-run" v-model="place" type="text" placeholder="Место назначения"
                  :disabled="gameState !== 'RUN'">
              </div>
              <div class="game-button-run-shadow"></div>
              <button class="game-button-run" type="submit" :disabled="gameState !== 'RUN'">
                <HeaderText>Поехали</HeaderText>
              </button>
            </div>
          </form>
        </div>
      </BeltBlock>
    </div>

    <!-- Действия -->
    <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 in team.actions" :key="action.id">
            <MessageCloud :action="action" :gameState="gameState" :login="login" :password="password"></MessageCloud>
          </div>
        </div>
      </div>
    </div>

  </div>

</template>

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

.form-custom {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  color: white;
  z-index: 1000;
}

.game-input-form {
  height: 76px;
  position: relative;
  z-index: 1000;
}

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

@media (min-width: 1025px) {
  .center-block-custom {
    width: 700px;
    margin: 0 auto;
  }
}

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

.controller {
  display: flex;
}

.game-button-run {
  background-image: url("@/assets/button.png");
  background-size: cover;
  font-size: 1.5em;
  position: absolute;
  right: 10px;
  top: -5px;
  height: 80px;
  width: 155px;
  border: 0;
  background-color: transparent;
  margin: 0;
  padding: 0;
}

.game-button-run-shadow {
  position: absolute;
  right: 10px;
  top: -5px;
  height: 80px;
  width: 150px;
  box-shadow: -5px 5px 10px black;
}

.game-button-run:hover {
  /* TODO */
}

.game-button-run:disabled {
  /* TODO */
}

.game-input {
  position: relative;
  top: 14px;
  left: 25px;
  height: 50px;
  width: calc(100% - 150px - 50px);
}

.game-input-run-left {
  height: 50px;
  width: 25px;
  position: absolute;
  left: 0px;
  background-image: url("@/assets/input_left.png");
  background-size: cover;
}

.game-input-run {
  height: 100%;
  width: 100%;
  margin-left: 15px;
  padding-left: 12px;
  background-image: url("@/assets/input_center.png");
  background-size: cover;
  border: 0;
  font-size: 18px;
  font-family: a_OldTyper;
}

.game-input-run::placeholder {
  color: #333333;
}

.game-input-run:focus {
  border: 0;
  outline: none;
}

.controller-metal {
  width: 30px;
  height: calc(100% + 2px);
  position: absolute;
  top: -1px;
}

.controller-metal-left {
  left: -15px;
}

.controller-metal-right {
  right: -15px;
}
</style>
