<script setup>
    import CloseIcon from '../assets/icons/close.svg'
    import LikeIcon from '../assets/icons/like.svg'
</script>

<template>
  <div class="windowStyle" id="msgVisualizer" ref="msgPannel" @mousedown="$emit('focus')" @touchstart="$emit('focus')">
    <Moveable
        className="moveable"
        :target="$refs.msgPannel"
        :draggable="true"
        @drag="onDrag"
    />
    <div class="windowTitle">
      <p>
        <b class="windowTitleLower">Message du {{selectedMsg.date}}</b>
        <br>
        {{selectedMsg.title}}
      </p>
      <!-- touchstart.capture pour passer en prio sur déplacement fenêtre/ .stop si pas de method-->
      <button type="button" class="closeBtn" @mousedown.stop @touchstart.capture="closeMsg" @click="closeMsg" data-tooltip="fermer">
        <CloseIcon name="close" class="icon"/>
      </button>
    </div>
    <div class="windowContent" id="msgContent">
      <div class="reactBar">
        <p class="reactStatStyle">{{selectedMsg.like}}</p>
        <button data-tooltip="j'aime bien" type="button" class="iconBtnStyle" :class="{reactedStyle: selectedMsg.isLiked}" @mousedown.capture="likeMsg" @touchstart.capture="likeMsg">
          <LikeIcon name="test" class="icon"/>
        </button>
      </div>
      <p id="selectedMsgText" @touchstart.stop>{{selectedMsg.content}}</p>
    </div>
  </div>
</template>

<style scoped>
  /*================= Mise en page:
    => Mobile First : par défaut, moins de 500px
    => Tablette et PC format haut : de 500 à 1000px
    => PC large : à partir de 1000px
  */

  /*+++++++++++++++++ COPYBOX
      ================ PC HAUT/IPAD
      @media(min-width:650px){}
      ================ PC LARGE
      @media(min-width:1300px){}
  */
  #msgVisualizer{
    position: fixed;
    top:50%;
    left:3.33%;
    width: 400px;
    height: 400px;
  }

  #msgContent{
    width: 95%;
    height: 100%;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    overflow-y: scroll;
  }

  /*================ PC LARGE*/
  @media(min-width:1000px){
    #msgVisualizer{
      left: 45%;
      top: 33.3%;
    }
  }

  #selectedMsgText{
    white-space: pre-line;
    font-size: 16.1px;
    font-family: 'velvelyne';
    color: var(--main-color);
    font-weight: bold;
    padding-right: 16.1px;
    padding-left: 7.77px;
    margin-top: 0;
    height: 77%;
    pointer-events: all;
    overflow-y: scroll;
  }
</style>

<script>
  import Moveable from 'vue3-moveable';
  import { dataStorage } from '../dataExchange.js'

  export default {
    name : 'MessagePannel',
    components:{
      Moveable
    },
    data(){
      return{
        target: null
      }
    },
    emits: ['close','focus'],
    computed:{
      selectedMsg(){
        return dataStorage.selectedMsg
      }
    },
    methods:{
      onDrag({ target, transform }) {
        target.style.transform = transform;
      },
      likeMsg(){
        dataStorage.selectedMsg.isLiked = !dataStorage.selectedMsg.isLiked
      },
      closeMsg(){
        this.$emit('close');
        dataStorage.selectedMsg.isSelected = false;
      }
    },
    mounted(){
      this.target = this.$refs.msgPannel;
      console.log("Message pannel is loaded!");
    }
  };
</script>
