package com.github.devnied.emvnfccard.iso7816emv;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.github.devnied.emvnfccard.model.enums.CountryCodeEnum;
import com.github.devnied.emvnfccard.model.enums.CurrencyEnum;

import fr.devnied.bitlib.BytesUtils;

public final class TagValueFactory {

	public static byte[] constructValue(final TagAndLength pTagAndLength) {
		byte ret[] = new byte[pTagAndLength.getLength()];
		byte val[] = null;
		if (pTagAndLength.getTag() == EMVTags.TERMINAL_TRANSACTION_QUALIFIERS) {
			TerminalTransactionQualifiers terminalQual = new TerminalTransactionQualifiers();
			terminalQual.setContactlessEMVmodeSupported(true);
			ret = terminalQual.getBytes();
		} else if (pTagAndLength.getTag() == EMVTags.TERMINAL_COUNTRY_CODE) {
			val = BytesUtils.toByteArray(CountryCodeEnum.FR.getNumeric());
		} else if (pTagAndLength.getTag() == EMVTags.TRANSACTION_CURRENCY_CODE) {
			val = BytesUtils.toByteArray(CurrencyEnum.EUR.getISOCodeNumeric());
		} else if (pTagAndLength.getTag() == EMVTags.TRANSACTION_DATE) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
			val = BytesUtils.fromString(sdf.format(new Date()));
		} else if (pTagAndLength.getTag() == EMVTags.TRANSACTION_TYPE) {
			val = BytesUtils.fromString("00");
		} else if (pTagAndLength.getTag() == EMVTags.AMOUNT_AUTHORISED_NUMERIC) {
			val = BytesUtils.fromString("99");
		}
		if (val != null) {
			System.arraycopy(val, Math.max(0, val.length - ret.length), ret, Math.max(ret.length - val.length, 0),
					Math.min(val.length, ret.length));
		}
		return ret;
	}

}

@@ -103,16 +103,62 @@
 .Date;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 import c
@@ -321,45 +321,278 @@
 s;%0A%0A
-public final class TagValueFactory %7B%0A
+/**%0A * Factory to create Tag value%0A * %0A * @author Millau Julien%0A * %0A */%0Apublic final class TagValueFactory %7B%0A%0A%09/**%0A%09 * Method used to construct value from tag and length%0A%09 * %0A%09 * @param pTagAndLength%0A%09 *            tag and length value%0A%09 * @return tag value in byte%0A%09 */
 %0A%09pu
@@ -1074,27 +1074,61 @@
 esUtils.
-toByteArray
+fromString(StringUtils.leftPad(String.valueOf
 (Country
@@ -1144,32 +1144,42 @@
 FR.getNumeric())
+, 4, %220%22))
 ;%0A%09%09%7D else if (p
@@ -1264,19 +1264,53 @@
 ils.
-toByteArray
+fromString(StringUtils.leftPad(String.valueOf
 (Cur
@@ -1343,16 +1343,26 @@
 meric())
+, 4, %220%22))
 ;%0A%09%09%7D el
@@ -1753,10 +1753,10 @@
 ng(%22
-99
+00
 %22);%0A

Fix error in GPO command