<template>
    <view class="send--voice">
        <view class="voice" hover-class="chat-hover" @touchstart="startRecord" @touchend="stopRecord">
            <text class="button">按住说话</text>
        </view>
        <!-- 录音中 -->
        <view class="modal" v-if="showRecordTip">
            <image class="icon" src="@/static/icon/record-icon.png" mode="widthFix"></image>
            <text class="text">录音中 {{recordTime}} s</text>
        </view>
        <!-- 录音完成发送或取消 -->
        <view class="lay" v-if="showConfirm">
            <view class="item red" @click="()=> { this.mp3AudioSrc = null, this.showConfirm = false }">取消</view>
            <view class="item" @click="startPlay">试听</view>
            <view class="item greed" @click="this.senVoice">发送</view>
        </view>
    </view>
</template>

<script>
    import im from '@/utils/im/index.js'
    import permision from '@/utils/permission.js'

    export default {
        props: {
            conversationType: {
                type: Number,
                default: 0
            },
            targetId: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                showRecordTip: false,
                showConfirm: false,
                recordTime: 60,
                interval: 0,
                maxRecordTime: 60,
                recorderManager: null,
                mp3AudioSrc: null,
                isBetaPlay: false
            }
        },
        computed: {
            sender() {
                return this.$store.getters.sender
            }
        },
        created() {
            this.recorderManager = uni.getRecorderManager()
        },
        methods: {
            onShowLay(){
                // this.showRecordTip = true
                uni.vibrateShort({
                    complete: com => {
                        console.log(com)
                    }
                })
            },
            // 检查安卓录制权限
            async getAndroidPermission() {
                return await permision.requestAndroidPermission('android.permission.RECORD_AUDIO')
            },
            // 录制语音消息
            startRecord() {
                this.getAndroidPermission().then(code => {
                    switch (code) {
                        case 1:
                            this.showRecordTip = true
                            this.recorderManager.start()
                            this.interval = setInterval(() => {
                                this.recordTime -= 1
                                if (this.recordTime === 0) {
                                    this.stopRecord()
                                }
                            }, 1000)
                            break;
                        case 0:
                            uni.showToast({
                                title: '暂无麦克风权限，请前往应用设置开启麦克风',
                                icon: 'none'
                            })
                            break;
                        case -1:
                            uni.showToast({
                                title: '应用权限错误',
                                icon: 'none'
                            })
                            break;
                    }
                })
            },
            // 结束录音
            stopRecord(e) {
                if (!this.showRecordTip) return
                // 延迟500毫秒结束录音
                setTimeout(()=> {
                    this.recorderManager.stop()
                    clearInterval(this.interval)
                    // 监听录音结束
                    this.recorderManager.onStop(res => {
                        console.log(res)
                        this.mp3AudioSrc = res.tempFilePath
                        this.showConfirm = true
                        this.showRecordTip = false
                    })
                },500)
            },
            // 发送语音消息
            senVoice(){
                if(this.mp3AudioSrc === null) {
                    uni.showToast({
                        title: '发送失败, 暂无音频信息',
                        icon : 'none'
                    })
                    return
                }
                im.sentVoice(this.conversationType, this.targetId, this.mp3AudioSrc, (this.maxRecordTime -
                    this.recordTime), this.sender, () => {
                    this.recordTime = this.maxRecordTime
                    this.mp3AudioSrc = null
                    this.showConfirm = false
                    setTimeout(() => {
                        this.$emit('success')
                    }, 500)
                })
            },
            // 试听语音
            startPlay() {
                let betaAudio = uni.createInnerAudioContext()
                betaAudio.src = this.mp3AudioSrc;
                // 在播放中
                if(this.isBetaPlay){
                    betaAudio.stop()
                    betaAudio.onStop(() => {
                        this.isBetaPlay = false;
                        betaAudio.destroy()
                    })
                    return
                }
                // 监听音频播放中
                betaAudio.play()
                betaAudio.onPlay(() => {
                    this.isBetaPlay = true;
                })
                // 监听音频结束
                betaAudio.onEnded(() => {
                    this.isBetaPlay = false;
                    betaAudio.destroy()
                })
            }
        }
    }
</script>

<style scoped lang="scss">
    .send--voice {
        .voice {
            display: flex;
            background: $window-color;
            height: 70rpx;
            line-height: 70rpx;
            justify-content: center;
            align-items: center;
            width: 500rpx;
            border-radius: 10rpx;
            margin-right: 15rpx;
            .button {
                font-size: 30rpx;
                color: #333;
            }
        }
        
        .lay{
            position: absolute;
            left: 0;
            bottom: 0;
            z-index: 9;
            background: rgba($color: #000000, $alpha: .5);
            height: 100vh;
            width: 100vw;
            padding: 20vh 30rpx;
            box-sizing: border-box;
            display: flex;
            justify-content: space-around;
            align-items: flex-end;
            flex-direction: row;
            .item{
                height: 140rpx;
                width: 140rpx;
                text-align: center;
                line-height: 140rpx;
                border-radius: 50%;
                background-color: white;
                &.red{
                    background: #e6576b;
                    color: white;
                }
                &.greed{
                    background: #34CE98;
                    color: white;
                }
            }
        }

        .modal {
            display: flex;
            background: rgba(0, 0, 0, .6);
            position: fixed;
            height: 200rpx;
            width: 300rpx;
            border-radius: 10rpx;
            z-index: 99;
            top: 550rpx;
            left: 225rpx;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            .icon {
                width: 88rpx;
                height: 88rpx;
            }

            .text {
                font-size: 28rpx;
                color: #FFFFFF;
            }
        }
    }
</style>
