<!--  -->
<template>
  <div class="NNPE-ArticleView" v-if="curQue">
    <AudioLine
      :mp3="curQue.mp3_list[0].url"
      v-if="curQue.mp3_list[0].url"
      class="aduioLine-box"
      :getCurTime="getCurTime"
      ref="audioLine"
      :stopAudio="stopAudio"
      @handleChangeStopAudio="handleChangeStopAudio"
    />
    <template v-if="resObj">
      <!--  -->
      <div class="NPC-sentences-list">
        <div v-for="(item, index) in resObj.sentList" :key="'detail' + index">
          <div
            class="NNPE-detail"
            @click="handleChangeTime(curQue.wordTime[index].bg)"
          >
            <div
              class="NNPE-words"
              v-for="(pItem, pIndex) in item"
              :key="'wordsList' + pIndex"
              :class="[pItem.wordIndex == 0 ? 'textLeft' : 'textCenter']"
            >
              <template v-if="!pItem.width">
                <template v-if="pItem.isShow">
                  <template
                    v-if="
                      item[pIndex + 1].chs &&
                      chsFhList.indexOf(item[pIndex + 1].chs) > -1
                    "
                  >
                    <span class="NNPE-words-box">
                      <span
                        class="NNPE-pinyin"
                        :class="[pItem.className ? pItem.className : '']"
                        >{{ pItem.pinyin }}</span
                      >
                      <span
                        class="NNPE-chs"
                        :class="[pItem.padding ? 'padding' : '']"
                      >
                        <template
                          v-if="pItem.timeList && pItem.timeList.length > 0"
                        >
                          <span
                            v-for="(wItem, wIndex) in pItem.leg"
                            :key="'ci' + wIndex + pIndex + index"
                            :class="[
                              pItem.timeList[wIndex] &&
                              curTime >= pItem.timeList[wIndex].wordBg &&
                              curTime <= curQue.wordTime[index].ed
                                ? 'active'
                                : '',
                            ]"
                            >{{ pItem.chs[wIndex] }}</span
                          >
                        </template>
                      </span>
                    </span>
                    <span class="NNPE-words-box">
                      <span class="NNPE-pinyin" style="text-align: left">{{
                        item[pIndex + 1].pinyin
                      }}</span>
                      <span class="NNPE-chs" style="text-align: left">
                        <span
                          :class="[
                            pItem.timeList[pItem.leg - 1] &&
                            curTime >= pItem.timeList[pItem.leg - 1].wordBg &&
                            curTime <= curQue.wordTime[index].ed
                              ? 'active'
                              : '',
                          ]"
                        >
                          {{ item[pIndex + 1].chs }}</span
                        >
                      </span>
                    </span>
                  </template>
                  <template v-else>
                    <span
                      class="NNPE-pinyin"
                      :class="[
                        pItem.padding ? 'padding' : '',
                        pItem.className ? pItem.className : '',
                      ]"
                      >{{ pItem.pinyin }}</span
                    >
                    <span
                      class="NNPE-chs"
                      :class="[pItem.padding ? 'padding' : '']"
                    >
                      <template
                        v-if="pItem.timeList && pItem.timeList.length > 0"
                      >
                        <span
                          v-for="(wItem, wIndex) in pItem.leg"
                          :key="'ci' + wIndex + pIndex + index"
                          :class="[
                            pItem.timeList[wIndex] &&
                            curTime >= pItem.timeList[wIndex].wordBg &&
                            curTime <= curQue.wordTime[index].ed
                              ? 'active'
                              : '',
                          ]"
                          >{{ pItem.chs[wIndex] }}</span
                        >
                      </template>
                    </span>
                  </template>
                </template>
              </template>
              <template v-else>
                <span
                  :style="{
                    height: pItem.height + 'px',
                    width: pItem.width + 'px',
                  }"
                ></span>
              </template>
            </div>
          </div>
          <div
            v-show="
              curTime >= curQue.wordTime[index].bg &&
              curTime <= curQue.wordTime[index].ed
            "
            class="Soundrecord-content"
          >
            <Soundrecord
              @handleWav="handleWav"
              type="pro"
              class="luyin-box"
              @handleParentPlay="handleParentPlay"
            />
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { timeStrToSen } from "@/utils/index";
import AudioLine from "../AudioLine.vue";
import Soundrecord from "../../preview/Soundrecord.vue"; // 录音模板
export default {
  name: "ArticleView",
  props: ["curQue"],
  components: {
    AudioLine,
    Soundrecord,
  },
  data() {
    return {
      resObj: null,
      curTime: 0, //单位s
      chsFhList: ["，", "。", "“", "：", "》", "《", "？", "！", "；"],
      enFhList: [",", ".", ";", "?", "!", ":", ">", "<"],
      stopAudio: false,
    };
  },
  computed: {},
  watch: {},
  //方法集合
  methods: {
    getCurTime(curTime) {
      this.curTime = curTime * 1000;
    },
    handleData() {
      let resArr = [],
        timeArr = [];
      let curQue = JSON.parse(JSON.stringify(this.curQue));
      let wordTimeList = curQue.wordTime;
      curQue.detail.forEach((dItem, dIndex) => {
        dItem.wordsList.forEach((sItem, sIndex) => {
          let sentArr = [];

          sItem.forEach((wItem, wIndex) => {
            let startIndex =
              wIndex == 0
                ? 0
                : sentArr[wIndex - 1].startIndex +
                  sentArr[wIndex - 1].chs.length;
            let endIndex =
              wIndex == 0
                ? wItem.chs.length
                : sentArr[wIndex - 1].endIndex + wItem.chs.length;
            // this.judgePad(sItem, wItem, wIndex);
            this.mergeWordSymbol(sItem, wItem, wIndex);
            let obj = {
              paraIndex: dIndex, //段落索引
              sentIndex: sIndex, //在段落中句子索引
              wordIndex: wIndex, //单词的索引
              pinyin: wItem.pinyin,
              chs: wItem.chs,
              padding: true,
              className: wItem.className,
              isShow: wItem.isShow,
              startIndex: startIndex,
              endIndex: endIndex,
              leg: wItem.chs.length,
              timeList: [],
            };
            sentArr.push(obj);
          });
          resArr.push(sentArr);
        });

        timeArr.push(dItem.timeList);
      });
      this.mergeWordTime(resArr, wordTimeList);
      let timeList = [];
      timeArr.forEach((item) => {
        item.forEach((aItem) => {
          if (timeList.indexOf(aItem) < 0) {
            timeList.push(aItem);
          }
        });
      });
      this.resObj = { sentList: resArr, timeList: timeList };
      console.log(this.resObj);
    },
    mergeWordTime(resArr, wordTimeList) {
      resArr.forEach((item, index) => {
        let wordsResultList = wordTimeList[index].wordsResultList;
        item.forEach((wItem) => {
          let startIndex = wItem.startIndex;
          let endIndex = wItem.endIndex;
          wItem.timeList = wordsResultList.slice(startIndex, endIndex);
        });
      });
    },
    //词和标点合一起
    mergeWordSymbol(sItem, wItem, curIndex) {
      let leg = sItem.length;
      if (curIndex < leg - 1) {
        let nextIndex = curIndex + 1;

        if (this.chsFhList.indexOf(wItem.chs) > -1) {
          wItem.isShow = false;
        } else {
          wItem.isShow = true;
        }
      }
    },
    handleData2() {
      let resArr = [],
        timeArr = [];
      let leg = this.curQue.detail.length;
      let curQue = JSON.parse(JSON.stringify(this.curQue));
      curQue.detail.forEach((dItem, dIndex) => {
        let endTime = 0;
        if (dIndex < leg - 1) {
          endTime = curQue.detail[dIndex + 1].timeList[0];
          dItem.timeList.push(endTime);
          dItem.timeList = this.handleTimeList(dItem.timeList);
        } else {
          dItem.timeList = this.handleTimeList(dItem.timeList);
          endTime = curQue.mp3_list[0].duration;
          dItem.timeList.push(endTime);
        }

        dItem.wordsList.forEach((sItem, sIndex) => {
          let time_diff = dItem.timeList[sIndex + 1] - dItem.timeList[sIndex];
          let wordsLeg = dItem.sentences[sIndex].length;
          let wordTime = time_diff / wordsLeg;
          // console.log(time_diff, wordsLeg, wordTime);
          let sentArr = [];
          sItem.forEach((wItem, wIndex) => {
            let preTotal = wIndex > 0 ? sentArr[wIndex - 1].endTime : 0;
            let endTime =
              wIndex > 0
                ? wordTime * wItem.chs.length + preTotal
                : dItem.timeList[sIndex] + wordTime * wItem.chs.length;
            //console.log("endTime" + endTime);
            let startTime =
              wIndex > 0 ? sentArr[wIndex - 1].endTime : dItem.timeList[sIndex];
            this.judgePad(sItem, wItem, wIndex);
            let obj = {
              paraIndex: dIndex, //段落索引
              sentIndex: sIndex, //在段落中句子索引
              wordIndex: wIndex, //单词的索引
              pinyin: wItem.pinyin,
              chs: wItem.chs,
              padding: wItem.padding,
              className: wItem.className,
              startTime: startTime,
              endTime: endTime,
            };
            sentArr.push(obj);
          });
          resArr.push(sentArr);
        });

        timeArr.push(dItem.timeList);
      });
      let timeList = [];
      timeArr.forEach((item) => {
        item.forEach((aItem) => {
          if (timeList.indexOf(aItem) < 0) {
            timeList.push(aItem);
          }
        });
      });
      this.resObj = { sentList: resArr, timeList: timeList };
      console.log(this.resObj);
    },
    //判断是否有padding
    judgePad(sItem, wItem, curIndex) {
      let leg = sItem.length;
      if (curIndex < leg - 1) {
        let nextIndex = curIndex + 1;
        let chs = sItem[nextIndex].chs;
        if (
          this.chsFhList.indexOf(chs) > -1 ||
          this.chsFhList.indexOf(wItem.chs) > -1
        ) {
          wItem.padding = false;
        } else {
          wItem.padding = true;
        }
        if (this.enFhList.indexOf(wItem.pinyin) > -1) {
          wItem.className = "textLeft";
        }
      }
    },
    //转化时间
    handleTimeList(list) {
      let listRes = [];
      list.forEach((item) => {
        let res = timeStrToSen(item);
        listRes.push(res);
      });
      return listRes;
    },
    //计算总时间
    countWordTime(sentArr) {
      let total = 0;
      sentArr.forEach((item) => {
        total += item.endTime;
      });
      return total;
    },
    //点击播放某个句子
    handleChangeTime(time) {
      this.curTime = time;
      this.$refs.audioLine.onTimeupdateTime(time / 1000);
    },
    handleWav(data) {},
    // 录音时暂停音频播放
    handleParentPlay() {
      this.stopAudio = true;
    },
    // 音频播放时改变布尔值
    handleChangeStopAudio() {
      this.stopAudio = false;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    console.log(this.curQue);
    if (this.curQue) {
      this.handleData();
    }
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.NNPE-ArticleView {
  width: 100%;
  .NPC-sentences-list {
    padding: 24px;
  }
  .Soundrecord-content {
  }
  .NNPE-detail {
    clear: both;
    overflow: hidden;
    width: 100%;
    background: #ffffff;
    border: 1px solid rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
    border-radius: 8px;
    margin-bottom: 8px;
    box-sizing: border-box;
    padding: 8px 16px 0px;
    .NNPE-words {
      float: left;
      padding: 0 0px 8px 0px;
      &-box {
        float: left;
        > span {
          display: block;
          &.NNPE-pinyin {
            font-family: "GB-PINYINOK-B";
            font-weight: normal;
            font-size: 14px;
            line-height: 20px;
            color: #000000;
            height: 20px;
            &.textLeft {
              text-align: left;
            }
          }
          &.NNPE-chs {
            font-family: "FZJCGFKTK";
            font-size: 20px;
            line-height: 150%;
            color: #000000;
            .active {
              color: #32a5d8;
            }
          }
          // &.padding {
          //   padding-right: 6px;
          // }
        }
      }
      &.textLeft {
        text-align: left;
      }
      &.textCenter {
        text-align: center;
      }
      > span {
        display: block;
        &.NNPE-pinyin {
          font-family: "GB-PINYINOK-B";
          font-weight: normal;
          font-size: 14px;
          line-height: 20px;
          color: #000000;
          height: 20px;
          &.textLeft {
            text-align: left;
          }
        }
        &.NNPE-chs {
          font-family: "FZJCGFKTK";
          font-size: 20px;
          line-height: 150%;
          color: #000000;
          .active {
            color: #32a5d8;
          }
        }
        &.padding {
          padding: 0 3px;
        }
      }
    }
  }
  .luyin-box {
    width: 200px;
    margin: 8px 0 8px 0px;
    padding: 0 12px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background: #ffffff;
  }
}
</style>