<template>
  <div class="more-box">
    <div class="barrage" v-if="barrageInfo.status == 1" @click="_setBarrage">
      <div class="icon-show" v-if="showBarrage"></div>
      <div class="icon-hide" v-else></div>
    </div>
    <div class="back" v-if="showBack" @click="_backPlay">
      <div class="icon"></div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex';
export default {
  name: 'pc-player-more',
  props: {
    mps: {}
  },
  data() {
    return {
      showBarrage: true
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      playerInfo: 'playerInfo'
    }),
    barrageInfo() {
      return this.channelInfo.barrageInfo;
    },
    showBack() {
      return this.playerInfo.playUrl !== this.playerInfo.saveUrl;
    }
  },
  mounted() {
    this.$Bus.$on('bus-sendBarrage', this._sendBarrage);
  },
  methods: {
    ...mapMutations({
      set_playerInfo: 'set_playerInfo'
    }),
    _backPlay() {
      const info = JSON.parse(JSON.stringify(this.playerInfo));
      info.playUrl = info.saveUrl;
      info.cameraId = '';
      this.set_playerInfo(info);
    },
    _setBarrage() {
      this.showBarrage = !this.showBarrage;
      if (!this.showBarrage && this.mps) {
        this.mps.removelement();
      }
    },
    makeColor() {
      const colorArray = '0123456789abcdef';
      let color = '';
      for (let i = 0; i < 6; i++) {
        color += colorArray[Math.floor(Math.random() * 16)];
      }
      return color;
    },
    _sendBarrage(str) {
      const content = str.replace(/(#)[\u4e00-\u9fa5A-Za-z0-9.]+(#)/gi, '');
      if (!this.mps || !content || !this.showBarrage || this.barrageInfo.status !== 1) {
        return false;
      }
      const fontColor = this.barrageInfo.fontColor || 'FFFFFF';
      const fontSize = this.barrageInfo.fontSize || 2;
      const fontSpeed = this.barrageInfo.speed || 2;
      const color = fontColor === 'random' ? this.makeColor() : fontColor;
      const sizeArr = { '1': 16, '2': 20, '3': 32 };
      const speedArr = { '1': 100, '2': 50, '3': 20 };
      const size = sizeArr[fontSize] || 16;
      const speed = speedArr[fontSpeed] || 50;
      this.mps.sendbarrage2(content, size, '#' + color, speed, '', '微软雅黑');
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-sendBarrage', this._sendBarrage);
  }
};
</script>
<style lang="less" scoped>
.more-box {
  .barrage {
    margin-top: 10px;
    width: 40px;
    height: 40px;
    border-radius: 100%;
    background: rgba(0, 0, 0, 0.5);
    cursor: pointer;
    .icon-show {
      width: 100%;
      height: 100%;
      background: url(./img/icon-barrage.png) no-repeat center center;
      background-size: 50% auto;
    }
    .icon-hide {
      width: 100%;
      height: 100%;
      background: url(./img/icon-barrage-no.png) no-repeat center center;
      background-size: 50% auto;
    }
  }
  .back {
    margin-top: 10px;
    width: 40px;
    height: 40px;
    border-radius: 100%;
    background: rgba(0, 0, 0, 0.5);
    cursor: pointer;
    .icon {
      width: 100%;
      height: 100%;
      background: url(./img/icon-back.png) no-repeat center center;
      background-size: 50% auto;
    }
  }
}
</style>
