<template>
  <transition name="notice_an">
    <div class="notice__content"
         :title="msg.msg"
         v-show="isShow"
         @mouseout="mouseEvent(false)"
         @mouseover="mouseEvent(true)">
      <div class="notice__img">
        <img :src="msg.avatar">
      </div>
      <div class="notice__main">
        <div class="notice__name">
        <span style="color: #E6A23C">
          {{ msg.title }}
        </span>
          <br>
          <span style="color: red">
          {{ msg.deptName }}
        </span>
          <span style="color: #0a84fd;margin-left: 3px">
         {{ msg.name }}
        </span>
        </div>
        <div class="notice__msg">
          {{ msg.msg }}
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup name='Notice' lang="ts">
import {onMounted, ref} from 'vue'
import sleep from "@/utils/sleep";
import {setCallback} from '@/utils/websocket'
import store from '@/store'
import {isDev} from '@/utils/public'

const isShow = ref(false)
const displaySeconds = 1000 * 2
const isMouse = ref(false)

const msg = ref({
  avatar: '',
  deptName: '',
  name: '',
  msg: '',
  title: '',
  countDown: 2,
})

let queue = new Queue()
const fillData = async () => {
  let temp = queue.poll()
  if (temp === null) {
    return
  }
  isShow.value = true
  msg.value = temp;
  // 显示时间
  await sleep(displaySeconds);
  // 鼠标移入等待
  do {
    await sleep(100)
  } while (isMouse.value)
  // 关闭
  isShow.value = false;
  // 等待关闭动画结束
  await sleep(500);
  // 如果还有数据就执行自己，没有数据的结束
  if (queue.size() > 0) {
    await fillData()
  } else {
    open = true
  }
}

const mouseEvent = (flag) => {
  isMouse.value = flag
}

const test = () => {
  if (isDev) {
    let value = {
      avatar: 'asd',
      deptName: '测试',
      name: '测试发送',
      msg: '阿三发射点',
      title: 'title',
      countDown: 2,
    }
    queue.enqueue(value)
    fillData()
  }
}

// 是否开启显示通知
let open = true
onMounted(() => {
  // test()
  setCallback('avatarNotification', async (value) => {
    let newCount = store.state.app.unreadMessageCount + value.count
    store.commit('app/setUnreadMessageCount', newCount)
    // 把消息放到队列中
    queue.enqueue(value)
    if (open) {
      open = false
      await fillData()
    }
  })
})

function Queue() {

  let items = []

  Queue.prototype.enqueue = element => {
    items.push(element)
  }

  Queue.prototype.poll = () => {
    if (items.length === 0) {
      return null;
    }
    let temp = items[0];
    items.shift();
    return temp;
  }

  Queue.prototype.getElement = () => {
    return items;
  }

  Queue.prototype.size = () => {
    return items.length;
  }

}

</script>

<style scoped lang="scss">
$height: 70px;
$width: 290px;
$img_size: 50px;

.notice__content {
  position: fixed;
  width: $width;
  height: $height;
  left: 50%;
  top: 2%;
  margin-left: (-$width/2);
  border-radius: 5px;
  display: flex;
  align-items: center;
  background-color: white;
  z-index: 99;
  box-shadow: var(--el-box-shadow-dark);


  .notice__img {
    width: $img_size;
    height: $img_size;
    margin-left: 10px;

    img {
      width: $img_size;
      height: $img_size;
    }
  }

  .notice__main {
    width: $width - $img_size - 10 - 20;
    padding: 0 10px;
    height: 50px;

    .notice__name {
      width: 100%;
    }

    .notice__msg {
      margin-top: 7px;
      width: 100%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

  }

}

.notice_an-leave-active {
  animation: notice .2s reverse;
}

.notice_an-enter-active {
  animation: notice .2s;
}


@keyframes notice {
  0% {
    top: 0;
    opacity: 0;
  }
  100% {
    top: 2%;
    opacity: 1;
  }

}


</style>
