jQuery(document).ready(function(jQuery) {
    if( window.location.pathname == '/scoreboard/'){
        load_scoreboard_index(); 
    }
    if( window.location.pathname == '/scoreboard/detail/'){
        load_scoreboard_detail(); 
    }
});

var scoreboard_reload;
var scoreboard_detail_reload;

function load_scoreboard_index() {

    var dataString = jQuery('#scoreboard_form').serialize();
    jQuery('#scoreboard_loader').html('<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>');
    jQuery.ajax({
        type: 'POST', // use jQuery_POST request to submit data
        url: '/wp-admin/admin-ajax.php', // URL to 'wp-admin/admin-ajax.php'
        data: {
            action: 'scoreboard_index',
            data: dataString,
        },
        success: function(data) {
            // console.log('success');
            jQuery('#scoreboard_frame').html(data);
            jQuery('.time_convert').each(
                function(index) {
                    //console.log(index + ": " + jQuery(this).html() + " | ");
                    jQuery(this).html(convert_times(jQuery(this).html()));
                }
            );
            jQuery('#scoreboard_loader').html('');
            if (scoreboard_reload) {
                clearTimeout(scoreboard_reload);
                reload = null;
            }
            scoreboard_reload = setTimeout(load_scoreboard_index, 20000);

        },
        error: function() {
            console.log(errorThrown); // error
        }
    });
}


function load_scoreboard_detail() {
    var game_id = jQuery('#game_id').val();
    var game_toggle = jQuery('#game_toggle').val();
    var team_1_toggle = jQuery('#team_1_toggle').val();
    var team_2_toggle = jQuery('#team_2_toggle').val();    
    var player_1_toggle = jQuery('#player_1_toggle').val();
    var player_2_toggle = jQuery('#player_2_toggle').val();

    var dataString = 'game_id=' + game_id + '&game_toggle=' + game_toggle + '&team_1_toggle=' + team_1_toggle + '&team_2_toggle=' + team_2_toggle + '&player_1_toggle=' + player_1_toggle + '&player_2_toggle=' + player_2_toggle;
    jQuery('#scoreboard_detail_loader').html('<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>');
    jQuery.ajax({
        type: 'POST', // use jQuery_POST request to submit data
        url: '/wp-admin/admin-ajax.php', // URL to 'wp-admin/admin-ajax.php'
        data: {
            action: 'scoreboard_detail',
            data: dataString,
        },
        success: function(data) {
            // console.log('success');
            jQuery('#scoreboard_detail_frame').html(data);
            jQuery('.time_convert').each(
                function(index) {
                    //console.log(index + ": " + jQuery(this).html() + " | ");
                    jQuery(this).html(convert_times(jQuery(this).html()));
                }
            );
            jQuery('#scoreboard_detail_loader').html('');
            if (scoreboard_detail_reload) {
                clearTimeout(scoreboard_detail_reload);
                scoreboard_detail_reload = null;
            }
            scoreboard_detail_reload = setTimeout(function() {load_scoreboard_detail(game_id);}, 20000);

        },
        error: function() {
            console.log(errorThrown); // error
        }
    });
}







function convert_times(timestamp) {

    const date = new Date(timestamp * 1000);

    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0' + minutes : minutes;
    var strTime = hours + ':' + minutes + ampm;
    return strTime;

}


function change_scoreboard_date(direction) {

    var date_string = jQuery('#scoreboard_date').val();

    var chooseDate = new Date(date_string);

    if (direction == 'prev') {
        chooseDate.setDate(chooseDate.getUTCDate() - 1);
    } else {
        chooseDate.setDate(chooseDate.getUTCDate() + 1);
    }

    var futureDate = chooseDate.getFullYear() + '-' + ('0' + (chooseDate.getMonth() + 1)).slice(-2) + '-' + ('0' + (chooseDate.getDate())).slice(-2);

    jQuery('#scoreboard_date').val(futureDate);

}