<?php
	$local = isset($_REQUEST['cdn'])? false: true; // true = use only locally installed modules, false = use cdn
	$machine = empty($_REQUEST['machine'])? 'elettra': strtolower($_REQUEST['machine']);
	$mode = isset($_REQUEST['fast'])? 'fast': (isset($_REQUEST['premium'])? 'premium': '');
	$mode = !empty($_REQUEST['fast'])? 'all': $mode;
    file_put_contents('./logs'.date('Y').'.txt', date('Y-m-d H:i:s').' '.$_SERVER['REMOTE_ADDR'].'_'.$_SERVER['HTTP_X_FORWARDED_FOR'].' - '.$_SERVER['QUERY_STRING'].PHP_EOL , FILE_APPEND | LOCK_EX);
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>PAnTHer</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

		<!--
			Firefox seems to not like having scripts that come in before the importmap
			https://discourse.threejs.org/t/solved-importmaps-broken-on-firefox-which-now-supports-importmaps/48413/5 
		-->
		<script type="importmap">
			{
				"imports": {
                    "three": "<?php echo ($local? './threejs': 'https://cdn.jsdelivr.net/npm/three@0.161.0/build');?>/three.module.js",
                    "three/addons/": "<?php echo ($local? './threejs/': 'https://cdn.jsdelivr.net/npm/three@0.161.0/examples/jsm/');?>",
					"components/": "./components/src/",
					"bundle": "./components/bundle_<?php echo $machine.$mode;?>.js"
				}
			}
		</script>
		<script>;
			const client = '<?php echo gethostbyaddr($_SERVER['REMOTE_ADDR']);?>'.split('.')[0];
			const fermi = ['do','re','mi','fa','sol','la','si'].indexOf(client)>-1? '?machine=fermi': '';
			if (navigator.userAgent.indexOf('Firefox/63')>-1) document.location = './ff63/'+fermi;
		</script>

		<style>
			body {
				background-color: #333333;
				color: #000;
				margin: 0px;
			}
			a {
				color: #f00;
			}
			#c {
				width: 100%;
				height: 100%;
				display: block;
			}
			.blm {
				display: none;
			}
			.blmVal {
				background-color: white;
				padding: 2px;
				color: black;
				border-radius: 5px;
			}
		</style>
		<link rel="stylesheet" href="<?php echo ($local? '../lib/jquery/jquery-ui.min.css': 'https://code.jquery.com/ui/1.13.3/themes/base/jquery-ui.css');?>">
		<script src="<?php echo ($local? '../lib/jquery/jquery.min.js': 'https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js');?>" type="text/javascript"></script>
		<script src="<?php echo ($local? '../lib/jquery/jquery-ui.min.js': 'https://cdn.jsdelivr.net/npm/jquery-ui@1.13.3/dist/jquery-ui.min.js');?>" type="text/javascript"></script>
	</head>

	<body style="overflow: hidden;">
		<canvas id="c" style="margin-top: -15px;"></canvas>
		<div id="demo" style="position: absolute; display: none; top: 0px; right: -8px; z-index: 10000;" <?php
			 if ($_REQUEST['machine']=='elettra2') echo 'title="click me for Lattice source" onClick="document.location=\'https://drive.elettra.eu/group/268/\';"'; ?>>
				 <svg width="150px" height="150px" viewBox="0 0 100 100" class="icon" version="1.1" xmlns="http://www.w3.org/2000/svg">
			<path d="M0 0 h70 l30 30 v70 z" stroke="black" stroke-width="2" fill="red"></path>
			<text transform="rotate(45)" x="43" y="-30" font-weight="bold" fill="white">DEMO</text>
			<text transform="rotate(45)" x="14" y="-6" font-size="70%" fill="white"> all numbers are random</text>
		</svg></div>
		<script type="module" src="./panther.js"></script>
		<div id='tooltip' style='position:absolute; display:none; z-index: 100000000;'>
			<div style='width: 490px; background-color: white; padding:5px; border-width: 3px; border-color: blue; border-top-left-radius: 5px; border-top-right-radius: 5px;'><button id='compdb'>find component <span id='compname'/> on components DB</button></div>
			<iframe id='tooltipFrame' style="border:0; position:absolute; background:transparent; " width="240px" height="220px" src=''></iframe>
		</div>
		<div id='application' style='display:none; position:absolute; left:0; top:0; bottom:0; width:22%; max-width:600px; z-index: 10000;'>
			<iframe id='applicationFrame' style="border:0; position:absolute; width: 100%; height: 200px;" src=''></iframe>
		</div>
		<div id='notice' style='display:<?php echo $_REQUEST['machine']=='elettra2'? 'block': 'none';?>; position:absolute; right:5px; bottom:5px; max-width:600px; z-index: 10000;'>
			Data source <a href='https://drive.elettra.eu/group/268/'>https://drive.elettra.eu/group/268/</a>
		</div>
	</body>
</html>