let even_checkbox = false;
let dozen_checkbox = false;
let combo_checkbox = false;
let street_checkbox = false;
let line_checkbox = false;

let global_stats_even_spins_value = 10;
let global_stats_dozens_spins_value = 15;
let global_stats_combo_spins_value = 10;
let global_stats_street_spins_value = 20;
let global_stats_line_spins_value = 10;

let even_button_all = false;
let dozen_button_all = false;
let combo_button_all = false;
let street_button_all = false;
let line_button_all = false;


function update_stats() {
    set_all_spins();
    calc_Evens();
    calc_Dozens();
    calc_Combo();
    calc_Street();
    calc_Line();
    
}
function testNothing()
{
    return;
}
function removeFromBoard()
{
    //stats_checkboxes();
    let checkbox = document.getElementById('checkbox_stats_id_stats_table_evens');
    if (checkbox) {
        if(!checkbox.checked)
        {
            let itemToRemove = document.getElementById('board_stats_even');
            let mainContainer = document.getElementById('roulette-table-container');
            // Remove the item from the main container
            if (itemToRemove) {
                mainContainer.removeChild(itemToRemove);
            }
        }
    }

    let ckdozens = document.getElementById('checkbox_stats_id_stats_table_dozens');
    if (ckdozens) {
        if(!ckdozens.checked)
        {
            let itemToRemove = document.getElementById('board_stats_dozen');
            let mainContainer = document.getElementById('roulette-table-container');
            // Remove the item from the main container
            if (itemToRemove) {
                mainContainer.removeChild(itemToRemove);
            }
        }
    }

    let ckcombo = document.getElementById('checkbox_stats_id_stats_table_combo');
    if (ckcombo) {
        if(!ckcombo.checked)
        {
            let itemToRemove = document.getElementById('board_stats_combo');
            let mainContainer = document.getElementById('roulette-table-container');
            // Remove the item from the main container
            if (itemToRemove) {
                mainContainer.removeChild(itemToRemove);
            }
        }
    }

    let ckstreet = document.getElementById('checkbox_stats_id_stats_table_street');
    if (ckstreet) {
        if(!ckstreet.checked)
        {
            let itemToRemove = document.getElementById('board_stats_street');
            let mainContainer = document.getElementById('roulette-table-container');
            // Remove the item from the main container
            if (itemToRemove) {
                mainContainer.removeChild(itemToRemove);
            }
        }
    }

    let ckline = document.getElementById('checkbox_stats_id_stats_table_line');
    if (ckline) {
        if(!ckline.checked)
        {
            let itemToRemove = document.getElementById('board_stats_line');
            let mainContainer = document.getElementById('roulette-table-container');
            // Remove the item from the main container
            if (itemToRemove) {
                mainContainer.removeChild(itemToRemove);
            }
        }
    }
}
function stats_checkboxes()
{
    // Stats Even checkbox
    let checkbox = document.getElementById('checkbox_stats_id_stats_table_evens');
    if (checkbox) {
        checkbox.onclick = function() {
            even_checkbox = this.checked;
        };
        if (even_checkbox) {
            checkbox.checked = true;
        } else {
            checkbox.checked = false;
        }
    }


    // Stats Dozen checkbox
    let checkboxDozen = document.getElementById('checkbox_stats_id_stats_table_dozens');
    if (checkboxDozen) {
        checkboxDozen.onclick = function() {
            dozen_checkbox = this.checked;
        };
        if (dozen_checkbox) {
            checkboxDozen.checked = true;
        } else {
            checkboxDozen.checked = false;
        }
    
    }

    // Stats Combo checkbox
    let checkboxCombo = document.getElementById('checkbox_stats_id_stats_table_combo');
    if (checkboxCombo) {
        checkboxCombo.onclick = function() {
            combo_checkbox = this.checked;
        };
        if (combo_checkbox) {
            checkboxCombo.checked = true;
        } else {
            checkboxCombo.checked = false;
        }
    }

        // Stats Street checkbox
        let checkboxStreet = document.getElementById('checkbox_stats_id_stats_table_street');
        if (checkboxStreet) {
            checkboxStreet.onclick = function() {
                street_checkbox = this.checked;
            };
            if (street_checkbox) {
                checkboxStreet.checked = true;
            } else {
                checkboxStreet.checked = false;
            }
        }

            // Stats Line checkbox
    let checkboxLine = document.getElementById('checkbox_stats_id_stats_table_line');
    if (checkboxLine) {
        checkboxLine.onclick = function() {
            line_checkbox = this.checked;
        };
        if (line_checkbox) {
            checkboxLine.checked = true;
        } else {
            checkboxLine.checked = false;
        }
    }

}
function set_all_spins() {
    if (even_button_all) {
        global_stats_even_spins_value = numbers.length;
        let textbox = document.getElementById('look_back_id_stats_table_evens');
        if (textbox) { textbox.value = global_stats_even_spins_value; }
        
    }
    if (dozen_button_all) {
        global_stats_dozens_spins_value = numbers.length;
        let textbox = document.getElementById('look_back_id_stats_table_dozens');
        if (textbox) { textbox.value = global_stats_dozens_spins_value; }
    }

    if (combo_button_all) {
        global_stats_combo_spins_value = numbers.length;
        let textbox = document.getElementById('look_back_id_stats_table_combo');
        if (textbox) { textbox.value = global_stats_combo_spins_value; }
    }

    if (street_button_all) {
        global_stats_street_spins_value = numbers.length;
        let textbox = document.getElementById('look_back_id_stats_table_street');
        if (textbox) { textbox.value = global_stats_street_spins_value; }
    }

    if (line_button_all) {
        global_stats_line_spins_value = numbers.length;
        let textbox = document.getElementById('look_back_id_stats_table_line');
        if (textbox) { textbox.value = global_stats_line_spins_value; }
    }
}

