// login

var setting = {
    username: "huashe1",
    password: "huashe123",
    ip: "10.55.134.3",
    host: "http",
    port: "6699",
};

setting.URL = setting.host + "://" + setting.ip + ":" + setting.port;

localStorage.setItem("url", setting.URL);

window.onload = () => {
    window.addEventListener(
        "message",
        async(e) => {
            // 指定域名 防止xss 攻击
            //   if (e.origin != "") return;

            if (e.data === "onload") {
                // 登陆 && 保活
                var package = new Package(setting.URL, {
                    username: setting.username,
                    password: setting.password,
                });
                if (localStorage.getItem('token')) {
                    await package.keepalive();
                } else {
                    await package.login();
                }
            }
            // 打开单兵视频
            if (e.data.key === "danbing") {
                const meeting = new Meeting();
                await meeting.getLocalUserInfo();
                const res = await meeting.queryUser(e.data.value);

                meeting.localUserInfo.name = "danbing_Meeting";

                await meeting.creatMeeting();
                await meeting.startInvite({
                    userName: res.userName,
                    userId: res.userCode,
                    region: res.paasId,
                    type: "single",
                    // speak:'true',
                    number: res.userPhone,
                    deviceId: e.data.deviceId,
                    channelId: e.data.channelId,
                });
                $(".video-container").css({
                    visibility: "visible"
                })
                await meeting.openClient(
                    () => {

                        $(".video-action").one("click", () => {
                            meeting.closeClient();
                            meeting.closeVideo();
                            meeting.deleteMeeting();
                            $(".video-container").css({
                                visibility: "hidden"
                            })
                            window.parent.postMessage("closeIframe", "*");
                        })
                        meeting.playVideo({
                            type: "single",
                            number: res.userPhone,
                            deviceId: e.data.deviceId,
                            channelId: e.data.channelId,
                        })
                    }
                )
            } else if (e.data.key === "video") {
                const meeting = new Meeting();
                console.log(e.data.channelId);
                $(".video-container").css({
                    visibility: "visible"
                })
                await meeting.openClient(
                    () => {
                        $(".video-action").one("click", () => {
                            meeting.closeClient();
                            meeting.closeVideo();
                            meeting.deleteMeeting();
                            $(".video-container").css({
                                visibility: "hidden"
                            })
                            window.parent.postMessage("closeIframe", "*");
                        })
                        meeting.playVideo({
                            type: "single",
                            deviceId: e.data.deviceId,
                            channelId: e.data.channelId,
                        })
                    }
                )
            }

            // 视频会议
            if (e.data === "creatMeeting") {
                const meeting = new Meeting();

                // close video meeting iframe
                $("#closeMeAction").click(() => {
                    try {
                        meeting.closeClient();
                        meeting.closeVideo();
                        meeting.deleteMeeting();

                    } catch (e) {

                    }

                    $(".video-container").css({
                        visibility: "hidden"
                    })
                    $(".video-user-content").css({
                        display: "none"
                    })
                    $(".user-list-container").css({
                        visibility: "hidden"
                    })
                    window.parent.postMessage("closeIframe", "*");
                });

                $(".user-list-container").css({
                    visibility: "visible"
                })

                // 创建会议并打开窗口
                $("#createMeAction").click(async() => {
                    $(".video-user-content").css({
                        display: "flex"
                    })
                    await meeting.getLocalUserInfo();
                    // const res = await meeting.queryUser(e.data.value);
                    meeting.localUserInfo.name = "video_Meeting";
                    await meeting.creatMeeting();
                    // await meeting.startInvite();
                    await meeting.openClient();
                    $(".video-container").css({
                        visibility: "visible"
                    })
                });
                $(".video-action").click(() => {
                    try {
                        meeting.closeClient();
                        meeting.closeVideo();
                        meeting.deleteMeeting();
                    } catch (e) {}

                    $(".video-container").css({
                        visibility: "hidden"
                    })
                })

                // 添加人员, 并播放视频
                $(".user-list").click(async(e) => {
                    console.log(e, e.target, e.target.className, e.target.dataset, "+++");
                    if (e.target.className === "videoAction" && e.target.dataset.code) {
                        const res = await meeting.queryUser(e.target.dataset.code);
                        await meeting.startInvite({
                            userName: res.userName,
                            userId: res.userCode,
                            region: res.paasId,
                            type: "client",
                            // speak:'true',
                            number: res.userPhone,
                            deviceId: res.deviceId,
                            channelId: res.chnId,
                        });
                        // await meeting.playVideo(meeting.InvitedMembers);
                        meeting.playVideo({
                            type: "client",
                            number: res.userPhone,
                            userId: res.userCode,
                            deviceId: res.deviceId,
                            channelId: res.chnId,
                        })
                    }
                });
            }
            if (e.data === "openClient") {
                await meeting.openClient();
            }
        },
        false
    );
};