<?php

if( !isset($globalsSet)) {
// Enforcing strict complience with file inclusion
exit('User should not call this file directly.');
}
// This file should not be called directly. Call index.php!
if( !isset($globalMobile)) {
exit('need layout set.');
}

define('ROOT_CONTRACT','0x44a8e5648334a6685c8425676d7c074e19c9a37f');

class CCardData {

	private $md = array();
	private $c_url = array();

	// Constructor sets the basic structure of the metadata
	function __construct($_c_url) {

		$this->c_url = $_c_url;

		$this->md=self::hashMD(false);

		$this->md['interfaces']=array("IERC165" => true,
									"IERC721" => true,
									"IERC721Metadata" => true,
									"ISelNetV1" => true,
									"IPennyOracleV1" => true,
									"IMetadataV1" => true,
									"ICountV1" => true,
									"IPayV1" => true,
									"IMintV1" => true,
									"IERC721ExV1" => true);
 
 	}

	public function hashMD($_bHashes) {
		$hashMD = array();
	
		$hashMD['blockchainId']=361;
		// These two allow core code to build paths to stuff in this location.
		$hashMD['siteRoot']=$GLOBALS['theRootURL'];
		$hashMD['sitePath']='nfts';
		$hashMD['siteName']='drmaudiov1'; // project name
		$hashMD["siteURL"]=$hashMD['siteRoot'].'/'.$hashMD['sitePath'].'/'.$hashMD['siteName'].'/'; // home

		// project description stuff
		$hashMD["description"]="This downloadable open source code demonstrates how a musician can tokengate the download of content from their own server using a Digital Access Token and blockchain validation.";
		$hashMD['whitepaper']=$hashMD["siteURL"].'whitepaper.pdf';
		$hashMD["creator"]="AmorStyle";
		$hashMD["name"]="DRM Audio Smart Contract";
		$hashMD["wtype"]="simpex";

		// smart contract info
		$hashMD['addrContract']=ROOT_CONTRACT;
		$hashMD['abiContract']=$hashMD["siteURL"].'project/'.$hashMD['siteName'].'_abi.json';
		$hashMD['bytecodeContract']=$hashMD["siteURL"].'project/'.$hashMD['siteName'].'_bytecode.txt';

		$hashMD["image"]=$hashMD["siteURL"].'images/'.$hashMD['siteName'].'.jpg';

		if( $_bHashes ) {
			$hashMD['hashes']=array();
			$hashMD['hashes']['abiContract']= hash_file('sha1',$hashMD['abiContract']);
			$hashMD['hashes']['bytecodeContract']= hash_file('sha1',$hashMD['bytecodeContract']);
			$hashMD['hashes']['image']= hash_file('sha1',$hashMD["image"]);
			$hashMD['hashes']['whitepaper']= hash_file('sha1',$hashMD["whitepaper"]);
		}

		return $hashMD;
	}


	// returns an array that holds all the local datapoints about this project.
	public function localData() {
		return $this->md;
	}

	// Makes 2 RPC calls to get owner and titled
	public function project() {

		// project image
		$this->md["image"]=$this->md["siteURL"].'images/'.$this->md['siteName'].'.jpg';

		// We know this contract supports sendLock functionality
		//$this->md['sendLock'] = false;

		//NOTE:
		// This project is soulBound NFTs.
		// uses payMintPrice to get amount of tfuel for mint.
		// uses paySendLockPrice for sendLock functionality (DON'T USE)
		// uses payBurn and price is zero.
		// price is set at 500 pennies.

		// going to call the contract for ownership info.
		$caller = new CNFTCaller($this->md['addrContract']);
		$arrOwnership = $caller->getOwnerAndTitled(); // Two RPC calls
		$this->md['contractOwner'] = $arrOwnership['contractOwner'];
		$this->md['contractTitled'] = $arrOwnership['contractTitled'];

		$this->md['cost']=[];
		$this->md['cost']['selfMint']=(int)500;

		$this->md['audio']=self::audioCollection();
/*
		// one time call
		$results = $caller->getSupportedInterfaces();
		var_dump($results);
*/

		return $this->md;
	}

