﻿using System.Threading.Tasks;
using Trinity.Core;
using Trinity.Extensions;

namespace Trinity.Api.Queries
{
	public class GetStorageApiQuery : IGetStorageApiQuery
	{
		private readonly IBlockchain _blockchain;

		public GetStorageApiQuery(IBlockchain blockchain)
		{
			_blockchain = blockchain;
		}
		public async Task<string> ExecuteAsync(string scriptHash, string key)
		{
			await Task.FromResult(0); //build out API to support async transactions.

			var scriptHashAsUInt160 = UInt160.Parse(scriptHash);
			var keyAsBytes = key.HexToBytes();
			var item = _blockchain.GetStorageItem(new StorageKey
			{
				ScriptHash = scriptHashAsUInt160,
				Key = keyAsBytes
			}) ?? new StorageItem();
			return item.Value?.ToHexString();
		}
	}
}