<?php

$pool_algo = VEXON_DEFAULT_ALGO;
user()->setState('vexon-algo', $pool_algo);

JavascriptFile('/vexon/ui/js/auto_refresh.js');

$coin = getdbosql('db_coins', "symbol='VEX'");
$site_name = htmlspecialchars(VEXON_SITE_NAME, ENT_QUOTES, 'UTF-8');
$pool_url = htmlspecialchars(VEXON_STRATUM_URL, ENT_QUOTES, 'UTF-8');
$stratum_port = getAlgoPort($pool_algo);
if (!$stratum_port) $stratum_port = 4499;

$fee = floatval(vexon_fee($pool_algo));
$min_payout = floatval(VEXON_PAYMENTS_MINI);
$payout_label = htmlspecialchars(VEXON_PAYOUT_SCHEDULE_LABEL, ENT_QUOTES, 'UTF-8');
$payout_note = htmlspecialchars(VEXON_PAYOUT_NOTE, ENT_QUOTES, 'UTF-8');
$official_url = htmlspecialchars(VEXON_OFFICIAL_URL, ENT_QUOTES, 'UTF-8');
$wallet_url = htmlspecialchars(VEXON_WALLET_URL, ENT_QUOTES, 'UTF-8');
$docs_url = htmlspecialchars(VEXON_DOCS_URL, ENT_QUOTES, 'UTF-8');

$t15 = time() - 15*60;
$t24 = time() - 24*60*60;
$active_miners = vexon_live_active_miners($pool_algo, 15*60);
$accepted_15m = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=1 AND algo=:algo AND time>$t15", array(':algo'=>$pool_algo)));
$blocks_24h = intval(dboscalar("SELECT COUNT(*) FROM blocks WHERE coin_id IN (SELECT id FROM coins WHERE symbol='VEX') AND time>$t24 AND category!='orphan'"));
$pool_hashrate = vexon_live_pool_rate($pool_algo, 15*60);

$difficulty = $coin && $coin->difficulty ? floatval($coin->difficulty) : 0;
$reward = $coin && $coin->reward ? floatval($coin->reward) : floatval(VEXON_BLOCK_REWARD_ESTIMATE);
$block_time = $coin && $coin->block_time ? intval($coin->block_time) : intval(VEXON_BLOCK_TIME_ESTIMATE);
if ($block_time <= 0) $block_time = 300;

$network_ths = $difficulty > 0 ? ($difficulty * pow(2, 32) / $block_time / 1000000000000) : 0;
$default_ths = max(0, floatval(VEXON_DEFAULT_ESTIMATE_THS));
$daily_blocks = 86400 / $block_time;
$daily_emission = $daily_blocks * $reward * (1 - $fee/100);
$network_share = $network_ths > 0 ? min(1, $default_ths / $network_ths) : 0;
$estimate_daily = $network_ths > 0 ? $network_share * $daily_emission : 0;
$estimate_daily_text = $estimate_daily > 0 ? number_format($estimate_daily, 4).' VEX' : 'Waiting for network data';
$network_text = $network_ths > 0 ? ($network_ths < 0.001 ? '< 0.001 TH/s' : number_format($network_ths, 3).' TH/s') : 'Waiting';
$daily_emission_text = number_format($daily_emission, 4).' VEX/day max';
$pool_hashrate_text = $pool_hashrate > 0 ? Itoa2($pool_hashrate).'h/s' : 'No shares yet';

echo <<<END
<div id="resume_update_button" class="vexon-refresh-paused" onclick="auto_page_resume();" style="display: none;">
	Auto refresh is paused. Click to resume.
</div>

