package com.paymill.services;

import java.util.Date;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.StringUtils;

import com.paymill.Paymill;
import com.paymill.models.Fee;
import com.paymill.models.Transaction;
import com.paymill.utils.RestfulUtils;
import com.paymill.utils.ValidationUtils;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class TransactionService implements PaymillService {

  private final static String PATH = "/transactions";

  public Transaction show( Transaction transaction ) {
    return RestfulUtils.show( TransactionService.PATH, transaction, Transaction.class );
  }

  public Transaction createWithToken( String token, Integer amount, String currency ) {
    return this.createWithTokenAndFee( token, amount, currency, null, null );
  }

  public Transaction createWithToken( String token, Integer amount, String currency, String description ) {
    return this.createWithTokenAndFee( token, amount, currency, description, null );
  }

  public Transaction createWithTokenAndFee( String token, Integer amount, String currency, Fee fee ) {
    return this.createWithTokenAndFee( token, amount, currency, null, fee );
  }

  public Transaction createWithTokenAndFee( String token, Integer amount, String currency, String description, Fee fee ) {
    ValidationUtils.validatesToken( token );
    ValidationUtils.validatesAmount( amount );
    ValidationUtils.validatesCurrency( currency );
    ValidationUtils.validatesFee( fee );

    MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.add( "token", token );
    params.add( "amount", String.valueOf( amount ) );
    params.add( "currency", currency );

    if( StringUtils.isNotBlank( description ) )
      params.add( "description", description );
    if( fee.getAmount() != null )
      params.add( "fee_amount", String.valueOf( fee.getAmount() ) );
    if( StringUtils.isNotBlank( fee.getPayment() ) )
      params.add( "fee_payment", fee.getPayment() );

    return RestfulUtils.create( TransactionService.PATH, params, Transaction.class );
  }

  public void list( Integer count, Integer offset, Date created_at ) {
    WebResource webResource = Paymill.getHttpClient().resource( Paymill.ENDPOINT + PATH );
    ClientResponse response = webResource.get( ClientResponse.class );

    if( response.getStatus() != 200 ) {
      System.out.println( "Failed : HTTP error code : " + response.getStatus() );
    }

    String output = response.getEntity( String.class );

  }

  public Transaction createWithPayment() {
    return null;
  }

  public Transaction createWithPreauthorization() {
    return null;
  }

  public Transaction update( Transaction transaction ) {
    return RestfulUtils.update( TransactionService.PATH, transaction, Transaction.class );
  }

}

@@ -578,16 +578,424 @@
 ions%22;%0A%0A
+  public void list( Integer count, Integer offset, Date created_at ) %7B%0A    WebResource webResource = Paymill.getHttpClient().resource( Paymill.ENDPOINT + PATH );%0A    ClientResponse response = webResource.get( ClientResponse.class );%0A%0A    if( response.getStatus() != 200 ) %7B%0A      System.out.println( %22Failed : HTTP error code : %22 + response.getStatus() );%0A    %7D%0A%0A    response.getEntity( String.class );%0A  %7D%0A%0A
   public
@@ -2299,16 +2299,31 @@
 %0A    if(
+ fee != null &&
  fee.get
@@ -2409,32 +2409,47 @@
 t() ) );%0A    if(
+ fee != null &&
  StringUtils.isN
@@ -2627,433 +2627,8 @@
  %7D%0A%0A
-  public void list( Integer count, Integer offset, Date created_at ) %7B%0A    WebResource webResource = Paymill.getHttpClient().resource( Paymill.ENDPOINT + PATH );%0A    ClientResponse response = webResource.get( ClientResponse.class );%0A%0A    if( response.getStatus() != 200 ) %7B%0A      System.out.println( %22Failed : HTTP error code : %22 + response.getStatus() );%0A    %7D%0A%0A    String output = response.getEntity( String.class );%0A%0A  %7D%0A%0A
   pu

check if fee is null before get its attributes