<template>
  <div class="o_box">
    <div class="o_top"></div>
    <div class="o_content">
      <div class="type_box">
        {{ getType(cjson) }}
      </div>
      <div class="word_box">
        <div class="word_box2" v-if="cjson.type == 'word' || cjson.type == 'QA'">
          <div class="word_content" v-html="cjson.content"></div>
          <img class="word_img" :src="cjson.img" alt="" v-if="cjson.img" @click="previewImg(cjson.img)" />
        </div>
        <div class="sentence_box" v-if="cjson.type == 'sentence'">
          <div class="word_content" v-html="cjson.content">
          </div>
          <img class="word_img" :src="cjson.img" alt="" v-if="cjson.img" @click="previewImg(cjson.img)" />
        </div>
        <div class="word_box2" v-if="cjson.type == 'theme'" style="flex-direction: column;">
          <div class="word_content" v-html="cjson.content"></div>
          <div class="word_content2" v-html="cjson.content2"></div>
        </div>
        <testRole v-if="cjson.type == 'createRole'" :checkJson="answerArray"></testRole>
      </div>
    </div>
    <div class="o_bottom" v-loading="isloading">
      <!-- <div class="audio" v-if="!LuAudioUrl">
        <img v-if="!isRecord" src="../../../assets/icon/englishVoice/start_aduio.png" alt="" @click="startRecorder">
        <img v-else src="../../../assets/icon/englishVoice/stop_audio.png" alt="" @click="startRecorder">
      </div> -->
      <!-- <div class="audio_word" v-if="!LuAudioUrl">
        <span v-if="!isRecord">点击话筒开始录音</span>
        <span v-else>点击话筒结束录音</span>
      </div> -->
      <div class="star_box" v-if="LuAudioUrl && star > 0">
        <div class="star" v-for="index2 in 5" :key="'star' + index2" :class="{ starA: star >= (index2) }">
        </div>
      </div>
      <div v-if="LuAudioUrl" class="audio_b">
        <mini-audio :audio-source="LuAudioUrl" class="audio_class"></mini-audio>
      </div>
      <div v-else-if="cjson.type !== 'createRole'">
        未上传录音1
      </div>
      <!-- <div v-if="LuAudioUrl" class="audio_rerecord" @click="restart()">
        <span>录音</span>
      </div> -->
      <!-- <div class="audio_index" v-if="!isRecord">
        <div class="audio_index_last" :class="{ disabled: checkType == 0 }" @click="checkIndex('-1')">
          <img src="../../../assets/icon/englishVoice/coin.png" alt="">
        </div>
        <div class="audio_index_content">
          <span>{{ checkType + 1 }}</span>
          <span>/</span>
          <span>{{ checkJson.length }}</span>
        </div>
        <div class="audio_index_next" :class="{ disabled: checkType == (checkJson.length - 1) }" @click="checkIndex('1')">
          <img src="../../../assets/icon/englishVoice/coin.png" alt="">
        </div>
      </div>
      <div v-else class="audio_ing">
        <span>正在录音...</span>
      </div> -->
    </div>
  </div>
</template>

<script>
import Recorder from "js-audio-recorder";
const lamejs = require("lamejs");

const recorder = new Recorder({
  sampleBits: 16, // 采样位数，支持 8 或 16，默认是16
  sampleRate: 48000, // 采样率，支持 11025、16000、22050、24000、44100、48000，根据浏览器默认值，我的chrome是48000
  numChannels: 1 // 声道，支持 1 或 2， 默认是1
  // compiling: false,(0.x版本中生效,1.x增加中) // 是否边录边转换，默认是false
});