<section class="vexon-hero">
	<div class="vexon-hero-copy">
		<div class="vexon-logo-lockup">
			<img src="/vexon/ui/images/vexon-logo.png" alt="Vexon">
			<span>Official Mining Pool</span>
		</div>
		<p class="vexon-kicker">Blake256r14 ASIC mining</p>
		<h1>Mine VEX with a clean, transparent pool.</h1>
		<p class="vexon-hero-text">Connect Blake256r14 ASIC hardware such as WhatsMiner D1 and Antminer DR5, monitor accepted work in real time, and receive VEX payouts to your wallet after block maturity.</p>
		<div class="vexon-hero-actions">
			<a class="vexon-primary-button" href="#start-mining">Start mining</a>
			<a class="vexon-secondary-button" href="{$wallet_url}" target="_blank" rel="noopener">Open wallet</a>
			<a class="vexon-secondary-button" href="{$official_url}" target="_blank" rel="noopener">Website</a>
		</div>
	</div>
	<div class="vexon-hero-panel">
		<div class="vexon-panel-label">Pool Snapshot</div>
		<div class="vexon-snapshot-grid">
			<div><span>Active miners</span><strong>{$active_miners}</strong></div>
			<div><span>Pool hashrate</span><strong>{$pool_hashrate_text}</strong></div>
			<div><span>Accepted shares</span><strong>{$accepted_15m}</strong><em>15 minutes</em></div>
			<div><span>Blocks found</span><strong>{$blocks_24h}</strong><em>24 hours</em></div>
		</div>
	</div>
</section>

<section class="vexon-status-strip">
	<div>
		<span>Coin</span>
		<strong>VEX</strong>
	</div>
	<div>
		<span>Algorithm</span>
		<strong>Blake256r14</strong>
	</div>
	<div>
		<span>Minimum payout</span>
		<strong>{$min_payout} VEX</strong>
	</div>
	<div>
		<span>Payout schedule</span>
		<strong>{$payout_label}</strong>
	</div>
</section>

<section id="start-mining" class="vexon-workbench">
	<div class="vexon-connect-panel">
		<div class="vexon-panel-label">Miner endpoint</div>
		<h2>Connect your ASIC</h2>
		<div class="vexon-endpoint-row">
			<code id="vexon-endpoint">stratum+tcp://{$pool_url}:{$stratum_port}</code>
			<button type="button" onclick="vexonCopyEndpoint(this)">Copy</button>
		</div>
		<div class="vexon-connection-grid">
			<span>Username</span><strong>VEX_ADDRESS.worker</strong>
			<span>Password</span><strong>123</strong>
			<span>Port</span><strong>{$stratum_port}</strong>
			<span>Pool fee</span><strong>{$fee}%</strong>
		</div>
	</div>

	<div class="vexon-estimator" data-network-ths="{$network_ths}" data-daily-emission="{$daily_emission}">
		<div class="vexon-panel-label">Mining estimate</div>
		<h2>Expected VEX per day</h2>
		<div class="vexon-preset-row">
			<button type="button" onclick="vexonSetHashrate(40)">WhatsMiner D1 · 40 TH/s</button>
			<button type="button" onclick="vexonSetHashrate(35)">Antminer DR5 · 35 TH/s</button>
		</div>
		<div class="vexon-input-row">
			<label for="vexon-hashrate-input">ASIC hashrate</label>
			<div><input id="vexon-hashrate-input" type="number" min="0" step="0.1" value="{$default_ths}"><span>TH/s</span></div>
		</div>
		<div class="vexon-estimate-result" id="vexon-estimate-result">{$estimate_daily_text}</div>
		<div class="vexon-estimate-meta">
			<span>Network {$network_text}</span>
			<span>Reward {$reward} VEX</span>
			<span>Emission {$daily_emission_text}</span>
		</div>
	</div>

	<div class="vexon-payout-card">
		<div class="vexon-panel-label">Payout rule</div>
		<h2>{$payout_label}</h2>
		<p>{$payout_note}</p>
		<p>Payments require a reachable Vexon wallet service and are sent only when your balance is above {$min_payout} VEX.</p>
	</div>
</section>

<section class="vexon-link-grid">
	<a href="{$official_url}" target="_blank" rel="noopener"><span>Official site</span><strong>Protocol, mission, downloads</strong></a>
	<a href="{$wallet_url}" target="_blank" rel="noopener"><span>Web wallet</span><strong>Create, receive, and send VEX</strong></a>
	<a href="{$docs_url}" target="_blank" rel="noopener"><span>Mining guide</span><strong>ASIC setup and payout rules</strong></a>
	<a href="/explorer"><span>Explorer</span><strong>Blocks, transactions, network status</strong></a>
</section>

