const { TouchBar, shell } = require('electron');
const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar

let mainWindow, port;

// Home button
const home = new TouchBarButton({
    label: 'Home',
    backgroundColor: '#0d6efd',
    click: () => {
        mainWindow.loadURL(`http://localhost:${port}/`);
    }
});


let loginButton = new TouchBarButton({
    label: 'Sign In',
    backgroundColor: '#0d6efd',
    url: `http://localhost:${port}/signin`,
    click: () => {
        if (loginButton.url) {
            mainWindow.loadURL(loginButton.url);
        } else {
            mainWindow.loadURL(`http://localhost:${port}/signin`);
        }
    }
});


function setWindowTouch(window, newPort) {
    mainWindow = window;
    port = newPort;
}

function setUserTouch(user) {
    loginButton.label = 'Logout ' + user.username;
    loginButton.url = `http://localhost:${port}/logout`;

    let additionalButtons;

    if (user.role === 'basic') {
        additionalButtons = [
            new TouchBarButton({
                label: 'Books',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/books/`);
                }
            }),
        ];
    } else if (user.role === 'advanced') {
        additionalButtons = [
            new TouchBarButton({
                label: 'Books',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/books/`);
                }
            }),
            new TouchBarButton({
                label: 'Settings',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/settings/`);
                }
            }),
            new TouchBarButton({
                label: 'GitHub',
                backgroundColor: '#212529',
                click: async () => {
					await shell.openExternal('https://github.com/cs340-24/litelib')
				}
            }),
        ];
    } else if (user.role === 'admin') {
        additionalButtons = [
            new TouchBarButton({
                label: 'Books',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/books/`);
                }
            }),
            new TouchBarButton({
                label: 'Users',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/users/`);
                }
            }),
            new TouchBarButton({
                label: 'Dashboard',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/dashboard/`);
                }
            }),
            new TouchBarButton({
                label: 'Settings',
                backgroundColor: '#0d6efd',
                click: () => {
                    mainWindow.loadURL(`http://localhost:${port}/settings/`);
                }
            }),
            new TouchBarButton({
                label: 'GitHub',
                backgroundColor: '#212529',
                click: async () => {
					await shell.openExternal('https://github.com/cs340-24/litelib')
				}
            }),
        ];
    }

    let updatedTouchBar = addButtons(additionalButtons);
    mainWindow.setTouchBar(updatedTouchBar);
}

function resetUserTouch() {
    loginButton.label = 'Sign In';
    loginButton.url = `http://localhost:${port}/signin`;

    mainWindow.setTouchBar(touchBar);
}

const touchBar = new TouchBar({
    items: [
        home,
        loginButton
    ]
});

// Function to add more buttons dynamically after login
function addButtons(buttons) {
    const items = [
        home,
        loginButton,
        ...buttons
    ];
    return new TouchBar({ items });
}

module.exports = { touchBar, addButtons, setWindowTouch, setUserTouch, resetUserTouch };
