var currentVencCodec = 96; // 96=H.264, 265=H.265

function browserSupportsH265WebRTC() {
    var ua = navigator.userAgent;
    if (/Chrome/i.test(ua) && !/Edg\//i.test(ua)) return true;
    if (/Safari/i.test(ua) && !/Chrome/i.test(ua)) return true;
    return false;
}

function hideWebRTCPreview() {
    $("#osdWebRTCBox_card").addClass("display_off");
    if (pc) {
        pc.close();
        pc = null;
    }
    var videoEl = document.getElementById("video");
    if (videoEl) {
        videoEl.srcObject = null;
    }
}

function DisplayWebRTC(){
    // OSD 页仅在 B 帧模式开启时才显示 WebRTC 预览；关闭 B 帧时保留 MJPEG 预览。
    $.ajax({
        type: "GET",
        url: "/get_sys",
        dataType: "xml",
        cache: false,
        success: function (data) {
            var bFrameMode = $(data).find("b_frame_mode").text();
            if (bFrameMode != "1") {
                hideWebRTCPreview();
                return;
            }

            // 根据当前码流配置判断是否需要显示 WebRTC 预览（H.264/H.265 且 WebRTC 预览开启，且浏览器支持对应编码）
            $.ajax({
                type: "GET",
                url: "/get_output?input=" + input + "&output=" + output,
                dataType: "xml",
                cache: false,
                success: function (data) {
                    var venc_codec = $(data).find("venc_codec").text();
                    var webrtcStatus = $(data).find("webrtc_p2p_http_enable").text();
                    var canPreview = (venc_codec == 96) || (venc_codec == 265 && browserSupportsH265WebRTC());

                    if (canPreview && webrtcStatus == 1) {
                        currentVencCodec = parseInt(venc_codec);
                        $("#osdWebRTCBox_card").removeClass("display_off");
                        connect();
                    } else {
                        hideWebRTCPreview();
                    }
                }
            });
        },
        error: function () {
            hideWebRTCPreview();
        }
    });
}

function InitOsdWebRTCContent(){
    var $container = $('.page-body .container-xl');
    if (!$container.length) return;
    if ($('#osdWebRTCBox_card').length) return;

    var title = 'status.previewWebRTC';
    if (typeof i18n !== 'undefined' && typeof i18n.t === 'function') {
        title = i18n.t('status.previewWebRTC');
    }

    var content = '';
    content += "<div id='osdWebRTCBox_card' class='card mb-3 display_off'>";
    content += "  <div class='card-header'>";
    content += "    <h3 class='card-title' data-i18n='status.previewWebRTC'>" + title + "</h3>";
    content += "  </div>";
    content += "  <div class='card-body'>";
    content += "    <div id='osdWebRTCBox_div' class='video-container'>";
    content += "      <video id='video' autoplay playsinline muted></video>";
    content += "    </div>";
    content += "  </div>";
    content += "</div>";

    $container.prepend(content);

    // 只翻译新插入的卡片，避免重复初始化整页语言选择器
    var card = document.getElementById('osdWebRTCBox_card');
    if (card && typeof i18n !== 'undefined') {
        if (typeof i18n.translate === 'function') {
            i18n.translate(card);
        } else if (typeof i18n.init === 'function') {
            i18n.init();
        }
    }
}

let pc = null;

async function connect() {
    if (pc) return;

    const group = typeof input !== 'undefined' ? input : 0;
    const stream = typeof output !== 'undefined' ? output : 0;

    try {
        pc = new RTCPeerConnection({
            iceServers: []
        });

        pc.ontrack = (event) => {
            const video = document.getElementById('video');
            if (event.streams && event.streams[0]) {
                video.srcObject = event.streams[0];
            }
        };

        pc.addTransceiver('video', { direction: 'recvonly' });
        pc.addTransceiver('audio', { direction: 'recvonly' });

        let offer = await pc.createOffer();

        // Modify SDP to prefer the configured codec (H264 or H265)
        let sdp = offer.sdp;
        const lines = sdp.split('\r\n');
        let mLineIndex = -1;
        let preferredPayloadTypes = [];
        const codecName = (currentVencCodec == 265) ? 'H265' : 'H264';
        const codecRegex = new RegExp('^a=rtpmap:(\\d+) ' + codecName + '/');

        for (let i = 0; i < lines.length; i++) {
            if (lines[i].startsWith('m=video')) {
                mLineIndex = i;
            }
            const match = lines[i].match(codecRegex);
            if (match) {
                preferredPayloadTypes.push(match[1]);
            }
        }

        if (mLineIndex >= 0 && preferredPayloadTypes.length > 0) {
            const mLine = lines[mLineIndex];
            const parts = mLine.split(' ');
            const proto = parts.slice(0, 3).join(' ');
            const allPTs = parts.slice(3);
            const otherPTs = allPTs.filter(pt => !preferredPayloadTypes.includes(pt));
            const newPTs = [...preferredPayloadTypes, ...otherPTs];
            lines[mLineIndex] = proto + ' ' + newPTs.join(' ');
            sdp = lines.join('\r\n');
        }

        offer = { type: 'offer', sdp: sdp };
        await pc.setLocalDescription(offer);

        await new Promise((resolve) => {
            if (pc.iceGatheringState === 'complete') {
                resolve();
            } else {
                pc.onicegatheringstatechange = () => {
                    if (pc.iceGatheringState === 'complete') {
                        resolve();
                    }
                };
                setTimeout(resolve, 3000);
            }
        });

        const url = '/webrtc_stream?group=' + group + '&stream=' + stream + '&action=offer';

        const response = await fetch(url, {
            method: 'POST',
            headers: { 'Content-Type': 'text/plain' },
            body: pc.localDescription.sdp
        });

        const result = await response.json();

        if (result.error) {
            throw new Error(result.error);
        }

        const answer = { type: 'answer', sdp: result.sdp };
        await pc.setRemoteDescription(answer);

    } catch (error) {
        if (pc) {
            pc.close();
            pc = null;
        }
        const video = document.getElementById('video');
        if (video) {
            video.srcObject = null;
        }
    }
}
