<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Scholar</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,600,700" rel="stylesheet">
    <link rel="stylesheet" href="{{asset('css/lumen.min.css')}}">
    <link rel="stylesheet" href="{{asset('css/toastr.min.css')}}">

    {{-- Bootstrap --}}
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js" integrity="sha384-1CmrxMRARb6aLqgBO7yyAxTOQE2AKb9GfXnEo760AUcUmFx3ibVJJAzGytlQcNXd" crossorigin="anonymous"></script>

    <script src="{{asset('js/jquery-ui.min.js')}}"></script>
    <script src="{{asset('js/jquery.form.js')}}"></script>
    <script src="{{asset('js/toastr.min.js')}}"></script>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <link rel="stylesheet" href="/css/meeting.css">

    {{-- WS --}}
    <script src="/js/sockjs.min.js"></script>
    <script src="/js/stomp.min.js"></script>
</head>

<body class="p-0 m-0">
    <nav class="navbar navbar-expand-sm navbar-dark bg-primary">
        <a class="navbar-brand" href="#">Scholar</a>
        <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#collapsibleNavId"
            aria-controls="collapsibleNavId" aria-expanded="false" aria-label="Toggle navigation"></button>
        <div class="collapse navbar-collapse" id="collapsibleNavId">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
                <li class="nav-item active">
                    <a class="nav-link" href="{{route('pro-dashboard')}}">Dashboard <span
                            class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{route('pro-index')}}">Pros</a>
                </li>
            </ul>
            <form class="form-inline my-2 my-lg-0">
                <input class="form-control mr-sm-2" type="text" placeholder="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
            <ul class="navbar-nav mt-2">
                <li class="nav-item">
                    <a class="nav-link" href="{{route('pro-logout')}}">Log out</a>
                </li>
            </ul>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12 p-0">
                @yield('content')
            </div>
        </div>
    </div>

    <div class="modal" id="incomingCallModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Incoming Call</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <p>Click <b>Accept</b> to join the call.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success px-4" onclick="joinCall()">Answer</button>
                    <button type="button" class="btn btn-secondary px-4" data-dismiss="modal">Reject</button>
                </div>
            </div>
        </div>
    </div>

    <script src="{{asset('js/moe.js')}}"></script>

    <script>
        var stompClient = null;

        function setConnected(connected) {
            console.log("User is connected");
            $("#connect").prop("disabled", connected);
            $("#disconnect").prop("disabled", !connected);
            if (connected) {
                $("#conversation").show();
            }
            else {
                $("#conversation").hide();
            }
            $("#greetings").html("");
        }

        function connect() {
            console.log("connecting...");
            var socket = new SockJS('http://localhost:8080/ws');
            stompClient = Stomp.over(socket);
            stompClient.connect({}, function (frame) {
                console.log("Connected");
                setConnected(true);
                console.log('Connected: ' + frame);

                // stompClient.send("/app/update-participant-status", {}, JSON.stringify({meetingUid:"mymeetinguid", status:'mystatus'}));

                // stompClient.subscribe('/user/topic/on-participant-status-change', function (message) {
                //     console.log("Participant status changed: ", message);
                // });

                stompClient.subscribe("/user/topic/on-pro-incoming-call", function(message){
                    console.log("incoming call:", message);

                    message = JSON.parse(message.body);
                    console.log(message.meetingUid);

                    $('#incomingCallModal')
                        .data('meetingUid', message.meetingUid)
                        .modal('show');

                });

                // connect
                stompClient.send("/app/pro-connect", {}, JSON.stringify({sessionKey: "<?= request()->cookie('sessionKey') ?>"}));

                console.log('Pro connected')

            });
        }

        connect();

        function joinCall() {
            var modal = $('#incomingCallModal');
            window.location.href = '/pro/meeting/' +
                modal.data('meetingUid');
        }

        function disconnect() {
            if (stompClient !== null) {
                stompClient.disconnect();
            }
            setConnected(false);
            console.log("Disconnected");
        }

        function sendName() {
            stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
        }

        function showGreeting(message) {
            $("#greetings").append("<tr><td>" + message + "</td></tr>");
        }

        $(function () {
            $("form").on('submit', function (e) {
                e.preventDefault();
            });
            $( "#connect" ).click(function() { connect(); });
            $( "#disconnect" ).click(function() { disconnect(); });
            $( "#send" ).click(function() { sendName(); });
        });
    </script>

</body>

</html>
