import httpRequestApi from '../../../utils/APIClient';
Page({
    data: {
        title: '',
        id: '',
        img: '',
        fullScreenBtn: false,
        playBtn: false,
        gesture: true,
        muted: true,
        gesture: false,
        centerBtn: false,
        recordFlag: 0,
        recordSource: '',
        videoCtr: 'recordingVideoEnd',
        btnFlag: false,
        btnImgFlag: false,
        microphonePng: '../../../static/image/microphone.png',
        recordingGif: '../../../static/image/readingNow.gif',
        videoUrl: '',
        readingText: ''
    },
    onLoad: function (option) {
        console.log(option);
        this.videoCtx = null;

        const uid = wx.getStorageSync('uid')
        httpRequestApi.getClassDetail(uid, option.id).success(res => {
            let reg = /\\n/g
            this.setData({
                title: res.data.data.title,
                videoUrl: res.data.data.playUrl,
                img: res.data.data.iconImg,
                id: res.data.data.id,
                // readingText: res.data.data.lessonText.replace(reg,'')
                readingText: res.data.data.lessonText,
                grade: res.data.data.gradeClassify
            })
            console.log(this.data.readingText)
            console.log(this.data.img)
        }, () => {
            wx.setNavigationBarTitle({
                title: this.data.title //页面标题为路由参数
            })
        })

        this.recorderManager = wx.getRecorderManager();


        // 监听录音部分
        // 录音开始
        this.recorderManager.onStart(() => {
            // this.saveVideo();
            this.setData({
                btnImgFlag: true
            })
            console.log('recorder start')
        })
        // 录音结束
        this.recorderManager.onStop((res) => {
            this.videoCtx.stop();
            console.log('recorder stop', res)
            const recordFile = res.tempFilePath;
            this.setData({
                recordFlag: 0,
                recordSource: recordFile,
                btnFlag: true,
                btnImgFlag: false
            })
        })
    },
    // onShow:function(){

    // },

    // 录音中视频播放结束 (控制录音同时结束)
    recordingVideoEnd: function () {
        console.log(this.data.videoCtr)
        console.log('recordingVideoEnd');
        // 
        if (this.data.recordFlag === 0) {
            // this.recordStop();
            this.playingVideoEnd();
            // this.setData({
            //     videoCtr: 'playingVideoEnd'
            // })
        }
        // 录音结束
        if (this.data.recordFlag === 1) {
            this.recordStop();
            // this.recordStop();
            // this.innerAudioContext.stop();
        }
    },
    // 播放中视频播放结束 (控制录音同时结束)
    playingVideoEnd: function () {
        console.log('playingVideoEnd')
        this.innerAudioContext.stop();
    },
    /***
     *  recordFlag: 
     *      0 初始状态
     *      1 录音中
     *      2 录音结束
     ***/
    audioRecord: function () {
        console.log(this.data.recordFlag)
        if (this.data.recordFlag === 0) {
            // this.recordStart();
            // this.saveVideo();
            this.videoComplete();
        }
        // 录音结束后
        if (this.data.recordFlag === 1) {
            this.recordStop();
        }
    },
    // 录音开始
    /**
     * duration: 时长 最长10分钟
            sampleRate: 44100, 采样率
            numberOfChannels: 1, 录音通道
            encodeBitRate: 192000, 码率
            format: 'mp3', 格式
            frameSize: 50 制定帧大小
     */
    recordStart: function () {
        console.log('录音开始');
        const options = {
            duration: 600000,
            sampleRate: 44100,
            numberOfChannels: 1,
            encodeBitRate: 192000,
            format: 'mp3',
            frameSize: 50
        }
        this.recorderManager.start(options);
    },
    // 录音结束
    recordStop: function () {
        console.log('录音结束')
        this.recorderManager.stop();
    },
    // 播放录音
    audioPlay: function () {
        console.log('音频播放');
        this.innerAudioContext = wx.createInnerAudioContext();
        this.innerAudioContext.onError((res) => {
            // 播放音频失败的回调
        })
        this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
        console.log(this.innerAudioContext.src);
        this.videoCtx.play();
        this.innerAudioContext.play();
    },

    videoComplete: function () {
        // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4';
        this.setData({
            recordFlag: 1
        }, () => {
            this.videoCtx = wx.createVideoContext('myVideo', this);
            this.videoCtx.play();
            this.recordStart();
        })
    },
    // 上传
    upload: function () {
        const recordSource = this.data.recordSource;
        wx.uploadFile({
            url: 'https://readerbase.efunbox.cn/file/upload',
            filePath: recordSource,
            name: '朗读录音',
            header: {
                uid: wx.getStorageSync('uid')
            },
            success: (res) => {
                const formateRes = JSON.parse(res.data);
                let audioPath = formateRes.data;
                let uid = wx.getStorageSync('uid');
                shareWorks(uid, audioPath);
            }

        })
        let shareWorks = (uid, audio) => {
            console.log(this.data.img)
            const data = {
                "lessonId": this.data.id,
                "originVideo": this.data.videoUrl,
                "audioPath": audio,
                "title": this.data.title,
                "iconImg": this.data.img,
                "summary": this.data.grade,
            };
            httpRequestApi.postWork(uid, data).success(res => {
                wx.showToast({
                    title: '上传成功',
                    icon: 'success',
                    duration: 1000
                })
            })
        };
    }
})