function even_set_all(id) {

    var overSpinsInput = document.getElementById('look_back_' + id);
    console.log("Made it to evens");
    console.log(`id: ` + id);
    if (overSpinsInput) {
        overSpinsInput.value = numbers.length;
        calc_Evens();
        even_button_all = true;
    }

};
function dozen_set_all(id) {
    var overSpinsInput = document.getElementById('look_back_' + id);
    if (overSpinsInput) {
        overSpinsInput.value = numbers.length;
        calc_Dozens();
        dozen_button_all = true;
    }
};
function combo_set_all(id) {
    var overSpinsInput = document.getElementById('look_back_' + id);
    if (overSpinsInput) {
        overSpinsInput.value = numbers.length;
        calc_Combo();
        combo_button_all = true;
    }
};
function street_set_all(id) {
    var overSpinsInput = document.getElementById('look_back_' + id);
    if (overSpinsInput) {
        overSpinsInput.value = numbers.length;
        calc_Street();
        street_button_all = true;
    }
};
function line_set_all(id) {
    var overSpinsInput = document.getElementById('look_back_' + id);
    if (overSpinsInput) {
        overSpinsInput.value = numbers.length;
        calc_Line();
        line_button_all = true;
    }
};

function stats_blank_page() {
    console.log("stats_blank_page()");
    let container = document.createElement('div');
    container.style.display = 'none';
    console.log("E_stats_blank_page()");
    console.log(`Global Spins: `, global_stats_even_spins_value);
    return container;
}

function evens_page() {
    console.log("evens_page()");
    const headerData = ['Bet Type', 'Misses in Row', 'Hits in Row', 'Hits', '% Hit'];
    const id = 'id_stats_table_evens';
    const title = 'Evens';
    const placeHolder = 'Look back spins';
    const buttonClickFunction = button_update_evens;
    const allButtonFunction = even_set_all;

    const myTable = createStyledTable(id, title, headerData, placeHolder, buttonClickFunction, global_stats_even_spins_value, even_checkbox, allButtonFunction);
    const div = document.createElement('div');
    div.appendChild(myTable);



    console.log("E_evens_page()");
    console.log(`Global Spins: `, global_stats_even_spins_value);
    return div;
}