<section class="vexon-home-dashboard">
	<div class="vexon-home-top">
		<div id="pool_current_results" class="vexon-live-box">
			<div class="vexon-loading">Loading live mining telemetry...</div>
		</div>
		<aside class="vexon-side-panel vexon-miner-checklist">
			<h2>Miner checklist</h2>
			<ul>
				<li>Use a Vexon wallet address as the miner username.</li>
				<li>Add worker names with <code>.worker</code> for per-device tracking.</li>
				<li>Keep your ASIC on Blake256r14 mode.</li>
				<li>Use fixed difficulty only if your firmware requires it.</li>
			</ul>
			<div class="vexon-side-actions">
				<a class="vexon-link-button" href="/site/mining">Open mining dashboard</a>
				<a class="vexon-link-button" href="/site/miners">View active miners</a>
			</div>
		</aside>
	</div>
	<div class="vexon-home-wide">
		<div id="pool_history_results" class="vexon-live-box">
			<div class="vexon-loading">Loading Vexon block history...</div>
		</div>
	</div>
</section>

<script>
function page_refresh()
{
	pool_current_refresh();
	pool_history_refresh();
}

function select_algo(algo)
{
	return false;
}

function pool_current_ready(data)
{
	$('#pool_current_results').html(data);
}

function pool_current_refresh()
{
	$.get("/site/current_results", '', pool_current_ready);
}

function pool_history_ready(data)
{
	$('#pool_history_results').html(data);
}

function pool_history_refresh()
{
	$.get("/site/history_results", '', pool_history_ready);
}

function vexonFormatAmount(value)
{
	if (!isFinite(value) || value <= 0) return 'Waiting for network data';
	if (value >= 1) return value.toFixed(4) + ' VEX';
	return value.toFixed(6) + ' VEX';
}

function vexonRefreshEstimate()
{
	var box = document.querySelector('.vexon-estimator');
	var input = document.getElementById('vexon-hashrate-input');
	var target = document.getElementById('vexon-estimate-result');
	if (!box || !input || !target) return;
	var network = parseFloat(box.getAttribute('data-network-ths'));
	var dailyEmission = parseFloat(box.getAttribute('data-daily-emission'));
	var ths = parseFloat(input.value);
	if (!network || !dailyEmission || !ths) {
		target.textContent = 'Waiting for network data';
		return;
	}
	var share = Math.min(ths / network, 1);
	var estimate = share * dailyEmission;
	target.textContent = vexonFormatAmount(estimate);
}

function vexonSetHashrate(value)
{
	var input = document.getElementById('vexon-hashrate-input');
	if (!input) return;
	input.value = value;
	vexonRefreshEstimate();
}

function vexonSetCopyState(button, text)
{
	if (!button) return;
	var original = button.getAttribute('data-original-label') || button.textContent;
	button.setAttribute('data-original-label', original);
	button.textContent = text;
	window.clearTimeout(button.vexonCopyTimer);
	button.vexonCopyTimer = window.setTimeout(function() {
		button.textContent = original;
	}, 1800);
}

function vexonFallbackCopy(text)
{
	var area = document.createElement('textarea');
	area.value = text;
	area.setAttribute('readonly', 'readonly');
	area.style.position = 'fixed';
	area.style.left = '-9999px';
	document.body.appendChild(area);
	area.select();
	var ok = false;
	try {
		ok = document.execCommand('copy');
	} catch (e) {
		ok = false;
	}
	document.body.removeChild(area);
	return ok;
}

function vexonCopyText(text, button)
{
	if (navigator.clipboard && navigator.clipboard.writeText) {
		navigator.clipboard.writeText(text).then(function() {
			vexonSetCopyState(button, 'Copied');
		}).catch(function() {
			vexonSetCopyState(button, vexonFallbackCopy(text) ? 'Copied' : 'Copy failed');
		});
		return;
	}
	vexonSetCopyState(button, vexonFallbackCopy(text) ? 'Copied' : 'Copy failed');
}

function vexonCopyEndpoint(button)
{
	var endpoint = document.getElementById('vexon-endpoint');
	if (!endpoint) return;
	vexonCopyText(endpoint.textContent.trim(), button);
}

$(function() {
	$('#vexon-hashrate-input').on('input', vexonRefreshEstimate);
	vexonRefreshEstimate();
});
</script>
END;
