package com.gsos.dimiter_robin;

import java.math.BigInteger;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.gsos.dimiter_robin.ibanInterface.Authentication;
import com.gsos.dimiter_robin.ibanInterface.Fault;
import com.gsos.dimiter_robin.ibanInterface.Fault_Exception;
import com.gsos.dimiter_robin.ibanInterface.IbanResponse;
import com.gsos.dimiter_robin.ibanInterface.IbanServiceInterface;
import com.gsos.dimiter_robin.ibanInterface.Ibanrequest;
import com.gsos.dimiter_robin.ibanInterface.ValidationResponse;
import com.gsos.dimiter_robin.ibanInterface.Validationrequest;

@WebService(endpointInterface = "com.gsos.dimiter_robin.ibanInterface.IbanServiceInterface")
public class IbanServiceImplementation implements IbanServiceInterface {
	@WebMethod(operationName = "toIban")
	@Override
	public IbanResponse toIban(@WebParam(name="authentication")Authentication auth,@WebParam(name = "ibanrequest") Ibanrequest request) 
			throws Fault_Exception{
		validateAuthentication(auth);
		
		String bankcode = request.getBankcode().value();
		BigInteger rekeningnummer = request.getRekeningnummer();
		System.out.println("New request with bankcode "+bankcode+" and number "+rekeningnummer);
		
		String partialCode = valueFromLetters(bankcode) + addZeroes(rekeningnummer) + valueFromLetters("NL") + "00";
		
		IbanResponse response = new IbanResponse();
		response.setIban("NL48INGB008829939");
		return response;
	}
	
	private String addZeroes (BigInteger nummer) {
		String stringNummer = nummer.toString();
		while (stringNummer.length() < 10) {
			stringNummer = "0" + stringNummer; 
		}
		return new String();
	}
	
	private String valueFromLetters (String letters) {
		String value = "";
		for (Character letter : letters.toCharArray()) {
			value += Integer.toString(letter - 'A' + 10);
		}
		return value;
		
	}

	@WebMethod(operationName = "validateIban")
	@Override
	public ValidationResponse validateIban(@WebParam(name="authentication")Authentication auth,@WebParam(name = "validationrequest") Validationrequest request)
			throws Fault_Exception {
		validateAuthentication(auth);
		
		ValidationResponse response = new ValidationResponse();
		response.setResult(true);
		return response;
	}
	
	private void validateAuthentication(Authentication auth) throws Fault_Exception{
		int postcodeNumbers = Integer.parseInt(auth.getPostcode().substring(0, 4));
		if(postcodeNumbers < 8200 || postcodeNumbers > 8299) throw new Fault_Exception("Postcode not between 8200 and 8299", null);
	}
}

@@ -1028,27 +1028,65 @@
 on(auth);%0A%09%09
-%0A%09%09
+final String countryCode = %22NL%22;%0A%09%09final 
 String bankc
@@ -1080,25 +1080,25 @@
  String bank
-c
+C
 ode = reques
@@ -1124,16 +1124,22 @@
 ue();%0A%09%09
+final 
 BigInteg
@@ -1141,28 +1141,27 @@
 Integer 
-rekeningnumm
+accountNumb
 er = req
@@ -1233,55 +1233,366 @@
 bank
+ 
 code %22
-+
+ + 
 bank
-code+
+Code + 
 %22 and 
-number %22+rekeningnummer);
+bank account number %22 + accountNumber);%0A%09%09%0A%09%09/*%0A%09%09System.out.println(%22Value of bank code %22+bankCode+%22: %22 + valueFromLetters(bankCode));%0A%09%09System.out.println(%22Bank account number with leading zeroes: %22 + addZeroes(accountNumber));%0A%09%09System.out.println(%22Value of country code %22+countryCode+%22: %22 + valueFromLetters(countryCode));%0A%09%09*/
 %0A%09%09%0A
@@ -1604,15 +1604,12 @@
 ing 
-partial
+iban
 Code
@@ -1632,17 +1632,17 @@
 ers(bank
-c
+C
 ode) + a
@@ -1654,20 +1654,19 @@
 oes(
-rekeningnumm
+accountNumb
 er) 
@@ -1684,20 +1684,27 @@
 Letters(
-%22NL%22
+countryCode
 ) + %2200%22
@@ -1711,93 +1711,441 @@
 ;%0A%09%09
-%0A%09%09IbanResponse response = new IbanResponse();%0A%09%09response.setIban(%22NL48INGB008829939%22
+//System.out.println(%22Numerical iban code: %22 + ibanCode);%0A%09%09%0A%09%09int controleGetal = 98 - (new BigInteger(ibanCode).mod(new BigInteger(%2297%22)).intValue());%0A%09%09//System.out.println(%22Controlegetal: %22 + controleGetal);%0A%0A%09%09%0A%09%09IbanResponse response = new IbanResponse();%0A%09%09response.setIban(countryCode + String.format(%22%2502d%22, controleGetal) + bankCode + addZeroes(accountNumber) );%0A%09%09System.out.println(%22Generated IBAN: %22 + response.getIban()
 );%0A%09
@@ -2353,20 +2353,20 @@
 urn 
-new String()
+stringNummer
 ;%0A%09%7D

Version 1.0 of IBAN Service works!