function dozens_page() {
    const headerData = ['Bet Type', 'Misses in Row', 'Hits in Row', 'Hits', '% Hit'];
    const id = 'id_stats_table_dozens';
    const title = 'Dozens';
    const placeHolder = 'Look back spins';
    const buttonClickFunction = button_update_dozens;
    const allButtonFunction = dozen_set_all;

    const myTable = createStyledTable(id, title, headerData, placeHolder, buttonClickFunction, global_stats_dozens_spins_value, dozen_checkbox, allButtonFunction);
    const div = document.createElement('div');
    div.appendChild(myTable);
    return div;
}

function combo_page() {
    const headerData = ['Bet Type', 'Misses in Row', 'Hits in Row', 'Hits', '% Hit'];
    const id = 'id_stats_table_combo';
    const title = 'Combinations';
    const placeHolder = 'Look back spins';
    const buttonClickFunction = button_update_combo;
    const allButtonFunction = combo_set_all;

    const myTable = createStyledTable(id, title, headerData, placeHolder, buttonClickFunction, global_stats_combo_spins_value, combo_checkbox, allButtonFunction);
    const div = document.createElement('div');
    div.appendChild(myTable);
    return div;
}

function street_page() {
    const headerData = ['Bet Type', 'Misses in Row', 'Hits in Row', 'Hits', '% Hit'];
    const id = 'id_stats_table_street';
    const title = 'Streets';
    const placeHolder = 'Look back spins';
    const buttonClickFunction = button_update_street;
    const allButtonFunction = street_set_all;

    const myTable = createStyledTable(id, title, headerData, placeHolder, buttonClickFunction, global_stats_street_spins_value, street_checkbox, allButtonFunction);
    const div = document.createElement('div');
    div.appendChild(myTable);
    return div;
}

function line_page() {
    const headerData = ['Bet Type', 'Misses in Row', 'Hits in Row', 'Hits', '% Hit'];
    const id = 'id_stats_table_line';
    const title = 'Lines';
    const placeHolder = 'Look back spins';
    const buttonClickFunction = button_update_line;
    const allButtonFunction = line_set_all;

    const myTable = createStyledTable(id, title, headerData, placeHolder, buttonClickFunction, global_stats_line_spins_value, line_checkbox, allButtonFunction);
    const div = document.createElement('div');
    div.appendChild(myTable);
    return div;
}

