<?php

$algo = VEXON_DEFAULT_ALGO;
$coin = getdbosql('db_coins', "symbol='VEX'");
if (!$coin) {
	echo '<section class="vexon-telemetry-card"><div class="vexon-empty-state">VEX coin configuration is not ready yet.</div></section>';
	return;
}

$now = time();
$t15 = $now - 15*60;
$t60 = $now - 60*60;
$t24 = $now - 24*60*60;

$active_miners = vexon_live_active_miners($algo, 15*60);
$accepted_1h = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=1 AND algo=:algo AND time>$t60", array(':algo'=>$algo)));
$accepted_24h = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=1 AND algo=:algo AND time>$t24", array(':algo'=>$algo)));
$rejected_24h = intval(dboscalar("SELECT COUNT(*) FROM shares WHERE valid=0 AND algo=:algo AND time>$t24", array(':algo'=>$algo)));
$pool_hashrate = vexon_live_pool_rate($algo, 15*60);
$pool_hashrate_text = $pool_hashrate > 0 ? Itoa2($pool_hashrate).'h/s' : 'Waiting for shares';

$difficulty = $coin->difficulty ? floatval($coin->difficulty) : 0;
$difficulty_text = $difficulty > 0 ? Itoa2($difficulty, 3) : 'Waiting';
$reward = $coin->reward ? floatval($coin->reward) : floatval(VEXON_BLOCK_REWARD_ESTIMATE);
$height = intval($coin->block_height);
$fee = floatval(vexon_fee($algo));
$min_payout = floatval(VEXON_PAYMENTS_MINI);
$block_time = $coin->block_time ? intval($coin->block_time) : intval(VEXON_BLOCK_TIME_ESTIMATE);
if ($block_time <= 0) $block_time = 300;

$network_hash = $difficulty > 0 ? $difficulty * 0x100000000 / $block_time : 0;
$network_hash_text = $network_hash > 0 ? Itoa2($network_hash).'h/s' : 'Waiting';
$pool_ttf = $pool_hashrate > 0 && $difficulty > 0 ? $difficulty * 0x100000000 / $pool_hashrate : 0;
$pool_ttf_text = $pool_ttf > 0 ? sectoa2($pool_ttf) : 'Waiting for pool hashrate';
$last_share_time = intval(dboscalar("SELECT MAX(time) FROM shares WHERE algo=:algo", array(':algo'=>$algo)));
$last_share = $last_share_time ? datetoa2($last_share_time).' ago' : 'No shares yet';
$reject_rate = ($accepted_24h + $rejected_24h) > 0 ? round($rejected_24h * 100 / ($accepted_24h + $rejected_24h), 2).'%' : '0%';
$payout_label = htmlspecialchars(VEXON_PAYOUT_SCHEDULE_LABEL, ENT_QUOTES, 'UTF-8');

echo <<<END
<section class="vexon-telemetry-card">
	<div class="vexon-card-heading">
		<div>
			<span class="vexon-card-kicker">Mining accounting</span>
			<h2>VEX Mining Status</h2>
		</div>
		<img src="/vexon/ui/images/vexon-logo.png" alt="Vexon">
	</div>

	<div class="vexon-metric-grid vexon-metric-grid-five">
		<div><span>Block height</span><strong>{$height}</strong></div>
		<div><span>Pool hashrate</span><strong>{$pool_hashrate_text}</strong></div>
		<div><span>Active miners</span><strong>{$active_miners}</strong></div>
		<div><span>Expected block</span><strong>{$pool_ttf_text}</strong></div>
		<div><span>Network</span><strong>{$network_hash_text}</strong></div>
	</div>

	<table class="vexon-stat-table">
		<thead>
			<tr>
				<th>Item</th>
				<th>Value</th>
				<th>Window</th>
				<th>Meaning</th>
			</tr>
		</thead>
		<tbody>
			<tr><td>Accepted work</td><td>{$accepted_1h}</td><td>1 hour</td><td>Shares credited to miners</td></tr>
			<tr><td>Accepted work</td><td>{$accepted_24h}</td><td>24 hours</td><td>Used for pool-side performance tracking</td></tr>
			<tr><td>Rejected work</td><td>{$rejected_24h}</td><td>24 hours</td><td>Reject rate {$reject_rate}</td></tr>
			<tr><td>Network difficulty</td><td>{$difficulty_text}</td><td>Current</td><td>Block target used by Vexon Core</td></tr>
			<tr><td>Reward target</td><td>{$reward} VEX</td><td>Per block</td><td>Subject to consensus and maturity</td></tr>
			<tr><td>Pool fee</td><td>{$fee}%</td><td>Per payout</td><td>Deducted from credited rewards</td></tr>
			<tr><td>Minimum payout</td><td>{$min_payout} VEX</td><td>{$payout_label}</td><td>Paid after block maturity</td></tr>
			<tr><td>Last share</td><td>{$last_share}</td><td>Live</td><td>Latest accepted or rejected miner traffic</td></tr>
		</tbody>
	</table>
</section>
END;