// 绑定事件-打印的是当前录音数据
recorder.onprogress = function (params) {
  // console.log('--------------START---------------')
  // console.log('录音时长(秒)', params.duration);
  // console.log('录音大小(字节)', params.fileSize);
  // console.log('录音音量百分比(%)', params.vol);
  // console.log('当前录音的总数据([DataView, DataView...])', params.data);
  // console.log('--------------END---------------')
};
import testRole from "./testRole.vue";
export default {
  components: {
    testRole,
  },
  props: {
    checkJson: {
      type: Array
    },
    checkType: {
      type: Number
    },
    work: {
      type: Array
    }
  },
  data() {
    return {
      cjson: {},
      LuAudioUrl: "",
      isRecord: false,
      isPlayerRecord: false,
      isloading: false,
      answerArray: [],
      star: 0
    };
  },
  computed: {
    getType() {
      return function (json) {
        if (json.type == 'word') {
          return '单词/词组'
        } else if (json.type == 'QA') {
          return '问答题目'
        } else if (json.type == 'sentence') {
          return '句子/短文'
        } else if (json.type == 'theme') {
          return '主题陈述'
        } else if (json.type == 'createRole') {
          return '角色对话'
        }
      };
    },
  },
  watch: {
    checkType: {
      handler: function (newVal, oldVal) {
        this.cjson = JSON.parse(JSON.stringify(this.checkJson[newVal]));
        // this.LuAudioUrl = this.work[newVal]
        //   ? JSON.parse(JSON.stringify(this.work[newVal]))
        //   : "";
        this.LuAudioUrl = ''
        if (typeof this.work[this.checkType] == 'string') {
          this.LuAudioUrl = this.work[this.checkType] ? JSON.parse(JSON.stringify(this.work[this.checkType])) : ''
        } else if (typeof this.work[this.checkType] == 'object' && this.cjson.type != 'createRole') {
          this.LuAudioUrl = this.work[this.checkType].audio ? JSON.parse(JSON.stringify(this.work[this.checkType].audio)) : ''
        } else if (typeof this.work[newVal] == 'object' && this.cjson.type == 'createRole') {
          var a = Array.isArray(this.work[newVal])
          if (a) {
            this.answerArray = JSON.parse(JSON.stringify(this.work[newVal]))
          } else {
            this.answerArray = []
            this.answerArray.push(
              {
                isY: false,
                content: this.cjson.content3,
                name: this.cjson.content,
                img: this.cjson.img
              }
            )
          }
          this.$emit('setWork', this.answerArray, this.checkType)
        }
        if (!this.work[newVal] && this.cjson.type == 'createRole') {
          var a = Array.isArray(this.work[newVal])
          if (a) {
            this.answerArray = JSON.parse(JSON.stringify(this.work[newVal]))
          } else {
            this.answerArray = []
            this.answerArray.push(
              {
                isY: false,
                content: this.cjson.content3,
                name: this.cjson.content,
                img: this.cjson.img
              }
            )
          }
        }
        this.star = this.work[this.checkType] ? (this.work[this.checkType].score ? JSON.parse(JSON.stringify(this.work[this.checkType].score)) : 0) : 0
      },
      deep: true
    }
  },
  methods: {
    previewImg(url) {
      this.$hevueImgPreview(url);
    },
    restart() {
      this.LuAudioUrl = "";
      setTimeout(() => {
        this.startRecorder();
      }, 500);
    },
    checkIndex(type) {
      if (type == "1") {
        if (this.checkType == this.checkJson.length - 1) {
          return;
        }
        this.$emit("setType", this.checkType + 1);
      } else {
        if (this.checkType == 0) {
          return;
        }
        this.$emit("setType", this.checkType - 1);
      }
    },
    // 开始录音
    startRecorder() {
      let _this = this;
      if (!_this.isRecord) {
        recorder.destroy(); // 销毁录音
        _this.isRecord = true;
        recorder.start().then(
          () => { },
          error => {
            _this.isRecord = false;
            // _this.$message.error(`${error.name} : ${error.message}`);
            _this.$message.error(
              `没有找到可使用的麦克风，或者您没有允许此网页使用麦克风`
            );
            // 出错了
            console.log(`${error.name} : ${error.message}`);
          }
        );
      } else {
        _this.isRecord = false;
        recorder.stop(); // 结束录音
        this.getMp3Data();
      }
    },

    // 录音播放
    playRecorder() {
      if (!recorder.fileSize) {
        return;
      }
      if (!this.isPlayerRecord) {
        this.isPlayerRecord = true;
        recorder.play();
      } else {
        this.isPlayerRecord = false;
        recorder.stopPlay(); // 停止录音播放
      }
      recorder.onplayend = () => {
        this.isPlayerRecord = false;
        console.log("onplayend");
      };
    },

    /**
     * 文件格式转换 wav-map3
     * */
    getMp3Data() {
      if (!recorder.fileSize) {
        this.$message.error("请录音后在上传语音");
        return;
      }
      const mp3Blob = this.convertToMp3(recorder.getWAV());
      let audioFile = this.dataURLtoAudio(mp3Blob, "mp3");
      console.log(audioFile);
      this.beforeUpload1(audioFile, 3);
      // recorder.download(mp3Blob, "recorder", "mp3");
    },
    convertToMp3(wavDataView) {
      // 获取wav头信息
      const wav = lamejs.WavHeader.readHeader(wavDataView); // 此处其实可以不用去读wav头信息，毕竟有对应的config配置
      const { channels, sampleRate } = wav;
      const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128);
      // 获取左右通道数据
      const result = recorder.getChannelData();
      const buffer = [];
      const leftData =
        result.left &&
        new Int16Array(result.left.buffer, 0, result.left.byteLength / 2);
      const rightData =
        result.right &&
        new Int16Array(result.right.buffer, 0, result.right.byteLength / 2);
      const remaining = leftData.length + (rightData ? rightData.length : 0);
      const maxSamples = 1152;
      for (let i = 0; i < remaining; i += maxSamples) {
        const left = leftData.subarray(i, i + maxSamples);
        let right = null;
        let mp3buf = null;
        if (channels === 2) {
          right = rightData.subarray(i, i + maxSamples);
          mp3buf = mp3enc.encodeBuffer(left, right);
        } else {
          mp3buf = mp3enc.encodeBuffer(left);
        }
        if (mp3buf.length > 0) {
          buffer.push(mp3buf);
        }
      }

      const enc = mp3enc.flush();
      if (enc.length > 0) {
        buffer.push(enc);
      }
      return new Blob(buffer, { type: "audio/mp3" });
    },
    dataURLtoAudio(blob, filename) {
      return new File([blob], filename, { type: "audio/mp3" });
    },
    beforeUpload1(event, type) {
      this.isloading = true;
      var file;
      if (type == 3) {
        file = event;
      } else {
        file = event.target.files[0];
      }
      var credentials = {
        accessKeyId: "AKIATLPEDU37QV5CHLMH",
        secretAccessKey: "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"
      }; //秘钥形式的登录上传
      window.AWS.config.update(credentials);
      window.AWS.config.region = "cn-northwest-1"; //设置区域

      var bucket = new window.AWS.S3({ params: { Bucket: "ccrb" } }); //选择桶
      var _this = this;

      if (file) {
        var params = {
          Key:
            file.name.split(".")[0] +
            new Date().getTime() +
            "." +
            file.name.split(".")[file.name.split(".").length - 1],
          ContentType: file.type,
          Body: file,
          "Access-Control-Allow-Credentials": "*",
          ACL: "public-read"
        }; //key可以设置为桶的相抵路径，Body为文件， ACL最好要设置
        var options = {
          partSize: 2048 * 1024 * 1024,
          queueSize: 2,
          leavePartsOnError: true
        };
        bucket
          .upload(params, options)
          .on("httpUploadProgress", function (evt) {
            //这里可以写进度条
            // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
            // _this.progress = parseInt((evt.loaded * 80) / evt.total);
          })
          .send(function (err, data) {
            _this.isloading = false;
            // _this.progress = 100;
            if (err) {
              var a = _this.$refs.upload1.uploadFiles;
              a.splice(a.length - 1, a.length);
              _this.$message.error("上传失败");
            } else {
              if (type == 3) {
                _this.LuAudioUrl = data.Location;
                _this.$emit("setWork", _this.LuAudioUrl, _this.checkType);
              }
              console.log(data.Location);
            }
          });
      }
    }
  },
  mounted() {
    this.cjson = JSON.parse(JSON.stringify(this.checkJson[this.checkType]));
    this.LuAudioUrl = ''
    if (typeof this.work[this.checkType] == 'string') {
      this.LuAudioUrl = this.work[this.checkType] ? JSON.parse(JSON.stringify(this.work[this.checkType])) : ''
    } else if (typeof this.work[this.checkType] == 'object' && this.cjson.type != 'createRole') {
      this.LuAudioUrl = this.work[this.checkType].audio ? JSON.parse(JSON.stringify(this.work[this.checkType].audio)) : ''
    } else if (typeof this.work[newVal] == 'object' && this.cjson.type == 'createRole') {
      var a = Array.isArray(this.work[newVal])
      if (a) {
        this.answerArray = JSON.parse(JSON.stringify(this.work[newVal]))
      } else {
        this.answerArray = []
        this.answerArray.push(
          {
            isY: false,
            content: this.cjson.content3,
            name: this.cjson.content,
            img: this.cjson.img
          }
        )
      }
      this.$emit('setWork', this.answerArray, this.checkType)
    }
    if (!this.work[newVal] && this.cjson.type == 'createRole') {
      var a = Array.isArray(this.work[newVal])
      if (a) {
        this.answerArray = JSON.parse(JSON.stringify(this.work[newVal]))
      } else {
        this.answerArray = []
        this.answerArray.push(
          {
            isY: false,
            content: this.cjson.content3,
            name: this.cjson.content,
            img: this.cjson.img
          }
        )
      }
    }
    this.star = this.work[this.checkType] ? (this.work[this.checkType].score ? JSON.parse(JSON.stringify(this.work[this.checkType].score)) : 0) : 0
    // this.LuAudioUrl = this.work[this.checkType]
    //   ? JSON.parse(JSON.stringify(this.work[this.checkType]))
    //   : "";
  }
};
</script>

