tree = [];

window.addEventListener('load', function () {
    console.log('All assets are loaded');

    if (sessionStorage.getItem("authorization") !== null) {
        console.log('Logged in');
        url_listener();
    } else {
        console.log('Not logged in');
        create_login_view();
    }
});

window.addEventListener('popstate', function () {
    url_listener();
});

function url_listener() {
    let curr_dir = findGetParameter('path');

    if (curr_dir !== null) {
        httpGetAsync('http://localhost:8080' + curr_dir, null, show_files);
    } else {
        httpGetAsync('http://localhost:8080', null, show_files);
    }
}

function login() {
    let username = document.getElementById("username").value;
    let password = document.getElementById("password").value;

    const xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function () {
        if (this.readyState === 4) {
            if (xmlHttp.status === 200) {
                console.log(username + ':' + JSON.parse(xmlHttp.responseText)['token'])
                sessionStorage.setItem("authorization", btoa(username + ':' + JSON.parse(xmlHttp.responseText)['token']));

                httpGetAsync('http://localhost:8080', null, show_files);
            }
        }
    }

    xmlHttp.open("POST", 'http://localhost:8080/login', true);
    xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlHttp.send('username=' + username + '&password=' + password);
}

function show_files(response, code) {
    document.getElementById("wrapper").innerHTML = '';
    if (code === 200) {
        // OK
        console.log(code);
        console.log(JSON.parse(response));
        create_file_view(JSON.parse(response));
    } else if (code === 401) {
        // Not logged in
        sessionStorage.removeItem('authorization');
        // location.reload();
    } else {
        // Error
        console.error(code);
        console.error(JSON.parse(response));
        sessionStorage.removeItem('authorization');
        // location.reload();
    }
}

function one_dir_back() {
    let curr_dir = findGetParameter('path');

    let dir = curr_dir.split('/');
    dir = dir.slice(0, dir.length - 1);
    curr_dir = dir.join('/');

    return curr_dir;
}

// function change_dir(name) {
//     let curr_dir = findGetParameter('path');
//
//     if(curr_dir === null) {
//         curr_dir = '';
//     }
//
//     if (name === '..') {
//         let dir = curr_dir.split('/');
//         dir = dir.slice(0, dir.length - 1);
//         curr_dir = dir.join('/');
//
//         httpGetAsync('http://localhost:8080' + curr_dir, null, show_files);
//         window.history.pushState('index', 'Filemanager', 'index.html?path=' + curr_dir);
//     } else {
//         httpGetAsync('http://localhost:8080' + curr_dir + '/' + name, null, show_files);
//         window.history.pushState('index', 'Filemanager', 'index.html?path=' + curr_dir + '/' + name);
//     }
// }

function load_file(name) {
    let curr_dir = findGetParameter('path');

    console.log(curr_dir + '/' + name);
}