/*
 * 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.
 */

/*
 * 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.client.objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Preconditions;
import org.drausin.bitflow.blockchain.api.objects.BlockchainResult;
import org.immutables.value.Value;

/**
 * The RPC request to the bitcoin daemon.
 *
 * @author dwulsin
 * @see <a href="https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs">Bitcoin RPCs</a>
 */
@Value.Immutable
@Value.Style(visibility = Value.Style.ImplementationVisibility.PACKAGE)
@JsonDeserialize(as = ImmutableBitcoindRpcResponse.class)
public abstract class BitcoindRpcResponse {

    @Value.Parameter
    @JsonProperty("result")
    public abstract BlockchainResult getResult();

    @Value.Parameter
    @JsonProperty("error")
    public abstract BitcoindRpcResponseError getError();

    @Value.Parameter
    @JsonProperty("id")
    public abstract String getId();

    public static BitcoindRpcResponse of(BlockchainResult result, BitcoindRpcResponseError error, String id) {
        return ImmutableBitcoindRpcResponse.of(result, error, id);
    }

    public final void validate(Class<? extends BlockchainResult> resultType) {
        Preconditions.checkNotNull(getError(), String.format("reponse error: {}", getError()));
        Preconditions.checkState(getResult().getClass().equals(resultType),
                String.format("unexpected response type: expected was {}, actual is {}", getResult().getClass(),
                        resultType));
    }
}

@@ -1325,29 +1325,24 @@
 on.base.
-Precondi
+Op
 tion
-s
+al
 ;%0Aimport
@@ -1878,16 +1878,25 @@
 bstract 
+Optional%3C
 Blockcha
@@ -1903,16 +1903,17 @@
 inResult
+%3E
  getResu
@@ -1987,16 +1987,25 @@
 bstract 
+Optional%3C
 Bitcoind
@@ -2020,16 +2020,17 @@
 nseError
+%3E
  getErro
@@ -2100,22 +2100,32 @@
 bstract 
+Optional%3C
 String
+%3E
  getId()
@@ -2289,26 +2289,111 @@
 .of(
+Optional.fromNullable(
 result
+)
 , 
-error, 
+Optional.fromNullable(error),%0A                Optional.fromNullable(
 id)
+)
 ;%0A  
@@ -2412,18 +2412,248 @@
 lic 
-final void
+static BitcoindRpcResponse of(Optional%3CBlockchainResult%3E result, Optional%3CBitcoindRpcResponseError%3E error,%0A            Optional%3CString%3E id) %7B%0A        return ImmutableBitcoindRpcResponse.of(result, error, id);%0A    %7D%0A%0A    public final boolean
  val
@@ -2657,16 +2657,22 @@
 validate
+Result
 (Class%3C?
@@ -2724,128 +2724,319 @@
     
-Preconditions.checkNotNull(getError(), String.format(%22reponse error: %7B%7D%22, getError()));%0A        Preconditions.checkState
+if (!getResult().isPresent()) %7B%0A            throw new IllegalStateException(%22result value is absent%22);%0A        %7D%0A        if (getError().isPresent()) %7B%0A            throw new IllegalStateException(%22error value is present: %25s%22.format(getError().get().toString()));%0A        %7D%0A        if(!resultType.isAssignableFrom
 (get
@@ -3036,32 +3036,38 @@
 rom(getResult().
+get().
 getClass().equal
@@ -3064,28 +3064,121 @@
 ss()
-.equals(resultType),
+)) %7B%0A            throw new IllegalStateException(%22result value is not of type %25s%22.format(resultType.toString()));
 %0A   
@@ -3182,24 +3182,26 @@
         
+%7D%0A
         
 String.f
@@ -3196,141 +3196,370 @@
     
-String.format(%22unexpected response type: expected was %7B%7D, actual is %7B%7D%22, getResult().getClass(),%0A                        resultType))
+return true;%0A    %7D%0A%0A    public final boolean validateError() %7B%0A        if (!getError().isPresent()) %7B%0A            throw new IllegalStateException(%22error value is absent%22);%0A        %7D%0A        if (getResult().isPresent()) %7B%0A            throw new IllegalStateException(%22result value is present: %25s%22.format(getError().get().toString()));%0A        %7D%0A        return true
 ;%0A  

make data Optional and add result & error validators