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

interface Props {
  action: Action
  gameState: string
  login: string
  password: string
}
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">
          <img v-bind:src="props.action.image" class="message-image" />
        </div>{{ props.action.text }}
      </div>
      <hr class="hr" v-if="props.action.buttons?.length" />
      <button v-for="door in props.action.buttons" :key="door.code" class="button-dialog" v-on:click="letsgo(door.code)"
        :disabled="gameState !== 'RUN' || !door.show">
        {{ door.name }}
      </button>
      <hr class="hr" v-if="props.action.applications.length" />
      <div class="message-footer" v-for="application in props.action.applications" :key="application.name">
        {{ application.number }} Приложение: {{ application.name }}
      </div>
    </div>
  </div>
</template>

<style scoped>
.hr {
  margin: 7px 0;
}

.message-cloud {
  border: 1px solid #444444;
  border-radius: 15px;
  margin: 12px 10px;
  padding: 16px;
  display: flow-root;
  background-image: url("@/assets/paper.jpg");
  background-size: cover;
}

.message-header {
  font-size: large;
  font-weight: 200;
}

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

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

.message-image {
  width: 40%;
  float: left;
  margin-right: 15px;
}

.button-dialog {
  background-color: var(--main-color);
  font-weight: 600;
  color: white;

  padding: 6px 14px;
  border: 1px solid #ddd;
  border-radius: 15px;
  font-size: 16px;
  margin-right: 6px;
}

.button-dialog:hover {
  background-color: var(--main-color);
  opacity: 0.9;
}

.button-dialog:disabled {
  opacity: 0.5;
}

.message-footer {
  font-weight: 400;
  color: var(--second-color);
}
</style>
