<template>
  <main class="landing-page">
   
    <div class="container-fluid episode-intro">
          <Header></Header>

      <!-- menu wrapper -->
    <div class="intro-startup">
      <div class="smasung-g-10wrp">
        <div class="top-area-blank"></div>
           <div class="samsung-compare-c">
          <div class="logo-1"><img :src="currentSlideData.payload.metaData.mobileImage" /></div>
           
          </div>
           <!-- <div class="samsung-compare-c">
          <div class="logo-1"><img src="../assets/images/logo-1.png" /></div>
           <div class="vs">vs</div>
            <div class="logo-2"><img src="../assets/images/logo-2.png" /></div>
          </div> -->
              <div class="comment-box">
          <div class="comment-box-s-1">
          <!-- <p>👋 Hi there! My name is <strong> Rashmi</strong>, and these are a few observations on the user workflows that I had while using the WhiteHat Jr. app.</p> -->
          <p>{{currentSlideData.payload.metaData.textBox}}</p>
         <div class="footer">
           <img src="../assets/images/comment-user.svg" />  <a href="javascript:void(0);" @click="goToProfile">View My Profile</a>
           </div><!-- footer -->
          </div><!-- comment box 1 -->
              <!-- <div class="comment-box-s-1 comment-w-397">
          <p>
            Use arrow keys to navigate <br/>
<img src="../assets/images/key-arrow.jpg" class="key-arrow" />
          </p>
          </div> -->
          <!-- comment box 1 -->
          <div class="user-photo">
            <!-- <img src="../assets/images/comment-photo.png" /> -->
            <img :src="currentSlideData.payload.metaData.authorImage" />
          </div>
          </div><!-- comment box -->
        </div><!-- samsung g 10 -->
      <div class="footer-nav">
        <div class="footer-top">
              <ul class="top-intro-bt ps_right">
            <li><a href="javascript:void(0);" @click="goBack"><img src="../assets/images/skip-prev.svg" > Prev</a></li>
             <li><a href="javascript:void(0);" @click="goNext"><img src="../assets/images/skip-next.svg" > Skip to next slide</a></li>
          </ul>
          </div><!-- footer top -->
        <div class="footer-bottom">
          <ul>
            <li></li>
             <li></li>
          </ul>
          </div><!-- footer top -->
        </div><!-- footer -->
    
    </div>
      <!-- body wrapper -->
    </div>
    <!-- main wrapper -->
  </main>
</template>

<script>
import Vue from "vue";
import router from "../router";
import Header from "./Header";
import axios from "axios";

