<template>
  <div class="storie__wrapper" v-if="storie.image">
    <div class="storie__close" @click="close()">×</div>
    <div class="storie__prev" @click="prev()"></div>
    <div class="storie__next" @click="nextStorie()"></div>
    <div
      class="storie__bg-image"
      :style="'background-image:url(' + storie.image.url + ')'"
    ></div>
    <div
      class="storie__progress"
      role="progressbar"
      :aria-valuenow="progress"
      aria-valuemin="0"
      aria-valuemax="100"
      :style="{ width: `${progress}%` }"
    ></div>
    <div class="storie__progress-line"></div>
    <div class="storie__info">
      <div class="storie__logo">
        <BaseIcon :name="'amic-logo'" />
      </div>
      <div class="storie__title">
        <span><a @click="read" v-html="storie.title"></a></span>
        <button @click="read" class="storie__news-link">
          Читать полностью
        </button>
      </div>
    </div>
    <NewsBackgroundImage class="storie__content" :news="storie" />
    <div class="storie__comments">
      <div class="storie__comments-wrapper">
        <div
          v-for="comment in comments.comments"
          :key="comment.id"
          class="storie__comment"
        >
          <div class="detail-comments__item">
            <div class="detail-comments-item">
              <div class="detail-comments-item__text">
                {{ comment.text }}
              </div>
              <span> {{ $convertDate(comment.create) }} </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  props: {
    num: {
      type: Number,
    },
  },
  data() {
    return {
      storie: {},
      comments: [],
      progress: 0,
      completed: false,
      tempo: 50,
      storieIndex: this.num,
    };
  },
  computed: {
    ...mapGetters({
      stories: "modules/news/stories",
    }),
    id() {
      return this.stories[this.num].id;
    },
  },
  methods: {
    close() {
      this.progress = 0;
      this.completed = true;
      this.$modal.hide('storie')
    },
    read() {
      this.progress = 0;
      this.completed = true;
      this.$modal.hide("storie");
      this.$router.push({
        name: "news-category-alias",
        params: {
          category: this.storie.category.alias,
          alias: this.storie.alias,
        },
      });
    },
    convertDate(dateTampstamp) {
      const date = new Date(dateTampstamp * 1000);
      const dateString =
        ("0" + date.getHours()).slice(-2) +
        ":" +
        ("0" + date.getMinutes()).slice(-2) +
        ", " +
        ("0" + date.getDate()).slice(-2) +
        " " +
        date.toLocaleString("ru", { month: "long" }) +
        " " +
        date.getFullYear();
      return dateString;
    },
    timer(tempo) {
      let setIntervalRef = setInterval(() => {
        if (!this.completed) {
          this.progress++;
        }

        if (this.progress == 100) {
          clearInterval(setIntervalRef);
          this.completed = true;
          if (this.storieIndex + 1 < this.stories.length) {
            this.nextStorie(true);
          }
        }
      }, tempo);
    },
    prev() {
      if (this.storieIndex !== 0){
        this.progress = 0;
        this.storieIndex--;
        this.storie = this.stories[this.storieIndex];
        this.$axios
        .get("comments/pull_" + this.storie.comment_pull_id)
        .then((res) => {
          this.comments = res.data.data;
          if (this.completed) {
            this.completed = false;
            this.timer(this.tempo);
          }
        });
      }
    },
    nextStorie(repeat) {
      if (this.storieIndex + 1 < this.stories.length) {
        this.progress = 0;
        this.completed = false;
      if (this.storieIndex + 1 < this.stories.length) {
        this.storieIndex++;
      }
      this.storie = this.stories[this.storieIndex];
      this.$axios
        .get("comments/pull_" + this.storie.comment_pull_id)
        .then((res) => {
          this.comments = res.data.data;
          if (repeat) {
            this.timer(this.tempo);
          }
        });
    }},
  },
  async fetch() {
    this.storie = this.stories[this.storieIndex];
    await this.$axios
      .get("comments/pull_" + this.storie.comment_pull_id)
      .then((res) => {
        this.comments = res.data.data;
        this.timer(this.tempo);
      });
  },
  updated() {},
};
</script>

