package no.smalltypes.telephone.norway;

import static java.lang.String.format;
import static no.smalltypes.telephone.norway.NorwegianPhonenumberType.Machine2MachineCellular;
import static no.smalltypes.telephone.norway.NorwegianPhonenumberType.assertStringRepresentsCorrectPhoneNumberOrThrow;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NorwegianM2MCellularNumber extends NorwegianPhoneNumber {

	private static final Pattern pattern = Pattern.compile("(\\d{2})(\\d{2})(\\d{2})(\\d{2})");
	public NorwegianM2MCellularNumber(String normalizedNumber, String localPrettyPrintedNumber) {
		super(normalizedNumber, localPrettyPrintedNumber);
	}

	public static NorwegianM2MCellularNumber of(String terseRepresentation) {
		String normalizedNumber = normalizeNorwegianNumberOrThrow(terseRepresentation);
		assertStringRepresentsCorrectPhoneNumberOrThrow(normalizedNumber, Machine2MachineCellular);
		
		Matcher matcher = pattern.matcher(normalizedNumber); matcher.matches();
		
		String localPrettyPrintedNumber = format("%s %s %s %s", matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4));
		
		return new NorwegianM2MCellularNumber(normalizedNumber, localPrettyPrintedNumber);
	}
}
