<script setup lang="ts">
import type { Action } from './models';
import { apiLetsgo } from './client';

interface Props {
  action: Action
  gameState: string
  login: string
  password: string
  index: number
  count: number
}
const props = withDefaults(defineProps<Props>(), {})

const isPlaceFilled = defineModel<boolean>();

function clickCollapse() {
  // eslint-disable-next-line vue/no-mutating-props
  props.action.isOpen = !props.action.isOpen
}

async function letsgo(place: string) {
  isPlaceFilled.value = true
  await apiLetsgo(props.login, props.password, place)
}
</script>

<template>
  <div class="message-cloud">
    <div class="message-header">
      {{ props.action.place }}: {{ props.action.name }}
      <span class="collapse-icon" @click="clickCollapse">{{ props.action.isOpen ? '−' : '+' }}</span>
    </div>
    <div v-show="props.action.isOpen">
      <hr class="hr" />
      <div class="message-content">
        <div v-if="props.action.image.length">
          <div class="message-image-border">
            <img v-bind:src="props.action.image" class="message-image" />
          </div>
        </div>{{ props.action.text }}
      </div>
      <div v-if="index == count - 1">
        <hr class="hr" v-if="props.action.buttons?.length" />
        <div v-for="door in props.action.buttons" :key="door.code" class="button-dialog" v-on:click="letsgo(door.code)"
          :disabled="gameState !== 'RUN' || !door.show">
          <div class="button-dialog-text">
            {{ door.name }}
          </div>
        </div>
      </div>
      <hr class="hr" v-if="props.action.applications.length" />
      <div class="message-footer" v-for="application in props.action.applications" :key="application.name">
        Приложение: {{ application.name }}
        <div class="application-label">{{ application.number }}</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.hr {
  margin: 10px 0;
  border: dashed 1px;
  border-color: black;
}

.message-cloud {
  /* border-radius: 15px; */
  margin: 12px 10px;
  padding: 16px;
  display: flow-root;
  background-image: url("@/assets/paper.jpg");
  background-size: cover;
  font-family: main;
  color: black;
  line-height: 20px;
  font-size: 18px;
  font-weight: 900;
}

.message-header {
  font-size: 24px;
}

.collapse-icon {
  float: right;
  padding: 0 15px;
  cursor: pointer;
}

.message-content {
  font-weight: 500;
  white-space: pre-wrap;
}

.message-image-border {
  width: 40%;
  float: left;
  padding: 7px;
  margin-right: 15px;
  background-image: url("@/assets/paper_white.jpg");
  background-size: cover;
  box-shadow: 0px 3px 15px rgb(98, 98, 98);
  transform: rotate(-3deg);
}

.message-image {
  width: 100%;
}

.button-dialog {
  display: inline-block;
  font-weight: 600;
  border-radius: 5px;
  font-size: 16px;
  margin-top: 10px;
  background-image: url("@/assets/belt.png");
  background-size: cover;
  color: #bfa07d;
}

.button-dialog-text {
  padding: 5px 12px;
}

.message-footer {
  font-weight: 400;
  color: var(--second-color);
  position: relative;
}

.application-label {
  background-image: url("@/assets/label.png");
  background-size: cover;
  width: 30px;
  height: 52px;
  text-align: center;
  padding-top: 22px;
  color: black;
  position: absolute;
  bottom: -10px;
  right: 5px;
  transform: rotate(9deg);
  font-size: 30px;
  font-family: sans-serif;
}
</style>
