<div class="firebase_container">
    <script src="https://www.gstatic.com/firebasejs/7.16.1/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.16.1/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.16.1/firebase-messaging.js"></script>
    <script>
        const firebaseConfig = {
            apiKey: "AIzaSyCW3q23Sv_u_xudkVFeiHj1SiF1yFbUvUU",
            authDomain: "legal-346508.firebaseapp.com",
            databaseURL: "https://legal-346508-default-rtdb.firebaseio.com",
            projectId: "legal-346508",
            storageBucket: "legal-346508.appspot.com",
            messagingSenderId: "629299014900",
            appId: "1:629299014900:web:ce58e007cbcce538cb82a1",
            measurementId: "G-ZQ7EJNS37X"
        };

        // Initialize Firebase
        let init = firebase.initializeApp(firebaseConfig);
        let token = firebase.installations(init).getToken();
        let messaging = firebase.messaging();

        messaging.requestPermission().then(async function () {
            const token= await messaging.getToken();
            /** Send token to database*/
            const response = await ajax({
                url: "{{route('users.saveToken')}}",
                data: { device_token: token },
                method: 'POST',
                dataType: 'json',
            });
            if(response.status === true){
                localStorage.setItem('save_token_done', true)
            }
        }).catch(function (error) {
            console.log('Opps Unable get permission to notify.', error)
        });

        messaging.onMessage((payload) => {
            console.log('Message received. ', payload);

            if (payload.data && payload.data['gcm.notification.queue_id'] && payload.data['gcm.notification.shop_id']) {
                if(shopId && shopId == payload.data['gcm.notification.shop_id']){
                    console.log({shopId, 'ss': payload.data['gcm.notification.shop_id']});

                    if($('.your_turn_modal').length && payload.data['gcm.notification.your_turn']) {
                        $('.your_turn_modal').modal();
                    }

                    if($('.your_turn_modal').length && payload.data['gcm.notification.reset_queue']) {
                        $('.your_turn_modal .modal-body').html(`
                    <div class="alert alert-danger text-center"><i class="fa fa-times"></i> `
                            +payload.data['gcm.notification.message']+
                            `</div>`);
                        $('.your_turn_modal').modal();

                    }

                    if($('#countdown-number').length && payload.data['gcm.notification.decrease']) {
                        let count= $('#countdown-number').html()
                        count= --count >= 0? count : 0
                        console.log({count});
                        $('#countdown-number').html(count)
                    }
                }
            }
            // ...
        });
    </script>

</div>
<script>
    $(document).ready(function () {
        setTimeout(function () {
            let saveTokenDone = localStorage.getItem('save_token_done');
            if(saveTokenDone === 'true'){
                $('.firebase_container').remove();
                localStorage.setItem('save_token_done', 'false');
            }
        });
    })

</script>
