package com.brilliance.isc.swift.objmtconvert.util;

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

public class FormatUtils {
    public static String fmtAmount(BigDecimal fmtamt, String cur) {
        if (fmtamt == null){
            fmtamt = BigDecimal.ZERO;
        }
        String amt = fmtamt.toString();
        String intVal;
        String fractionVal;
        if (amt.contains(".")) {
            String[] split = amt.split("\\.");
            intVal = split[0];
            fractionVal = split[1];
        } else {
            intVal = amt;
            fractionVal = "00";
        }
        int length = intVal.length();
        int num = length / 3;
        int md = length % 3;
        StringBuilder sb = new StringBuilder();
        sb.append(intVal, 0, md);
        for (int i = 0; i < num; i++) {
            if (md != 0 || i != 0) {
                sb.append(",");
            }
            sb.append(intVal, 3 * i + md, 3 * (i + 1) + md);
        }
        if (!"JPY".equals(cur)) {
            sb.append(".").append(fractionVal);
        }
        return sb.toString();
    }

    public static String fmt6NDate(String sixNDate){
        if(sixNDate == null || sixNDate.isEmpty() || !sixNDate.matches("\\d{6}")){
            return sixNDate;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        try {
            Date date = sdf.parse(sixNDate);
            return new SimpleDateFormat("dd.MM.yyyy").format(date);
        }catch (Exception e){
            return sixNDate;
        }
    }
}
