<!--
// Sshwifty - A Web SSH client
//
// Copyright (C) 2019 Rui NI <nirui@gmx.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->

<template>
  <main style="position: relative">
    <slot v-if="screens.length <= 0"></slot>

    <div
      v-for="(screenInfo, idx) in screens"
      v-if="screens.length > 0"
      :key="screenInfo.id"
      :style="'visibility: ' + (screen === idx ? 'visible' : 'hidden')"
      class="screen"
      style="top: 0; right: 0; left: 0; bottom: 0; padding： 0; margin: 0; overflow: auto; position: absolute;"
    >
      <div v-if="screenInfo.indicator.error.length > 0" class="screen-error">
        {{ screenInfo.indicator.error }}
      </div>

      <div class="screen-screen" style="position: relative">
        <component
          :is="getComponent(screenInfo.control.ui())"
          :active="screen === idx"
          :control="screenInfo.control"
          :change="screenInfo.indicator"
          @stopped="stopped(idx, $event)"
          @updated="updated(idx)"
        ></component>
      </div>
    </div>
  </main>
</template>

<script>
import ConsoleScreen from "./screen_console.vue";

export default {
  components: {
    ConsoleScreen
  },
  props: {
    screen: {
      type: Number,
      default: 0
    },
    screens: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    getComponent(ui) {
      switch (ui) {
        case "Console":
          return "ConsoleScreen";

        default:
          throw new Error("Unknown UI: " + ui);
      }
    },
    stopped(index, stopErr) {
      this.$emit("stopped", index, stopErr);
    },
    updated(index) {
      this.$emit("updated", index);
    }
  }
};
</script>