<style scoped>
.o_box {
  width: 100%;
  height: 100%;
  /* background-image: url('../../../assets/icon/env_background.png'); */
  background-size: cover;
}

.o_top {
  height: 65px;
  position: absolute;
}

.o_content {
  height: calc(100% - 80px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.o_bottom {
  height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.word_box {
  width: calc(100% - 20px);
  background: #f2f2f2;
  border-radius: 10px;
  position: relative;
  height: 90%;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.word_box2 {
  max-height: calc(100% - 20px);
  max-width: calc(100% - 20px);
  min-width: 350px;
  display: flex;
  width: auto;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
  box-sizing: border-box;
  overflow: auto;
  color: #000;
}

.sentence_box {
  font-size: 16px;
  color: #000;
  line-height: 20px;
  word-break: break-word;
  white-space: pre-line;
  max-height: calc(100% - 20px);
  max-width: calc(100% - 20px);
  min-width: 350px;
  display: flex;
  width: auto;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
  box-sizing: border-box;
  overflow: auto;
}

.sentence_box>.word_content {
  position: relative;
  z-index: 999;
  /* text-align: center; */
  color: #000;
  width: calc(100%);
  word-break: break-word;
  white-space: pre-line;
}

.sentence_box>.word_img,
.word_box2>.word_img {
  min-width: 100px;
  width: 100px;
  height: 50px;
  z-index: 999;
  position: sticky;
  margin: 0 0 0 10px;
  display: block;
  border-radius: 10px;
  cursor: pointer;
  object-fit: cover;
  top: calc(50% - 25px);
}

.word_box2>.word_content {
  position: relative;
  z-index: 999;
  text-align: center;
  font-size: 30px;
  margin: 7px 0 7px 0px;
  color: #000;
  width: calc(100%);
  word-break: break-word;
  white-space: pre-line;
}

.word_box2>.word_content2 {
  position: relative;
  z-index: 999;
  /* text-align: center; */
  /* font-size: 30px; */
  margin: 7px 0 7px 0px;
  color: #727272;
  width: calc(100%);
  word-break: break-word;
  white-space: pre-line;
  font-size: 16px;

}

.o_bottom .audio {
  display: flex;
  align-items: center;
  justify-content: center;
}

.o_bottom .audio>img {
  width: 75px;
  height: 75px;
  cursor: pointer;
}

.o_bottom .audio_word {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #00000099;
  font-size: 16px;
  margin: 10px 0 8px;
}

.o_bottom .audio_index {
  display: flex;
  align-items: center;
  justify-content: center;
}

.audio_index_last,
.audio_index_next {
  height: 40px;
  width: 40px;
  background: #3681fc;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.audio_index_last>img,
.audio_index_next>img {
  width: 15px;
  height: auto;
}

.audio_index_last.disabled,
.audio_index_next.disabled {
  opacity: 0.6;
}

.audio_index_last>img {
  transform: rotate(180deg);
}

.audio_index_last {
  margin-right: 20px;
}

.audio_index_content {
  color: #000;
  font-size: 16px;
}

.audio_index_next {
  margin-left: 20px;
}

.audio_ing {
  color: #ee3e3e;
  margin-top: 25px;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.audio_b {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
}

.audio_rerecord {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
}

.audio_rerecord>span {
  display: flex;
  border: 1px solid #3981fa;
  align-items: center;
  color: #3981fa;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
}

.audio_rerecord>span::before {
  content: "";
  width: 15px;
  height: 15px;
  background: url("../../../assets/icon/englishVoice/restart.png");
  display: block;
  background-size: 100% 100%;
  margin-right: 5px;
}

.audio_class {
  background: #3680fb !important;
  margin: 0 !important;
}

.audio_b>>>.vueAudioBetter span:before {
  color: #fff;
}

.audio_class>>>.slider .process {
  background: #000;
}

.audio_b>>>.vueAudioBetter .iconfont:active {
  position: unset;
}

.star_box {
  display: flex;
  align-items: center;
  margin-bottom: 10px
}

.star_box>.star {
  width: 25px;
  height: 25px;
  display: block;
  background-image: url('../../../assets/icon/englishVoice/star-no.png');
  background-size: 100% 100%;
}

.star_box>.star+.star {
  margin-left: 5px;
}

.star_box>.starA {
  background-image: url('../../../assets/icon/englishVoice/star.png');
}

.type_box {
  width: calc(100% - 20px);
    text-align: right;
    color: #a5a5a5;
    position: absolute;
    top: 0;
}
</style>
