<template>
  <div class="danmaku-area">
    <div
      class="danmaku-container"
      v-chat-scroll
      v-if="isShowList"
    >
      <!-- <ul class=""> -->
      <transition-group  appear tag="ul" class="danmaku-list" name="dm">
        <li
          class="danmaku-list-item"
          v-for="(item) in showDanmakuData"
          :key="item"
        >
          <span> {{ item }}</span>
        </li>
      </transition-group>
    </div>
    <div class="input-container" v-if="!isMobile">
      <span class="send-choices" @click.stop="toggleSelectMenu"
        >请选择弹幕内发送吧~</span
      >
      <div class="send-btn-container">
        <img
          @click="hideList"
          class="show-icon"
          :src="showIcon || ''"
          v-if="isShowList"
        />
        <img
          @click="showList"
          class="close-icon"
          :src="closeIcon || ''"
          v-if="!isShowList"
        />
        <button class="send-btn primary">发送</button>
      </div>
      <ul class="show-list" v-show="isShowSelectList">
        <li
          class="list-item"
          v-for="(item, key) in quotes"
          :key="key"
          @click="sendDanmaku(key)"
        >
          {{ item }}
        </li>
      </ul>
    </div>
    <div class="input-mobile" v-else>
      <span class="send-choices" @click.stop="toggleSelectMenu">请选择弹幕内发送吧~</span>
      <div class="send-btn-container">
        <img
          @click="hideList"
          class="show-icon"
          :src="showIcon || ''"
          v-if="isShowList"
        />
        <img
          @click="showList"
          class="close-icon"
          :src="closeIcon || ''"
          v-if="!isShowList"
        />
        <!-- <button class="send-btn">发送</button> -->
      </div>
      <ul class="show-list" v-show="isShowSelectList">
        <li
          class="list-item"
          v-for="(item, key) in quotes"
          :key="key"
          @click="sendDanmaku(key)"
        >
          {{ item }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "danmaku",
  props: {
    quotes: {
      type: Array,
      default: function() {
        return [
        ];
      },
    },
    showIcon: {
      type: String,
      required: true,
    },
    closeIcon: {
      type: String,
      required: true,
    },
    arrowIcon: {
      type: String,
      required: true,
    },
    limit: {
      type: Number,
      required: false,
      default: 5000,
    },
  },
  watch: {
    isNotInputAction: function() {},
  },

  data() {
    return {
      showDanmakuData: [],
      isShowList: true,
      isShowSelectList: false,
      isNotInputAction: false,
      timer: null,
      autoIndex: 0,
      isMobile: /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)
    };
  },
  methods: {
    toggleSelectMenu() {
      this.isShowSelectList = !this.isShowSelectList;
    },
    showList() {
      this.isShowList = true;
    },
    hideList() {
      this.isShowList = false;
    },
    sendDanmaku(index) {
      const text = this.quotes[index];
      if (this.showDanmakuData.length <= this.limit) {
        this.showDanmakuData.push(text);
      }
      this.isShowSelectList = false;
      this.isNotInputAction = true;
    },
    autoPopAnimation() {
      if (!this.isNotInputAction) {
        this.timer = setInterval(() => {
          if (this.autoIndex <= this.quotes.length) {
            if (this.quotes[this.autoIndex]) {
              this.showDanmakuData.push(this.quotes[this.autoIndex]);
              this.autoIndex++;
            } else {
              clearInterval(this.timer);
            }
          }
        }, 2000);
      }
    },
  },
  mounted() {
    this.autoPopAnimation();
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

.danmaku-area {
  margin: 0;
}
.danmaku-container {
  max-width: 340px;
  max-height: 288px;
  overflow-x: hidden;
  overflow-y: scroll;
}

/* .hidedanmu{
  visibility: hidden;
  pointer-events: none;
} */

.danmaku-list {
  text-decoration: none;
  max-width: 342px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
}
.danmaku-list li.danmaku-list-item {
  margin-bottom: 10px;
  padding: 0;
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: left;
}
.danmaku-list li.danmaku-list-item > span {
  padding: 0 20px;
  line-height: 36px;
  font-size: 14px;
  display: inline-block;
  margin: 0;
  color: #fff;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid hsla(0, 0%, 100%, 0.53);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: left;
  max-width: 290px;
}
.danmaku-list li.danmaku-list-item:nth-last-child(6) {
  /* visibility: hidden; */
  animation: fadeout 0.3s linear;
}

.input-container {
  background: rgba(0, 0, 0, 0.6);
  border-radius: 25px;
  max-width: 350px;
  height: 48px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  padding: 10px;
}
.input-container span {
  color: rgb(140, 140, 140);
  cursor: pointer;
  font-size: 14px;
  height: 48px;
  text-align: left;
  line-height: 48px;
}
.input-container > * {
  cursor: pointer;
}
.arrow-icon {
  margin-right: 8px;
  width: 18px;
}

.send-btn {
  background: rgb(184, 23, 23);
  color: #fff;
  padding: 5px 15px;
  border: 10px;
  outline: 0;
  font-size: 16px;
  cursor: pointer;
  border-radius: 15px;
  margin-left: 5px;
}
.send-btn:hover,
.send-btn:focus {
  background: rgb(153, 17, 17);
}

.send-btn-container {
  display: flex;
  align-items: center;
  
}
.send-btn-container .show-icon,
.send-btn-container .close-icon {
  width: 24px;
  margin-right: 4px;
}
.input-container .show-list {
  text-decoration: none;
  position: absolute;
  left: 0;
  bottom: 64px;
  background: rgba(0, 0, 0, 0.7);
  width: 340px;
  color: #fff;
  border-radius: 10px;
  max-height: 200px;
  overflow-x: hidden;
  overflow-y: scroll;
  margin: 0;
  padding: 0;
}
.input-container .show-list .list-item {
  text-align: left;
  font-size: 14px;
  line-height: 36px;
  text-decoration: none;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #fff;
  max-width: 100%;
  padding: 0 10px;
  overflow: hidden;
}
.input-container .show-list .list-item:hover {
  background: rgb(184, 23, 23);
}

.dm-enter {
  opacity: 0;
  transform: translateY(20px);
}

.dm-leave-to {
  opacity: 0;
  transform: translateY(-50px);
}

.dm-enter-active,
.dm-leave-active {
  transition: all 0.6s ease;
}

.dm-move {
  transition: all 0.6s ease;
}

.dm-leave-active {
  position: absolute;
}


.input-mobile{
  min-width: 213px;
  height: 40px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 3px;
  display: flex;
  padding: 10px 10px 10px 20px;
  box-sizing: border-box;
  position: relative;
  justify-content: space-between;
}

.input-mobile .send-choices{
  color: #fff;
  font-size: 14px;
  display: inline-block;
  vertical-align: middle;
  line-height: 20px;
}

.input-mobile .show-list {
  text-decoration: none;
  position: absolute;
  left: 0;
  bottom: 50px;
  background: rgba(0, 0, 0, 0.9);
  width: 90%;
  color: #fff;
  border-radius: 3px;
  max-height: 200px;
  overflow-x: hidden;
  overflow-y: scroll;
  margin: 0;
  padding: 0;
}
.input-mobile .show-list .list-item {
  text-align: left;
  font-size: 14px;
  line-height: 36px;
  text-decoration: none;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #fff;
  max-width: 100%;
  padding: 0 10px;
  overflow: hidden;
}
.input-mobile .show-list .list-item:hover {
  background: rgb(184, 23, 23);
}

.input-mobile .send-btn-container{
  margin-left: 10px;
}


@keyframes fadeout {
  0% {
    opacity: 1;
    transform: translateY(10px);
  }
  50% {
    opacity: 0.7;
    transform: translateY(3px);
  }
  100% {
    opacity: 0.2;
    transform: translateY(0px);
  }
}

@media only screen and (max-width: 500px), (max-height: 487px) {
  .danmaku-list li.danmaku-list-item{
    margin-bottom: 5px;
  }
  .danmaku-list li.danmaku-list-item > span{
    border-radius: 3px;
    border: 0;
  }
}
</style>
