// Header pogas onclick() kurs nomaina body data-bs-theme data attribute par dark vai light
function changePageMode() {
    var body = $("body");
    if(body[0].attributes[0].nodeValue == "dark"){
        body.attr("data-bs-theme", "light");
    }
    else{
        body.attr("data-bs-theme", "dark");
    }
}
// ajax prieks blackjack
function blackjack(choice){

    $.ajax({
        url: baseURL + 'blackjack/submit',
        method: 'POST',
        dataType: 'json',
        data: {
            "choice": choice
        },

        success: function(response) {
            console.log(response);
            if(response.standResponse == false){
                if(response.lost == true){
                    $('#playerCardRow').append('<div class="col cardImg"><img src="/kartis/' + JSON.parse(response.playerCardToAdd)['filename'] + '" alt="Karts" class="img-fluid playingCards"></div>');
                    const modalElement = document.getElementById('lostModal');
                    const modal = new bootstrap.Modal(modalElement);
                    modal.show();
                }
                else if (response.lost == false){
                    //response.playerCardToAdd
                    
                    $('#playerCardRow').append('<div class="col cardImg"><img src="/kartis/' + JSON.parse(response.playerCardToAdd)['filename'] + '" alt="Karts" class="img-fluid playingCards"></div>');
                }
            } else if(response.standResponse == true) {
                console.log(response.winner);
                if(response.winner == 'player'){
                    let dealerCards = JSON.parse(response.dealerCards);
                    $('#dealerRow').html('');
                    
                    for(let i = 0; i < dealerCards.length; i++){
                        $('#dealerRow').append('<div class="col cardImg"><img src="/kartis/' + dealerCards[i]['filename'] + '" alt="Karts" class="img-fluid playingCards"></div>');
                    }
                    
                    const modalElement = document.getElementById('wonModal');
                    const modal = new bootstrap.Modal(modalElement);
                    modal.show();
                }
                if(response.winner == 'dealer'){
                    let dealerCards = JSON.parse(response.dealerCards);
                    $('#dealerRow').html('');
                    
                    for(let i = 0; i < dealerCards.length; i++){
                        $('#dealerRow').append('<div class="col cardImg"><img src="/kartis/' + dealerCards[i]['filename'] + '" alt="Karts" class="img-fluid playingCards"></div>');
                    }
                    const modalElement = document.getElementById('lostModal');
                    const modal = new bootstrap.Modal(modalElement);
                    modal.show();
                }
                
            }
            
        }
    })
}
// vienkarsi palaiz     $session = service('session');
//                      $session->destroy();
// jo kaut kads aids ir ielikt php bloku ieksa js + better scaling
function logout(){
    $.ajax({
        url: baseURL + 'login/logout',
        method: 'POST',
        success: function(){
            location.reload();
        }
    })
}
window.onload = function(){
    // kad logged in ieksa header ir php kas uz define startBlackjack ka true
    // ja es zinu ka tas nav optimali
    if(startBlackjack){
        $.ajax({
            url: baseURL + 'blackjack/start',
            method: 'POST',
            //data: 
            success: function(response){
                // japabeidz ka dealer dabu 21 no sakuma
                // vajag padod winner var 
                console.log(response.dealer21);
                if(response.dealer21){
                    let playerCards = JSON.parse(response.message);
                    let dealerCards = JSON.parse(response.dealerCards);
                    let html = '';
                    for(let i = 0; i < playerCards.length; i++){
                        html += '<div class="col cardImg"><img src="/kartis/' + playerCards[i]['filename'] + '" alt="Karts2" class="img-fluid playingCards"></div>';
                        $('#playerCardRow').html(html);
                    }
                    $('#dealerRow').html('');
                    
                    for(let i = 0; i < dealerCards.length; i++){
                        $('#dealerRow').append('<div class="col cardImg"><img src="/kartis/' + dealerCards[i]['filename'] + '" alt="Karts" class="img-fluid playingCards"></div>');
                    }


                }
                let playerCards = JSON.parse(response.message);
                //console.log($playerCards);
                let html = '';
                for(let i = 0; i < playerCards.length; i++){
                    html += '<div class="col cardImg"><img src="/kartis/' + playerCards[i]['filename'] + '" alt="Karts2" class="img-fluid playingCards"></div>';
                    $('#playerCardRow').html(html);
                }
            }
        });
    }
};