package de.ivu.elect.business.person.entity;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import static java.lang.Character.isDigit;
import static java.lang.Character.isLetter;
import static java.lang.Math.min;

/**
 * Diese Klasse stammt aus OSV und wurde nach elect kopiert (ELECTVAPP-1018). und angepasst (ELECTVAPP-1228)
 */
public abstract class ShortCodeCalculator {

  private static final int MAX_LEN = 14; // spare one character for a number

  private static final Map<String, String> SHORT_CODE_CACHE = new ConcurrentHashMap<>(); // ELECTVAPP-1228

  private ShortCodeCalculator() {
  }

  public static String createShortCode(String lastName, String initials) {
    return SHORT_CODE_CACHE.computeIfAbsent("lastName: " + lastName + "---First:" + initials, x -> createShortCodeInternal(lastName, initials));
  }

  private static String createShortCodeInternal(String lastName, String initials) {
    final String lastNameWI = removeInterpunction(lastName);
    String initialsWI = removeInterpunction(initials);
    final int maxInitialsLen = MAX_LEN / 2;
    final int maxLastNameLen = MAX_LEN - initialsWI.length();

    initialsWI = initialsWI.substring(0, min(initialsWI.length(), maxInitialsLen));

    return lastNameWI.substring(0, min(lastNameWI.length(), maxLastNameLen)) + initialsWI;
  }

    private static String removeInterpunction(String str) {
        if (str == null) {
            return ""; //$NON-NLS-1$
        }
        final int strLen = str.length();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strLen; i++) {
            final char ch = str.charAt(i);
            if (isLetter(ch)) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    // NOTE: Wird in elect nicht verwendet.

//    public static String extendShortCode(final String shortCode) {
//        final int strLen = shortCode.length();
//        // check for digits at the end of the short code...
//        boolean nonDigitFound = false;
//        int lastNonDigitIndex = strLen;
//        for (int i = strLen - 1; i >= 0 && !nonDigitFound; i--) {
//            final char ch = shortCode.charAt(i);
//            if (!isDigit(ch)) {
//                nonDigitFound = true;
//                lastNonDigitIndex = i + 1;
//            }
//        }
//        // build new short code...
//        final String pureShortCode = shortCode.substring(0, lastNonDigitIndex);
//        String newShortCodeExt = ""; //$NON-NLS-1$
//        if (strLen - lastNonDigitIndex > 0) {
//            // increment existing extension
//            final String oldShortCodeExt = shortCode.substring(lastNonDigitIndex);
//            final Integer newShortCodeExtNumber = Integer.valueOf(oldShortCodeExt) + 1;
//            newShortCodeExt = newShortCodeExtNumber.toString();
//        } else {
//            // append 1
//            newShortCodeExt = Integer.valueOf(1).toString();
//        }
//        return pureShortCode.concat(newShortCodeExt);
//    }
}