$(document).ready(function () {



    // Initialize Firebase
    var config = {
        apiKey: "AIzaSyCuvwf78cmSDoZ2yS4XxHZhnjUn7yIHYfw",
        authDomain: "test-667ca.firebaseapp.com",
        databaseURL: "https://test-667ca.firebaseio.com",
        projectId: "test-667ca",
        storageBucket: "test-667ca.appspot.com",
        messagingSenderId: "221332577314"
    };

    firebase.initializeApp(config);


    var segment_str = window.location.pathname;
    var segment_array = segment_str.split('/');
    var last_segment = segment_array.pop();

    //verifyInvite(last_segment);
    getList(last_segment);
    verifyInvite(last_segment);


    function verifyInvite(link) {
        //alert("Ore Link", link)
        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                firebase.auth().currentUser.getIdToken( /* forceRefresh */ true).then(function (idtoken) {
                    $.ajax({
                        type: "POST",
                        url: "/invitemember",
                        data: {
                            idtoken: idtoken,
                            link: link
                        },
                        success() {
                            window.location.href = "/dash/" + idtoken
                            //alert("Success");
                        },
                        error(err) {
                            console.error("Error: " + err);
                            //alert("Error");
                            window.location.href = "/dash/" + idtoken
                        }
                    });
                }).catch((error) => console.error("Get id token client error: ", error));
            } else {
                console.log("Check Auth error", user)
            }
        });
    }

    function getList(link) {
        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                firebase.auth().currentUser.getIdToken( /* forceRefresh */ true).then(function (idtoken) {
                    $.ajax({
                        type: "GET",
                        url: "/shoppinglistsbylink?link=" + link,
                        data: {
                            idtoken: idtoken
                        },
                        success(data) {
                            console.log(data);

                            $(".s_name").append(data.find(function (obj) {
                                return obj.invitelink == link
                            }).name);
                            $(".s_desc").append(data.find(function (obj) {
                                return obj.invitelink == link
                            }).description);
                            $(".s_link").append(data.find(function (obj) {
                                return obj.invitelink == link
                            }).invitelink);
                        },
                        error(err) {
                            console.error("Error: " + err);
                        }
                    });
                }).catch((error) => console.error("Get id token client error: ", error));
            } else {
                console.log("Check Auth error", user)
            }
        });
    }

    $(".btn_verify").click(function () {
        var segment_str = window.location.pathname;
        var segment_array = segment_str.split('/');
        var last_segment = segment_array.pop();
        verifyInvite(last_segment);
    })


});