/**
 * @overview Life of German Reallife - Login Main main.js
 * @author VegaZ, hydrant
 * @copyright (c) 2008 - 2018 Life of German
 */

export default function(globalData: GlobalData): void {
    var loginBrowser: BrowserMp;
    var loginCam: CameraMp = mp.cameras.new('login', new mp.Vector3(-1883.736, -781.4911, 78.27616), new mp.Vector3(3.185999, 0, -79.59519), 40);

    var localPlayer: PlayerMp = mp.players.local;

    mp.events.add('SERVER:Login_ShowBrowser', () => {
        mp.gui.cursor.show(true, true);
        mp.gui.chat.show(false);
        mp.game.ui.displayHud(false);
        mp.game.ui.displayRadar(false);

        loginBrowser = mp.browsers.new("package://assets/html/login/index.html");

        loginCam.setActive(true);
        mp.game.cam.renderScriptCams(true, false, 0, true, false);
    });

    mp.events.add('SERVER:Login_Error', (error: string) => {
        if (loginBrowser) {
            loginBrowser.execute(`showError("${error}");`);
        }
    });

    mp.events.add('CEF:Login_RequestPlayerName', () => {
        if (loginBrowser) {
            loginBrowser.execute(`setPlayerName("${localPlayer.name}");`);
        }
    });

    mp.events.add('CEF:Login_LoginRequest', (username: string, password: string) => {
        mp.events.callRemote('CLIENT:Login_LoginRequest', username, password);
    });

    mp.events.add('CEF:Login_RegisterRequest', (username: string, password: string, passwordRepeat: string) => {
        mp.events.callRemote('CLIENT:Login_RegisterRequest', username, password, passwordRepeat);
    });

    mp.events.add('SERVER:Login_Success', () => {
        if (loginBrowser) {
            loginBrowser.destroy();
        }

        mp.gui.cursor.show(false, false);
        mp.gui.chat.show(true);
        mp.game.ui.displayHud(true);
        mp.game.ui.displayRadar(true);

        globalData.LoggedIn = true;

        loginCam.setActive(false);
        mp.game.cam.renderScriptCams(false, false, 0, true, false);
    });
}