$(document).ready(function(){

$("body").on("click", ".favorite", function(){
    let id = $(this).data("id");
    if ($(this).hasClass("active")){
        actionfav("del", id);
        $(this).removeClass("active");
    }
    else{
        actionfav("add", id);
        $(this).addClass("active");
    }
    return false;
});

$("body").on("click", ".basket-btn", function(){
    let id = $(this).data("id");
    let count = $(this).data("count");
    if ($(this).hasClass("active")){
        actionbasket("del", id, count);
        $(this).removeClass("active");
    }
    else{
        actionbasket("add", id, count);
        $(this).addClass("active");
    }
    return false;
});

});




function actionbasket(action, id, count){
    if (!count) count = 1;
    var basket = JSON.parse(getCookie("basket"));
    if (basket === null || !(basket instanceof Array))
        basket = [];

    var inArr = false;
    for (i=0;i<basket.length;i++){
        if(basket[i]["id"] == id){
            if (action == "plus") {basket[i]["count"]++;}
            if (action == "minus") {basket[i]["count"]--;}
            if (action == "del") {basket.splice(i,1);}
            inArr = true;
        }
    }
    if (action == "add" && !inArr) basket.push({"id": id, "count": count});
    var d = new Date();
    d.setMonth(d.getMonth() + 1);
    setCookie("basket", JSON.stringify(basket), d.toUTCString(), '/');
    return basket;
}

function refreshbasket(){
    $.get(location.href, {AJAX_BASKET: "Y"}, function(data){
        $(".basket").html(data);
    });
}


function actionfav(action, id){
    var favorite = JSON.parse(getCookie("favorite"));
    if (favorite === null || !(favorite instanceof Array))
        favorite = [];

    var inArr = false;
    for (i = 0; i < favorite.length; i++){
        if (favorite[i] == id){
            if (action == "del") {favorite.splice(i, 1);}
            inArr = true;
        } 
    }
    if (action == "add" && !inArr) favorite.push(id);
    var d = new Date();
    d.setMonth(d.getMonth() + 1);
    setCookie("favorite", JSON.stringify(favorite), d.toUTCString(), '/');
    $(".badge.bg-primary.rounded-pill").text(favorite.length);
    return favorite;
}

function setCookie(name, value, expires, path, domain, secure){
    document.cookie = name + "=" + escape(value) + 
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name){
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0){
        offset = cookie.indexOf(search);
        if (offset != -1){
            offset += search.length;
            end = cookie.indexOf(";", offset);
            if (end == -1){
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}