<template>
    <view class="call">
        <view class="video">
            <RongCloud-Call-RCUniCallView ref="bigVideoView" :style="{width:windowWidth+'px',height:windowHeight+'px'}"
                class="bigVideoView">
            </RongCloud-Call-RCUniCallView>
            <RongCloud-Call-RCUniCallView ref="smallVideoView" class="smallVideoView">
            </RongCloud-Call-RCUniCallView>
        </view>

        <view class="status" v-if="!connected || mediaType == 0">
            <view class="remote">
                <u-avatar v-if="userInfo.portraitUrl" :src="userInfo.portraitUrl" shape="square" size="96"
                    bgColor="#fff" />
                <u-avatar size="80" v-if="!userInfo.portraitUrl" shape="square"
                    :text="userInfo.name ? userInfo.name.substring(0,1) : '未'" font-size="44" randomBgColor />
                <view><text class="nickname">{{userInfo.name}}</text></view>
                <view v-if="remoteRinging"><text class="mediaType">等待对方接听</text></view>
                <view v-if="connected"><text class="mediaType">已接通</text></view>
                <view v-else><text class="mediaType">{{ mediaType == 0 ? '邀请您语音通话' : '邀请您视频通话' }}</text></view>
            </view>
        </view>
        <view class="buttons">
            <view class="btn" v-if="connected" @click="changeMicrophone">
                <view class="icon">
                    <u-icon name="mic" color="#ffffff" size="30" />
                    <u-icon name="mic-off" color="#ffffff" size="30" />
                </view>
                <text class="text">麦克风</text>
            </view>
            <view class="btn" @click="hangup">
                <view class="icon hangup">
                    <u-icon name="close" color="#ffffff" size="25" />
                </view>
                <text class="text">挂断</text>
            </view>
            <view class="btn" v-if="!connected" @click="accept">
                <view class="icon">
                    <u-icon name="checkmark" color="#ffffff" size="30" />
                </view>
                <text class="text">接听</text>
            </view>
            <view class="btn" v-if="connected" @click="changeSpeaker">
                <view class="icon">
                    <u-icon name="volume-fill" color="#ffffff" size="27" />
                    <u-icon name="volume-off-fill" color="#ffffff" size="27" />
                </view>
                <text class="text">扬声器</text>
            </view>
        </view>
    </view>
</template>

<script>
    import {
        getFriendInfo
    } from '@/apis/interfaces/im.js'
    import * as CallLib from '@/uni_modules/RongCloud-CallWrapper/lib/index'

    export default {
        data() {
            return {
                targetId: '',
                mediaType: 0, // 0 语音  1 视频
                userInfo: {},
                isOut: false,
                connected: false,
                micStatus: false,
                speStatus: false,
                remoteRinging: false,
                ring: null
            }
        },
        onLoad(e) {
            this.targetId = e.targetId
            this.mediaType = e.mediaType
            getFriendInfo(this.targetId).then(res => {
                this.userInfo = res
            })
            this.startRing()
            // 监听通话链接状态
            uni.$once('onCallConnected', this.onCallConnected)
            uni.$once('onCallDisconnected', this.hangup)
            uni.$once('onRemoteUserRinging', () => {
                this.remoteRinging = true
            })
            uni.$once('onRemoteUserJoined', () => {
                this.remoteRinging = false
            })
        },
        computed: {
            windowWidth() {
                return uni.getSystemInfoSync().windowWidth
            },
            windowHeight() {
                return uni.getSystemInfoSync().windowHeight
            }
        },
        methods: {
            changeMicrophone() {
                CallLib.enableMicrophone(this.micStatus)
                this.micStatus = !this.micStatus
            },
            changeSpeaker() {
                CallLib.enableSpeaker(this.speStatus)
                this.speStatus = !this.speStatus
            },
            accept() {
                CallLib.accept()
            },
            onCallConnected() {
                // 关掉铃声
                this.stopRing()
                // 设置链接状态
                this.connected = true
                // 视频通话，才开摄像头
                if (this.mediaType == 1) {
                    const session = CallLib.getCurrentCallSession()
                    setTimeout(() => {
                        CallLib.setVideoView(session.targetId, this.$refs.bigVideoView.ref, 0,
                            false)
                        CallLib.setVideoView(session.mine.userId, this.$refs.smallVideoView.ref, 0,
                            true)
                    }, 200)
                }
            },
            hangup() {
                CallLib.hangup()
                this.stopRing()

                setTimeout(() => {
                    this.downRing()
                    uni.switchTab({
                        url: '/pages/im/index'
                    })
                }, 200);
            },
            toHome() {
                uni.switchTab({
                    url: '/pages/im/index'
                })
            },
            startRing() {
                const ring = uni.createInnerAudioContext()
                this.ring = ring
                ring.autoplay = true
                ring.loop = true
                ring.src = '/static/im/sounds/call-ring.mp3'
                ring.onEnded(() => {
                    ring.destroy()
                })
            },
            stopRing() {
                this.ring.stop()
            },
            downRing() {
                const ding = uni.createInnerAudioContext()
                ding.autoplay = true
                ding.src = '/static/im/sounds/call-down.mp3'
                ding.onEnded(() => {
                    ding.destroy()
                })
            }
        }
    }
</script>

<style scoped lang="scss">
    .call {
        display: flex;
        flex: 1;
        flex-direction: column;
        position: relative;

        .video {
            .bigVideoView {
                background: #000;
            }

            .smallVideoView {
                background: blue;
                position: absolute;
                right: 100rpx;
                top: 100rpx;
                width: 180rpx;
                height: 320rpx;
            }
        }

        .status {
            flex: 1;
            background: #666;
            width: 750rpx;
            position: fixed;
            bottom: 0;
            top: 0;

            .remote {
                flex: 1;
                width: 750rpx;
                display: flex;
                align-items: center;
                top: 300rpx;

                .mediaType {
                    font-size: 32rpx;
                    color: #aaa;
                }

                .nickname {
                    margin: 30rpx;
                    font-size: 44rpx;
                    color: #ffffff;
                }
            }
        }

        .buttons {
            position: fixed;
            bottom: 100rpx;
            width: 750rpx;
            display: flex;
            flex-direction: row;
            justify-content: space-around;
        }


        .btn {
            align-items: center;

            &.hangup {
                background: $text-price;
            }

            .icon {
                background: $main-color;
                width: 132rpx;
                height: 132rpx;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
            }

            .text {
                margin-top: 16rpx;
                color: #FFFFFF;
                font-size: 32rpx;
            }
        }
    }
</style>
