/*
 * Copyright 2013 Artur Mkrtchyan
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Deutsche Version
 */

package org.iban4j;

import static org.iban4j.Bic.BANK_CODE_INDEX;
import static org.iban4j.Bic.BANK_CODE_LENGTH;
import static org.iban4j.Bic.BRANCH_CODE_INDEX;
import static org.iban4j.Bic.BRANCH_CODE_LENGTH;
import static org.iban4j.Bic.COUNTRY_CODE_INDEX;
import static org.iban4j.Bic.COUNTRY_CODE_LENGTH;
import static org.iban4j.Bic.LOCATION_CODE_INDEX;
import static org.iban4j.Bic.LOCATION_CODE_LENGTH;

import de.ivu.elect.jee.faces.FacesMessageUtil;
import org.iban4j.support.Assert;

/**
 * ELECT-3129 Iban Bic Validierung lokalisieren
 */
public class BicUtilDE {

  protected static final int BIC8_LENGTH = 8;
  protected static final int BIC11_LENGTH = 11;

  /**
   * Validates bic.
   *
   * @param bic
   *          to be validated.
   * @throws BicFormatException
   *           if bic is invalid.
   */
  public static void validate(final String bic) throws BicFormatException {

    try {
      Assert.notNull(bic, FacesMessageUtil.getResourceText("bic.validation.detail.empty"));
      validateLength(bic);
      validateCase(bic);
      validateBankCode(bic);
      validateCountryCode(bic);
      validateLocationCode(bic);

      if (hasBranchCode(bic)) {
        validateBranchCode(bic);
      }
    } catch (UnsupportedCountryException e) {
      throw e;
    } catch (RuntimeException e) {
      throw new BicFormatException(e.getMessage());
    }
  }

  private static void validateLength(final String bic) {
    if (bic.length() != BIC8_LENGTH && bic.length() != BIC11_LENGTH) {
      throw new BicFormatException(FacesMessageUtil.getResourceText("bic.validation.detail.invalid.length", BIC8_LENGTH, BIC11_LENGTH));
    }
  }

  private static void validateCase(final String bic) {
    if (!bic.equals(bic.toUpperCase())) {
      throw new BicFormatException(FacesMessageUtil.getResourceText("bic.validation.detail.not.upper.case"));
    }
  }

  private static void validateBankCode(final String bic) {
    String bankCode = getBankCode(bic);
    for (char ch : bankCode.toCharArray()) {
      if (!Character.isLetter(ch)) {
        throw new BicFormatException(FacesMessageUtil.getResourceText("bic.validation.detail.bank.code.invalid.character"));
      }
    }
  }

  // Cf. IbanUtilDE.validateCountryCode!
  private static void validateCountryCode(final String bic) {
    String countryCode = getCountryCode(bic);

    if (countryCode.trim().length() < COUNTRY_CODE_LENGTH || !countryCode.equals(countryCode.toUpperCase())
        || !Character.isLetter(countryCode.charAt(0)) || !Character.isLetter(countryCode.charAt(1))) {
      throw new BicFormatException(FacesMessageUtil.getResourceText("bic.validation.detail.country.code.not.upper.case"));
    }

    Assert.notNull(CountryCode.getByCode(countryCode), FacesMessageUtil.getResourceText("bic.validation.detail.country.code.unknown", countryCode));
  }

  private static void validateLocationCode(final String bic) {
    String locationCode = getLocationCode(bic);
    for (char ch : locationCode.toCharArray()) {
      if (!Character.isLetterOrDigit(ch)) {
        throw new BicFormatException(FacesMessageUtil.getResourceText("bic.validation.detail.location.code.not.upper.case"));
      }
    }
  }

  private static void validateBranchCode(final String bic) {
    String branchCode = getBranchCode(bic);
    for (char ch : branchCode.toCharArray()) {
      if (!Character.isLetterOrDigit(ch)) {
        throw new BicFormatException(FacesMessageUtil.getResourceText("bic.validation.detail.branch.code.invalid.character"));
      }
    }
  }

  protected static String getBankCode(final String bic) {
    return bic.substring(BANK_CODE_INDEX, BANK_CODE_INDEX + BANK_CODE_LENGTH);
  }

  protected static String getCountryCode(final String bic) {
    return bic.substring(COUNTRY_CODE_INDEX, COUNTRY_CODE_INDEX + COUNTRY_CODE_LENGTH);
  }

  protected static String getLocationCode(final String bic) {
    return bic.substring(LOCATION_CODE_INDEX, LOCATION_CODE_INDEX + LOCATION_CODE_LENGTH);
  }

  protected static String getBranchCode(final String bic) {
    return bic.substring(BRANCH_CODE_INDEX, BRANCH_CODE_INDEX + BRANCH_CODE_LENGTH);
  }

  protected static boolean hasBranchCode(final String bic) {
    return bic.length() == BIC11_LENGTH;
  }
}
