var id;

$(document).ready(function() {
        //initialize all modals           
        $('.modal').modal();
        $('.materialboxed').materialbox();

        if ($(window).width() > 600) {
            //large     
            $(".col").css("height", "100%");
        }
        else {
            //small    
            $(".col").css("height", "50%");
        }
    
        $("#modal1").modal("open");

    $(".btnanalyze").click(function() {
        analyzeUploaded();
    });
    
//AJAX

// 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);

  getAllShoppinglists();

function movePurchases(text) {
    firebase.auth().onAuthStateChanged(function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
        $.ajax({
            type: "POST",
            url: "/dones",
            data:{
                idtoken: idtoken,
                sl_id: id,
                billcontent: text
            },
            success(result){
              console.log(result);
            },
            error(err){
                console.error("Error: " + err);
            }
        });
    }).catch((error) => console.error("Get id token client error: ", error));}else{console.log("Check Auth error", user)}});
}

$(".test").click(function() {
    //movePurchases();
    getAllShoppinglists();
});

function analyzeUploaded() {
    var img = document.getElementById("blah");

    Tesseract.recognize(img).progress((progress) => {

        if (progress.status == "recognizing text") {
           loading = true;
        }
    }).then((result) => {
        loading = false;
        $(".output").text(result.text);
        $('#modal1').modal("open");
        movePurchases(result.text);
        $(".determinate").css("width", "0%");
    });
}

setInterval(updateProgress(), 500);

function updateProgress(percent) {
    if(loading == true) {
        $(".determinate").css("width", progress.progress * 100 + "%");
    }
    
}

function getAllShoppinglists() {
    firebase.auth().onAuthStateChanged(function(user){if(user){firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idtoken) {
        $.ajax({
            type: "GET",
            url: "/myshoppinglists",
            data:{
                idtoken: idtoken,
            },
            success(data){
                for(let item of data) {
                    $(".output").append("<a id='" + item.sl_id +"' class='collection-item'>" + item.name + "</a>")
                }
              
            },
            error(err){
                console.error("Error: " + err);
            }
        });
    }).catch((error) => console.error("Get id token client error: ", error));}else{console.log("Check Auth error", user)}});
}

$(document).on("click", ".collection-item", function() {
    id = $(this).closest("a").attr("id");

    $(".active").removeClass("active");

    $(this).addClass("active");
});


});

var loading = false;

/*function analyze() {
    var img = document.getElementById("canvas").toDataURL();

    Tesseract.recognize(img).progress((progress) => {

        if (progress.status == "recognizing text") {
           loading = true;
        }
    }).then((result) => {
        loading = false;
        $(".output").text(result.text);
        $('.modal').modal("open");

        $(".determinate").css("width", "0%");
    });
}
*/