	// Make 5 RPC calls to get tokenOwner, Series & Spot, tokenURI, token and tnt contract info
	public function token($_tokenId) {
		$this->md['error']=false;

		$this->md['tokenId']=$_tokenId;

		// Now call the contract to get token specific stuff
		$caller = new CNFTCaller($this->md['addrContract']);
		$this->md['tokenOwner']=$caller->getTokenOwner($this->md['tokenId']);

		$this->md['tokenURI']=$this->md["siteURL"].'metadata/?t='.$_tokenId;

		$this->md['audio']=self::audioCollection();

		return $this->md;
	}


	private function readTrack($_track) {
		$arrData = array();

		$url=$this->md["siteURL"].'private/audio_demo_'.$_track.'.json';
		//echo $url.'<br>';
		$theData = file_get_contents($url);
		$arrData = json_decode($theData,true);

		return $arrData;
	}


	public function audioCollection() {
		$arrCollection = array();
		$arrCollection['title'] ="Under the Full Moon";
		$arrCollection['artist'] ="Collection of Open Source Singles";
		$arrCollection['tracks'] = array();
		$index = 0;
		$track = 1;
		$arrCollection['tracks'][$index++] = self::readTrack($track++);
		$arrCollection['tracks'][$index++] = self::readTrack($track++);
		$arrCollection['tracks'][$index++] = self::readTrack($track++);
		$arrCollection['tracks'][$index++] = self::readTrack($track++);

		return $arrCollection;
	}

}



class CHelp_Common {

	function __construct() {
	}

	// 
	// Did we get the correct parameters on the url? This code supports:
	//
	// p=1 -> which gets the basic project info.
	// t=N -> which gets a specific token on the contract.
	// series=N&spot=M -> resolves to t (see above)
	// 
	// Optional:
	// upgrade=N -> can be used with series and spot
	// asset=N -> token id for deposit situations

	public function needHelp($_c_url) {
		$bHelp=false;
		if( $_c_url->exists('p') ) {
			// if we have p, we can't have t
			if( $_c_url->exists('t') ) {
				$bHelp=true;
			} 
			// because we have p, we can have a couple flavors
			if( 1 != $_c_url->value('p') && 'hash' != $_c_url->value('p')&& 'hashval' != $_c_url->value('p')) {
				$bHelp=true;
			}
		} 
		// same check for t
		if( !$bHelp ) {
			// if we have t, we can't have p
			if( $_c_url->exists('t') ) {
				if( $_c_url->exists('p') ) {
					$bHelp=true;
				} 
			}		
		}

		if( !$_c_url->exists('p') && !$_c_url->exists('t')) {
			// set to project
			$_c_url->set(array('p'=>1));
		}

		return $bHelp;
	}

	// This code behaves differently based upon the URL parameters.
	// - if there are none, the metadata returned will provide information about
	//   the last few newly created items. The ui will show the last item 
	// - If there is a ?t=Number on the URL, the metadata code will display information
	//   about that specific basket. The image displayed will be the first image used.
	// - If the caller includes an 'i=Number' on the URL, the image and token details 
	//   about that specific item will be displayed as the main image. 
	//
	public function help($_c_url) {
		$arrLocal = array();
		$arrLocal['URL']=$_c_url->propURL();
		$arrLocal['usage']=
'Each marketplace item is a token on the contract. Because tokens can be burnt, valid tokens have owners. If you want to add to the marketplace, you can create a new token by providing a nftcontract and asset on the URL.'
;

		$arrLocal['params']['p']='To get basic project info set p=1.';
		$arrLocal['params']['t']='This is the token id. Cards are tokens. t=NUMBER.';

		//$arrInfo['global']=$_c_url->docs();
		return $_c_url->docs($arrLocal);

	}
}