function createStyledTable(id, title, headerData, pHolder = 'Look back spins', buttonFunction = nullClick, lookbackValue, checkboxStatus, allButtonFunction) {

    console.log("createStyledTable()");

    // Main container
    let container = document.createElement('div');
    container.id = 'container-' + id;
    container.style.marginLeft = '25px';
    container.style.marginBottom = '15px';
    container.style.fontSize = '30px';

    // Sub-container for title, checkbox, and label
    let subContainer = document.createElement('div');
    subContainer.style.marginTop = '25px';
    subContainer.style.marginLeft = '0px';
    subContainer.style.marginBottom = '15px';
    subContainer.style.fontSize = '30px';
    subContainer.style.display = 'flex';  // Use flexbox
    subContainer.style.alignItems = 'center';  // Align items vertically in the center

    var sectionTitle = document.createElement('h2');
    sectionTitle.innerText = title;
    sectionTitle.style.margin = '0';  // Remove default margins
    subContainer.appendChild(sectionTitle);

    // Checkbox
    let checkbox = document.createElement('input');
    checkbox.id = `checkbox_stats_` + id;
    checkbox.type = 'checkbox';
    checkbox.style.transform = 'scale(1.5)';  // Adjust scaling as needed
    checkbox.style.marginRight = '20px';
    checkbox.style.marginLeft = '40px';
    checkbox.style.marginTop = '0';  // Align with label
    checkbox.addEventListener('click', function() {
        removeFromBoard();
    });

    subContainer.appendChild(checkbox);

    // Labels
    let label1 = document.createElement('label');
    label1.textContent = 'Display under Board';
    label1.style.fontSize = '30px';  // Adjust font size as needed
    label1.style.marginLeft = '10px';
    label1.setAttribute('for', `checkbox_stats_` + id);
    subContainer.appendChild(label1);

    container.appendChild(subContainer);

    // Textbox
    const textBox = document.createElement('input');
    textBox.setAttribute('type', 'text');
    textBox.setAttribute('placeholder', pHolder);
    textBox.style.padding = '10px';
    textBox.style.marginLeft = '10px';
    textBox.style.borderRadius = '4px';
    textBox.style.border = '1px solid #ccc';
    textBox.style.width = '50px';
    textBox.style.height = '30px';
    textBox.style.fontSize = '25px';
    textBox.style.color = 'black';
    textBox.id = 'look_back_' + id;
    textBox.value = lookbackValue;
    textBox.classList.add('textbox-container');
    textBox.classList.add('triggerGroups');
    console.log(`Global Value: `, lookbackValue);
    container.appendChild(textBox);

    // Update Button
    var statButton = document.createElement('button');
    statButton.id = 'update_button_' + id;
    statButton.style = 'width: 150px; height: 50px; margin: 5px 10px 5px 10px; font-size: 30px;';
    statButton.textContent = 'Update';
    statButton.onclick = buttonFunction;
    container.appendChild(statButton);

    // All Button
    var allButton = document.createElement('button');
    allButton.id = 'update_button_all_' + id;
    allButton.style = 'width: 150px; height: 50px; margin: 5px 10px 5px 10px; font-size: 30px;';
    allButton.textContent = 'All Spins';
    allButton.onclick = function () { allButtonFunction(id); };
    container.appendChild(allButton);


    var overSpins = document.createElement('span');
    overSpins.id = 'over_spins_text_' + id;
    overSpins.innerHTML = '';
    container.appendChild(overSpins);

    // Create table
    var table = document.createElement('table');
    table.className = "table-alert";
    table.style.width = '95%';
    table.style.maxWidth = '95%';
    table.style.borderCollapse = 'collapse';
    table.style.marginLeft = '10px';
    table.style.marginRight = '10px';
    table.style.marginBottom = '10px';
     table.style.color = 'black';


    table.id = id;

    // Sample headers
    var headers = headerData;
    var headerRow = document.createElement('tr');

    headers.forEach(function (headerText) {
        var header = document.createElement('th');
        header.textContent = headerText;
        header.style.padding = '8px';
        header.style.border = '1px solid #ddd';
        header.style.backgroundColor = '#ed9542';
        header.style.whiteSpace = 'nowrap';
        headerRow.appendChild(header);
    });

    table.appendChild(headerRow);

    // Sample data
    var data = [
    ];

    // Create and append data rows
    data.forEach(function (rowData) {
        var row = document.createElement('tr');


        rowData.forEach(function (cellData, index) {
            var cell = document.createElement('td');
            cell.textContent = cellData;
            cell.style.padding = '8px';
            cell.style.border = '1px solid #ddd';
            if (index === 0) {
                cell.style.textAlign = 'left'; // Align the first column to the left
            } else {
                cell.style.textAlign = 'center'; // Align other columns to the center
            }
            row.appendChild(cell);
        });

        table.appendChild(row);
    });
    container.appendChild(table);

    console.log("E_createStyledTable()");
    console.log(`Global Spins: `, global_stats_even_spins_value);
    return container;
}
function updateTableData(id, newData) {

    console.log("updateTableData()");
    var table = document.getElementById(id);

    if (!table) {
        console.log('Table not found!', id);
        console.log("E_updateTableData()");
        console.log(`Global Spins: `, global_stats_even_spins_value);
        return;
    }

    // Delete existing table rows
    if (table && table.rows) {
        while (table.rows.length > 1) {
            table.deleteRow(1);
        }
    }

    newData.forEach(function (rowData) {
        var row = table.insertRow(-1);
        rowData.forEach(function (cellData, index)  {
            var cell = row.insertCell(-1);
            cell.textContent = cellData;
            if (index === 0) {
                cell.style.textAlign = 'left'; // Align the first column to the left
            } else {
                cell.style.textAlign = 'center'; // Align other columns to the center
            }
        });
    });

    console.log("E_updateTableData()");
}

















