<?php

if (!isset($coin))
	$coin = getdbosql('db_coins', "symbol='VEX'");

$this->pageTitle = 'Vexon Explorer';

$name = $coin ? $coin->name : 'Vexon';
$symbol = $coin ? $coin->symbol : 'VEX';
$logo = '/vexon/ui/images/vexon-logo.png';
$remote = $coin ? new WalletRPC($coin) : null;

$chain = vexon_explorer_rpc($remote, 'getblockchaininfo');
$height = is_array($chain) ? intval(arraySafeVal($chain, 'blocks', 0)) : ($coin ? intval($coin->block_height) : 0);
$headers = is_array($chain) ? intval(arraySafeVal($chain, 'headers', $height)) : $height;
$difficulty = is_array($chain) ? arraySafeVal($chain, 'difficulty', ($coin ? $coin->difficulty : 0)) : ($coin ? $coin->difficulty : 0);
$bestHash = is_array($chain) ? arraySafeVal($chain, 'bestblockhash', '') : '';
$chainName = is_array($chain) ? arraySafeVal($chain, 'chain', 'mainnet') : 'mainnet';
$blockTime = $coin && objSafeVal($coin, 'block_time', 0) ? intval($coin->block_time) : (defined('VEXON_BLOCK_TIME_ESTIMATE') ? intval(VEXON_BLOCK_TIME_ESTIMATE) : 150);
if ($blockTime <= 0) $blockTime = 150;
$peers = vexon_explorer_rpc($remote, 'getconnectioncount');
$peers = is_numeric($peers) ? intval($peers) : ($coin ? intval($coin->connections) : 0);

$poolBlocks = $coin ? intval(vexon_explorer_query_scalar(
	"SELECT COUNT(*) FROM blocks WHERE coin_id=:coin",
	array(':coin'=>$coin->id),
	0
)) : 0;

$poolLatest = $coin ? vexon_explorer_query_list(
	"SELECT height, category, amount, difficulty, blockhash, confirmations, time FROM blocks WHERE coin_id=:coin ORDER BY id DESC LIMIT 8",
	array(':coin'=>$coin->id)
) : array();

$latestBlocks = array();
if ($coin && $height >= 0) {
	$start = $height;
	$end = max(0, $height - 9);
	for($i=$start; $i >= $end; $i--) {
		$hash = ($i == $height && $bestHash) ? $bestHash : vexon_explorer_rpc($remote, 'getblockhash', array($i));
		if (!$hash)
			continue;
		$block = vexon_explorer_rpc($remote, 'getblock', array($hash));
		if (!is_array($block))
			continue;
		$latestBlocks[] = $block;
	}
}

$bestHashText = $bestHash ? vexon_explorer_short($bestHash, 16, 12) : 'Waiting';
$difficultyText = vexon_explorer_metric($difficulty, 3);
$networkHashrateText = vexon_explorer_hashrate($difficulty, $blockTime);
$syncText = $headers > 0 ? $height.' / '.$headers : (string)$height;

echo '<section class="vexon-page-title">';
echo '<div><p class="vexon-kicker">Explorer</p><h1>Vexon Block Explorer</h1></div>';
echo '<p>Inspect VEX blocks, transactions, addresses, and pool reward records from one clean explorer.</p>';
echo '</section>';

echo '<section class="vexon-explorer-hero">';
echo '<div class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Network</span><h2>'.vexon_explorer_h($name).' '.$symbol.'</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
echo '<div class="vexon-metric-grid vexon-metric-grid-five">';
echo '<div><span>Block height</span><strong>'.intval($height).'</strong></div>';
echo '<div><span>Sync height</span><strong>'.vexon_explorer_h($syncText).'</strong></div>';
echo '<div><span>Difficulty</span><strong>'.vexon_explorer_h($difficultyText).'</strong></div>';
echo '<div><span>Network hashrate</span><strong>'.vexon_explorer_h($networkHashrateText).'</strong></div>';
echo '<div><span>Peers</span><strong>'.intval($peers).'</strong></div>';
echo '</div>';

echo '<form class="vexon-explorer-search" action="/explorer/search" method="POST">';
echo '<input type="text" name="q" class="main-text-input" placeholder="Search block height, block hash, transaction hash, or VEX address">';
echo '<input type="submit" value="Search Explorer" class="main-submit-button">';
echo '</form>';

if (!$coin) {
	echo '<div class="vexon-empty-state">VEX coin configuration is not ready yet.</div>';
} elseif (!$bestHash && $height <= 0) {
	echo '<div class="vexon-empty-state">The Vexon node is online, but the explorer is waiting for indexed block data.</div>';
}
echo '</div>';

