/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drausin.bitflow.blockchain;

import io.dropwizard.jersey.params.DateTimeParam;
import java.util.List;
import org.bitcoinj.core.Sha256Hash;
import org.drausin.bitflow.bitcoin.api.BitcoinNodeService;
import org.drausin.bitflow.bitcoin.api.requests.BitcoinNodeRequest;
import org.drausin.bitflow.bitcoin.api.requests.BitcoinNodeRequestFactory;
import org.drausin.bitflow.blockchain.api.BlockchainService;
import org.drausin.bitflow.blockchain.api.objects.BlockHeader;
import org.drausin.bitflow.blockchain.api.objects.BlockchainInfo;
import org.drausin.bitflow.service.utils.BitflowResource;

public class BlockchainResource extends BitflowResource implements BlockchainService {

    private BitcoinNodeService bitcoinNodeService;

    public BlockchainResource(BitcoinNodeService bitcoinNodeService) {
        this.bitcoinNodeService = bitcoinNodeService;
    }

    @Override
    public final BlockchainInfo getBlockchainInfo(String authHeader) {
        // TODO(dwulsin): what to do with authHeader?
        BitcoinNodeRequest request = BitcoinNodeRequestFactory.createBlockchainInfoRequest();
        return bitcoinNodeService.getBlockchainInfo(request).getResult().get();
    }

    @Override
    public final BlockHeader getBlockHeader(String authHeader, Sha256Hash hash) {
        // TODO(dwulsin): what to do with authHeader?
        BitcoinNodeRequest request = BitcoinNodeRequestFactory.createBlockHeaderRequest(hash);
        return bitcoinNodeService.getBlockHeader(request).getResult().get();
    }

    @Override
    public final List<BlockHeader> getBlockHeaderTimeSubchain(String authHeader, DateTimeParam from, DateTimeParam to) {
        // TODO(dwulsin)
        return null;
    }

    @Override
    public final List<BlockHeader> getBlockHeaderHeightSubchain(String authHeader, long from, long to) {
        // TODO(dwulsin)
        return null;
    }
}

@@ -909,16 +909,159 @@
 actory;%0A
+import org.drausin.bitflow.bitcoin.api.responses.BlockHeaderResponse;%0Aimport org.drausin.bitflow.bitcoin.api.responses.BlockchainInfoResponse;%0A
 import o
@@ -1105,32 +1105,32 @@
 ckchainService;%0A
-
 import org.draus
@@ -1818,60 +1818,189 @@
     
-return bitcoinNodeService.getBlockchainInfo(request)
+BlockchainInfoResponse blockchainInfoResponse = bitcoinNodeService.getBlockchainInfo(request);%0A        blockchainInfoResponse.validateResult();%0A        return blockchainInfoResponse
 .get
@@ -2275,22 +2275,57 @@
         
-return
+BlockHeaderResponse blockHeaderResponse =
  bitcoin
@@ -2359,16 +2359,98 @@
 request)
+;%0A        blockHeaderResponse.validateResult();%0A        return blockHeaderResponse
 .getResu

validate result before returning it