<template>
  <transition name="image-viewer">
    <div id="image" v-show="modalOpen" @click="close" :aria-hidden="!modalOpen" aria-modal="true" role="dialog">
      <div class="image-wrapper">
        <div class="image-header">
          <el-button type="text" @click="close" class="close-button">
            <font-awesome-icon icon="xmark" />
          </el-button>
        </div>
        <div class="image-content" role="presentation">
          <span class="button-area"
            ><el-button type="text"> <font-awesome-icon icon="arrow-left" /> </el-button
          ></span>
          <Media :src="imageURL" :type="imageType"></Media>
          <span class="button-area"
            ><el-button type="text"> <font-awesome-icon icon="arrow-right" /> </el-button
          ></span>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import Media from './Media'
import { mapState } from 'vuex'

export default {
  components: {
    Media
  },
  name: 'image-viewer',
  computed: {
    ...mapState({
      modalOpen: state => state.TimelineSpace.Modals.ImageViewer.modalOpen
    }),
    imageURL() {
      return this.$store.getters['TimelineSpace/Modals/ImageViewer/imageURL']
    },
    imageType() {
      return this.$store.getters['TimelineSpace/Modals/ImageViewer/imageType']
    },
    showLeft() {
      return this.$store.getters['TimelineSpace/Modals/ImageViewer/showLeft']
    },
    showRight() {
      return this.$store.getters['TimelineSpace/Modals/ImageViewer/showRight']
    }
  },
  methods: {
    close() {
      this.$store.dispatch('TimelineSpace/Modals/ImageViewer/closeModal')
    },
    decrementIndex() {
      if (this.showLeft) this.$store.dispatch('TimelineSpace/Modals/ImageViewer/decrementIndex')
    },
    incrementIndex() {
      if (this.showRight) this.$store.dispatch('TimelineSpace/Modals/ImageViewer/incrementIndex')
    },
    closeHandle(event) {
      switch (event.srcKey) {
        case 'close':
          this.close()
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.image-wrapper {
  background-color: rgba(0, 0, 0, 0.7);
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  outline: 0;

  .image-header {
    width: 100%;
    text-align: right;
    padding: 8px 8px 0 0;
    color: #409eff;
    box-sizing: border-box;
    position: fixed;

    .close-button {
      font-size: 24px;
    }
  }

  .image-content {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.image-viewer-enter-active,
.image-viewer-leave-active {
  transition: opacity 0.5s;
}
.image-viewer-enter,
.image-viewer-leave-to {
  opacity: 0;
}
.button-area {
  display: inline-block;
  width: 52px;
  height: 77px;
  i {
    font-size: 50px;
  }
}
</style>