echo '<aside class="vexon-side-panel vexon-explorer-side">';
echo '<span class="vexon-card-kicker">Live Snapshot</span><h2>Explorer Status</h2>';
echo '<ul>';
echo '<li><span>Chain</span><strong>'.vexon_explorer_h($chainName).'</strong></li>';
echo '<li><span>Algorithm</span><strong>Blake256r14</strong></li>';
echo '<li><span>Block target</span><strong>'.intval($blockTime).' seconds</strong></li>';
echo '<li><span>Best block</span><strong class="vexon-hash">'.vexon_explorer_h($bestHashText).'</strong></li>';
echo '<li><span>Pool blocks</span><strong>'.intval($poolBlocks).'</strong></li>';
echo '<li><span>Minimum payout</span><strong>'.(defined('VEXON_PAYMENTS_MINI') ? vexon_explorer_amount(VEXON_PAYMENTS_MINI, 8) : '0.001').' VEX</strong></li>';
echo '</ul>';
echo '<a class="vexon-link-button" href="/site/mining">Mining dashboard</a>';
echo '</aside>';
echo '</section>';

echo '<section class="vexon-explorer-grid">';
echo '<div class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Chain</span><h2>Latest Blocks</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
if (!count($latestBlocks)) {
	echo '<div class="vexon-empty-state">Blocks will appear here as soon as the Vexon node returns indexed block data.</div>';
} else {
	echo '<div class="vexon-table-scroll"><table class="vexon-stat-table vexon-explorer-table"><thead><tr>';
	echo '<th>Height</th><th>Hash</th><th>Time</th><th>Tx</th><th>Difficulty</th><th>Confirmations</th>';
	echo '</tr></thead><tbody>';
	foreach($latestBlocks as $block) {
		$blockHeight = intval(arraySafeVal($block, 'height', 0));
		$blockHash = arraySafeVal($block, 'hash', '');
		$txs = arraySafeVal($block, 'tx', array());
		$txCount = is_array($txs) ? count($txs) : 0;
		echo '<tr>';
		echo '<td><a href="/explorer?height='.$blockHeight.'">'.$blockHeight.'</a></td>';
		echo '<td><a class="vexon-hash" href="/explorer?hash='.vexon_explorer_h($blockHash).'">'.vexon_explorer_h(vexon_explorer_short($blockHash, 18, 12)).'</a></td>';
		echo '<td>'.vexon_explorer_h(vexon_explorer_time(arraySafeVal($block, 'time', 0))).'</td>';
		echo '<td>'.$txCount.'</td>';
		echo '<td>'.vexon_explorer_h(vexon_explorer_metric(arraySafeVal($block, 'difficulty', 0), 4)).'</td>';
		echo '<td>'.intval(arraySafeVal($block, 'confirmations', 0)).'</td>';
		echo '</tr>';
	}
	echo '</tbody></table></div>';
}
echo '</div>';

echo '<div class="vexon-telemetry-card">';
echo '<div class="vexon-card-heading"><div><span class="vexon-card-kicker">Pool</span><h2>Pool Block Records</h2></div><img src="'.$logo.'" alt="Vexon"></div>';
if (!count($poolLatest)) {
	echo '<div class="vexon-empty-state">Waiting for the first block found by Vexon Pool. Immature, mature, and rejected records will be shown here.</div>';
} else {
	echo '<div class="vexon-table-scroll"><table class="vexon-stat-table vexon-explorer-table"><thead><tr>';
	echo '<th>Height</th><th>Status</th><th>Amount</th><th>Difficulty</th><th>Time</th><th>Hash</th>';
	echo '</tr></thead><tbody>';
	foreach($poolLatest as $item) {
		$blockHash = arraySafeVal($item, 'blockhash', '');
		echo '<tr>';
		echo '<td>'.intval(arraySafeVal($item, 'height', 0)).'</td>';
		echo '<td><span class="vexon-status-pill">'.vexon_explorer_h(arraySafeVal($item, 'category', 'new')).'</span></td>';
		echo '<td>'.vexon_explorer_amount(arraySafeVal($item, 'amount', 0), 8).' VEX</td>';
		echo '<td>'.vexon_explorer_h(vexon_explorer_metric(arraySafeVal($item, 'difficulty', 0), 3)).'</td>';
		echo '<td>'.vexon_explorer_h(vexon_explorer_time(arraySafeVal($item, 'time', 0))).'</td>';
		echo '<td>'.($blockHash ? '<a class="vexon-hash" href="/explorer?hash='.vexon_explorer_h($blockHash).'">'.vexon_explorer_h(vexon_explorer_short($blockHash, 16, 10)).'</a>' : 'Pending').'</td>';
		echo '</tr>';
	}
	echo '</tbody></table></div>';
}
echo '</div>';
echo '</section>';
