import { ApiSocket } from "/js/connect/api.js";

// load styles
document.head.innerHTML += '\\n		<link href="/bootstrap/bootstrap.min.css" rel="stylesheet">';
document.head.innerHTML += '\\n		<link href="/css/connect/main.css" rel="stylesheet">';
document.head.innerHTML += '\\n		<link href="/font-awesome/css/all.css" rel="stylesheet">';
document.head.innerHTML += '\\n		<link rel="icon" href="/favicon.png">'

// load main page
document.body.innerHTML = `<nav id="navbar-main" class="navbar navbar-connect-dark navbar-dark navbar-expand-lg noselect" style="background-color: #e3f2fd;">
	<a style="padding-left:10px" class="navbar-brand linked-btn" id="server-name" onclick="fl.go('/');">
		Connection...
	</a>
	<div class="collapse navbar-collapse noselect" id="navbarNav"><ul class="navbar-nav"><ul class="navbar-nav">
		<div class="vr"></div>
		<li class="nav-item">
			<a class="nav-link translate linked-btn noselect" aria-current="page" id="nav-home-btn"><i class="fas fa-home"></i> Home</a>
		</li>
		<li class="nav-item">
			<a class="nav-link translate linked-btn noselect" aria-current="page" id="nav-docs-btn"><i class="fas fa-dice-d20"></i> Games</a>
		</li>
	</ul></div>

	<div class="vr" style="margin-right:5px"></div>
	<div class="d-flex" style="margin-right:10px" id="navbar-user-menu">
		<button id="toggle-theme" class="btn btn-outline-light" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"><i class="fas fa-sun"></i></button>
	</div>
</nav>
<div id="server.area">
	<div style='margin-top: 30vh'>
		<center>
			<h1>Connection to server...</h1>
			<h3 class="rotate"><i class="fas fa-sync-alt fa-spin"></i></h3>
		</center>
	</div>
</div>`;

//document <i class="fas fa-sync-alt"></i>

const curUrl = new URL(location.href);
const [address, port] = curUrl.searchParams.get("server").split(/:/);
const isTLSmode = curUrl.searchParams.get("encrypted") === "true";

const socket = new ApiSocket({
	isTLSmode, address, port
});
socket.run()
	.then(async (data) => {
		console.log("socket sends:", data);
		document.title = data.name;
		document.getElementById("server-name").innerText = data.name;
		await socket.html.renderAuth(data.authMode);
	})
	.catch(err => {
		console.error(err);
		document.title = "Connection error";
		document.getElementById("server-name").innerHTML = "Error";
		document.getElementById("server.area").innerHTML = `<div style='margin-top: 30vh'>
			<center>
				<h1>Connection errored</h1>
				<p>Connection errored</p>
			</center>
		</div>`;
	});