export default {
  components: {
    Header: Header,
  },
  name: "AuthorIntro",

  data() {
    
    return {
      allSlide:[],
      currentSlideIndex:null,
      currentSlideData:null,
    };
  },
  mounted() {
    var allSlideData = localStorage.getItem("spotlight_slide"+this.$route.params.caseStudyId);
     if (allSlideData) {
      this.allSlide = JSON.parse(allSlideData);
      this.getCurrentSlideData();
    }else{
      this.generatecaseStudies();
    }

  },
  methods: {
 generatecaseStudies(){
      axios
        .get("/caseStudy?caseStudyId="+this.$route.params.caseStudyId, {
          headers: {
            Authorization: "Bearer " + this.usertoken,
          },
        })
        .then((response) => {
         
          console.log('response',response.data.data);
          this.openStudy(response.data.data);
        })
        .catch((error) => console.log(error));
    },



  openStudy(payload) {
      console.log("payload-", payload);
      payload.intro.date = payload.createdAt;
      payload.intro.focusPoint = payload.focusAreas;
      axios
        .get("/caseStudy/slides?caseStudyId=" + payload._id, {
          headers: {
            Authorization: "Bearer " + this.usertoken,
          },
        })
        .then((response) => {
          this.createSlide(payload, response.data.data);
        })
        .catch((error) => console.log(error));
    },

    createSlide(payload, slides) {
      var finalSlides = [];
      slides.forEach((slides_) => {
        var url = this.assignRoutes(slides_.templateId);
        var obj = {
          forward: true,
          backward: true,
          ur: url,
          slideId: slides_._id,
          caseStudyId: slides_.caseStudyId,
          payload: {
            metaData: slides_.metaData,
            comments: slides_.comments,
            insight: slides_.insight ? slides_.insight : null,
          },
        };
        // slides_
        finalSlides.push(obj);
      });
      console.log("payload", payload);
      // add first slide at begining
      finalSlides.unshift({
        forward: true,
        backward: false,
        ur: "EpisodeIntro",
        slideId: "INTRO_oqkdMOVDrwRptsdWJ6Ye",
        caseStudyId: payload._id,
        payload: {
          metaData: payload.intro,
          comments: [],
        },
      });
      finalSlides.push({
        forward: true,
        backward: false,
        ur: "Outro",
        slideId: "OUTRO_oqkdMOVDrwRptsdWJ6Ye",
        caseStudyId: payload._id,
        payload: {
          metaData: payload.outro,
          comments: [],
        },
      });

      console.log(finalSlides);
      console.log("payload", payload);
      localStorage.setItem(
        "spotlight_slide" + payload._id,
        JSON.stringify(finalSlides)
      );
       this.allSlide = finalSlides;
      this.getCurrentSlideData();
    },
    assignRoutes(tempId) {
      // /episode-intro
      // /outro
      var routes = [
        {
          url: "AuthorIntro",
          tempId: "T1_RoeMG8130Xko1DvhC3Ou",
        },
        {
          url: "NoScreenshotSingleAuthor",
          tempId: "T2_ROsUOEy3vxsRAiQ72XdI",
        },
        {
          url: "SingleMobileScreenInsightTwo",
          tempId: "T3_cqNIf7tuqL4jyON63dA7",
        },
        {
          url: "TwoScreenWithoutInsight",
          tempId: "T4_4QC8W7kIYnJtZ26Jt0Go",
        },
        {
          url: "noscreenshotSingleautho",
          tempId: "T5_za3c3sYgx7bVvtKz5r0e",
        },
        {
          url: "SingleMobileScreenInsightOne",
          tempId: "T6_za3c3sYgx7bVvtKz5sgf",
        },
        {
          url: "TwoScreenWithInsight",
          tempId: "T7_za3c3sYgx7bVvtKzasdf",
        },
        {
          url: "AuthorReadingNow",
          tempId: "T8_zb4d4fYgx7bVvtKzasdf",
        },
        {
          url: "AuthorReadingBreak",
          tempId: "T9_zb3e4fYgy7dVvfKdasdf",
        },
      ];
      var i = routes.findIndex((routes_) => routes_.tempId == tempId);
      return routes[i].url;
    },


    getCurrentSlideData(){
      var i = this.allSlide.findIndex((slide_) => slide_.slideId == this.$route.params.slideId);
      this.currentSlideIndex = i; 
      this.currentSlideData = this.allSlide[i]
      console.log("currentSlideData",this.currentSlideData)

    },
    goNext(){
      this.currentSlideIndex++
      this.$router.push({
        name: this.allSlide[this.currentSlideIndex].ur,
        params: {
          caseStudyId: this.allSlide[this.currentSlideIndex].caseStudyId,
          slideId: this.allSlide[this.currentSlideIndex].slideId,
        },
      });

    },
    goBack(){
       this.currentSlideIndex--
      this.$router.push({
        name: this.allSlide[this.currentSlideIndex].ur,
        params: {
          caseStudyId: this.allSlide[this.currentSlideIndex].caseStudyId,
          slideId: this.allSlide[this.currentSlideIndex].slideId,
        },
      });

    },
    goToLogin() {
      this.$router.push("/login");
    },
    goToSignUp() {
      this.$router.push("/");
    },
    goToProfile() {
      this.$router.push("/profile");
    },
    
  },
};
</script>
