<?php

require_once("util.php");

class ExplorerController extends CommonController
{
	public $defaultAction='index';

	public function run($actionID)
	{
		if (!empty($actionID)) {
			if (is_numeric($actionID) && isset($_REQUEST['id'])) {
				$this->forward('id');
			}
			elseif (strlen($actionID) <= 6 && !isset($_REQUEST['id'])) {
				$coin = getdbosql('db_coins', "symbol=:symbol", array(
					':symbol'=>strtoupper($actionID)
				));
				if ($coin && ($coin->visible || $this->admin)) {
					$_REQUEST['id'] = $coin->id;
					if (!empty($_POST))
						$this->forward('search');
					$this->forward('id');
				}
			}
		}
		return parent::run($actionID);
	}

	public function createUrl($route,$params=array(),$ampersand='&')
	{
		if ($route == '/explorer' && isset($params['id'])) {
			$coin = getdbo('db_coins', intval($params['id']));
			if ($coin && $coin->visible && !is_numeric($coin->symbol)) {
				unset($params['id']);
				$query = http_build_query($params,'',$ampersand);
				$route = '/explorer/'.$coin->symbol.($query ? '?'.$query : '');
				$params = array();
			}
		}
		return parent::createUrl($route, $params, $ampersand);
	}

	private function vexonCoin()
	{
		$id = getiparam('id');
		$coin = $id > 0 ? getdbo('db_coins', $id) : null;
		if (!$coin)
			$coin = getdbosql('db_coins', "symbol='VEX'");
		return $coin;
	}

	public function actionIndex()
	{
		if(isset($_COOKIE['mainbtc'])) return;
		if(!LimitRequest('explorer')) return;

		$coin = $this->vexonCoin();
		if (!$coin) {
			$this->render('index', array('coin'=>null));
			return;
		}

		if($coin->no_explorer) {
			$link = $coin->link_explorer;
			die("Block explorer disabled, please use <a href=\"$link\">$link</a>");
		}

		$q = trim(strip_tags(getparam('q')));
		$heightParamPresent = isset($_REQUEST['height']);
		$height = $heightParamPresent ? getiparam('height') : -1;
		$hash = trim(strip_tags(getparam('hash')));
		$txid = trim(strip_tags(getparam('txid')));
		$address = trim(strip_tags(getparam('address')));

		if ($q !== '') {
			if (ctype_digit($q)) {
				$height = intval($q);
				$heightParamPresent = true;
			}
			elseif (vexon_explorer_is_address($q))
				$address = $q;
			elseif (vexon_explorer_is_hash($q))
				$hash = $q;
		}

		$remote = new WalletRPC($coin);

		if ($heightParamPresent && $height >= 0) {
			$foundHash = vexon_explorer_rpc($remote, 'getblockhash', array($height));
			if ($foundHash)
				$hash = $foundHash;
			else {
				$this->render('block', array('coin'=>$coin, 'hash'=>'', 'height'=>$height, 'error'=>'Block height not found on this node yet.'));
				return;
			}
		}

		if ($address !== '') {
			$this->render('address', array('coin'=>$coin, 'address'=>$address));
			return;
		}

		if ($txid !== '') {
			$this->render('tx', array('coin'=>$coin, 'txhash'=>$txid));
			return;
		}

		if ($hash !== '') {
			$block = vexon_explorer_rpc($remote, 'getblock', array($hash));
			if (is_array($block)) {
				$this->render('block', array('coin'=>$coin, 'hash'=>$hash, 'block'=>$block));
				return;
			}

			if (vexon_explorer_is_hash($hash)) {
				$this->render('tx', array('coin'=>$coin, 'txhash'=>$hash));
				return;
			}
		}

		$this->render('index', array('coin'=>$coin));
	}

	public function actionId()
	{
		return $this->actionIndex();
	}

	public function actionSearch()
	{
		$coin = $this->vexonCoin();
		$url = $coin && $coin->visible ? '/explorer/'.$coin->symbol.'?' : '/explorer?';

		$height = getiparam('height');
		$q = trim(strip_tags(getparam('q')));
		$hash = trim(strip_tags(getparam('hash')));
		$txid = trim(strip_tags(getparam('txid')));
		$address = trim(strip_tags(getparam('address')));

		$params = array();
		if (isset($_REQUEST['height']) && $height >= 0) $params['height'] = $height;
		if ($q !== '') $params['q'] = $q;
		if ($hash !== '') $params['hash'] = $hash;
		if ($txid !== '') $params['txid'] = $txid;
		if ($address !== '') $params['address'] = $address;

		if (empty($params))
			return $this->redirect('/explorer');

		return $this->redirect($url.http_build_query($params));
	}

	public function actionGraph()
	{
		echo "[]";
	}

	public function actionPeers()
	{
		$coin = $this->vexonCoin();
		$remote = $coin ? new WalletRPC($coin) : null;
		$peers = vexon_explorer_rpc($remote, 'getpeerinfo');
		header('Content-Type: application/json');
		echo json_encode(is_array($peers) ? $peers : array());
	}
}