<style lang="less">
.storie {
  &__prev {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 200px;
    content: "";
    z-index: 44;
  }
  &__next {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 200px;
    content: "";
    z-index: 44;
  }
}
.vm--modal {
  border-radius: none;
}
.storie {
  &__logo {
    position: absolute;
    left: 10%;
    top: 5%;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: white;
    text-align: center;
    padding-top: 35px;
    z-index:1;
    @media (max-width: 576px) {
      display: none;
    }
  }
  &__modal {
    background: grey;
  }
  &__news-link {
    display: block;
    font-size: 16px;
    color: white;
    padding: 20px 0;
  }
  &__wrapper {
    max-height: 100vh;
    overflow: hidden;
    height: 100%;
    display: flex;
    &::before {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      content: "";
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 1;
    }
    @media (max-width: 576px) {
      flex-direction: column;
    }
  }
  &__bg-image {
    filter: blur(8px);
    background-size: cover;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    content: "";
    z-index: 0;
    background-color: black;
  }
  &__content {
    max-height: 100%;
    height: 90%;
    flex: 0 1 40%;
    background-size: contain;
    background-position: 50%;
    z-index: 3;
    position: relative;
    margin: auto 0;
    background-color: black;
    border-radius: 20px;
    @media (max-width: 576px) {
      border-radius: 0;
      background-color: transparent;
      background-size: cover;
    }
  }
  &__info {
    flex: 0 1 30%;
    position: relative;
    color: white;
  }
  &__title {
    position: relative;
z-index: 99999;
  }
  &__comment {
    margin-bottom: 20px;
  }
  &__comments {
    flex: 0 1 30%;
    z-index: 3;
    position: relative;
    max-width: 80%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    font-size: 16px;
    padding: 0 5%;
    &-wrapper {
      overflow-y: scroll;
      &::-webkit-scrollbar {
          width: 0px;
          background: transparent;
      }
    }
    .detail-comments-item__text {
      color: white;
      font-size: 14px;
    }
    .detail-comments-item span {
      font-size: 12px;
      color: white;
      opacity: 0.5;
    }
    .detail-comments__item::marker {
      display: none;
    }
    @media (max-width: 576px) {
      overflow-y: scroll;
      z-index: 45;
      &-wrapper {
        overflow-y: unset;
      }
    }
  }
  &__info {
    display: flex;
    align-items: center;
    @media (max-width: 576px) {
      display: block;
      max-height: 20%;
    }
  }
  &__title {
    font-size: 28px;
    font-weight: bold;
    max-width: 85%;
    display: block;
    margin: 20px auto;
    padding-bottom: 30%;
    line-height: 130%;
    span {
      background: #000;
      padding: 2px;
      box-shadow: 10px 0 0 black, -10px 0 0 black;
      a{
        color: white;
        cursor: pointer;
      }
    }
  }
  @media (max-width: 576px) {
    &__title {
      font-size: 18px;
      margin: 20px 60px 20px 20px;
    }
  }
  &__comments {
    font-size: 20px;
    max-width: 80%;
    margin: auto;
    height: 90%;
    max-height: 100%;
  }
  &__progress {
    position: absolute;
    top: 0;
    left: 0;
    width: 0%;
    height: 5px;
    content: "";
    background-color: #f6911f;
    z-index: 11;
    border-radius: 0 4px 4px 0;
  }
  &__progress-line {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 5px;
    content: "";
    background-color: transparent;
    z-index: 10;
    border-radius: 0 4px 4px 0;
  }
  &__close {
    position: absolute;
    content: "×";
    font-weight: 100;
    width: 55px;
    z-index: 523455435345;
    height: 55px;
    text-align: center;
    font-size: 55px;
    font-family: Arial, sans-serif;
    right: 3%;
    color: white;
    top: 5%;
    cursor: pointer;
    opacity: .2;
    &:hover {
      opacity: .4;
    }
    @media (max-width: 576px) {
      top: 0;
      right: 0;
    }
  }
}
</style>
