export function initHome() {
    setHeight();
    setBooks();
    setEventListeners();
    $('#note-table').DataTable({
        paging: false,
        ajax: {
            url: '/get-notes',
            type: 'POST'
        },
        columns: [
            { data: 'link' },
            { data: 'speaker.name' },
            { data: 'passage' },
            {
                data: 'date.date',
                render: DataTable.render.date("L")
            },
        ]
    });
    $('#shareBtn').on('click', openShareNote);
    $('#modal-backdrop').on('click', closeShareNote);
}

/**
 * Fetches data from '/js/data.json', assigns it to BOOKS, and handles errors.
 *
 * @return {void}
 */
export function setBooks() {
    fetch('js/data.json')
        .then((res) => {
            if (!res.ok) {
                throw new Error('HTTP Error: Status: ${res.status}');
            }
            return res.json();
        })
        .then((data) => {
            BOOKS = data;
        })
        .catch((error) => {
            console.log(error);
        })
}

/**
 * Sets event listeners for keyup events on the document and the '#notes' element.
 *
 * @return {void}
 */
export function setEventListeners() {
    document.addEventListener('keyup', function (event) {
        if (event.key == "F3") {
            openRef(false);
        }
    });

    document.querySelector('#notes').addEventListener('keyup', function (event) {
        let key = event.keyCode;

        if (key == 8 || key >= 48 && key <= 90 || key >= 96 && key <= 111 || key >= 186 && key <= 222) {
            textDirty = true;
            document.querySelector('#note-header-left h2').classList.add('dirty');
        }
    });
}

/**
 * Sets the height of various elements on the page based on the window's inner height.
 * Also initializes a datepicker and event listener for the search input field.
 *
 * @return {void}
 */
export function setHeight() {
    md = new markdownit({
        html: true,
        linkify: true,
        breaks: true
    });

    var body = document.querySelector('body');
    body.style.height = window.innerHeight + 'px';

    var cont = document.querySelector('#main');
    cont.style.height = (window.innerHeight) + 'px';

    var tabs = document.querySelector('.ref-tab');
    tabs.style.height = (window.innerHeight - 13) + 'px';

    var ref = document.querySelector('.ref');
    ref.style.height = (window.innerHeight - 60) + 'px';

    var noteList = document.querySelector('#note-list');
    noteList.style.height = (window.innerHeight - 60) + 'px';

    var notes = document.querySelector('.notes');
    notes.style.height = (window.innerHeight - 60) + 'px';

    var notePreview = document.querySelector('#notePreview');
    notePreview.style.height = (window.innerHeight - 50) + 'px';

    if ($('#noteDate')) {
        $('#noteDate').datepicker();
    }

    if ($('#query')) {
        document.querySelector('#query').addEventListener('keyup', function (event) {
            if (event.key == "Enter") {
                search();
            }
        });
    }
    if (!to) {
        to = setTimeout(saveNote, saveInterval);
    }
}
