package cnetmap;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Dominik Janura
 */
public class CNetMap {

  /**
   * Definícia konštánt
   */
  public static final String TYPE_SWITCH = "switch";
  public static final String TYPE_ROUTER = "router";
  public static final String PATTERN_HOSTNAME = "hostname";
  public static final String PATTERN_IP = "^\\s*ip\\s*address";
  public static final String PATTERN_INTERFACE = "^interface\\s*";
  public static final String PATTERN_VLAN = "interface\\s*Vlan";
  public static final String PATTERN_MAC = "([0-9A-Fa-f]){4}\\.([0-9A-Fa-f]){4}\\.([0-9A-Fa-f]){4}";
  public static final String PATTERN_CONNECTED = "(connected)";
  public static final String ERR_INTERFACE_UNDEFINED = "undefined interface";

  /**
   * Vráti hostiteľský názov cisco zariadenia konfigurovaného vstupným súborom
   * typu running configuration.
   *
   * @param inputFile vstupný textový súbor
   * @return nájdený hostiteľský názov alebo null ak nebol žiaden nájdený
   * @throws java.io.IOException
   * @see BufferedReader, Pattern, Matcher
   */
  public static String resolveHostname(File inputFile) throws IOException {
    String hostName = null;
    Pattern pattern = Pattern.compile(PATTERN_HOSTNAME);
    Matcher match;
    try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
      String line;
      while ((line = reader.readLine()) != null) {
        match = pattern.matcher(line);
        if (match.find()) {
          hostName = line.split("\\s")[1];
          break;
        }
      }
    } catch (IOException e) {
      Logger.getLogger(CNetMapUI.class.getName()).log(Level.SEVERE, null, e);
    }

    return hostName;
  }

  /**
   * Vráti hostiteľský názov cisco zariadenia konfigurovaného vstupným súborom
   * typu running configuration. Typ môže byť "switch" alebo "router" v
   * závislosti od konfiguračného súboru.
   *
   * @param inputFile vstupný textový súbor
   * @return nájdený typ zariadenia
   * @throws java.io.IOException
   * @see BufferedReader, Pattern, Matcher
   */
  @SuppressWarnings("null")
  public static String resolveType(File inputFile) throws IOException {
    Pattern patternIp = Pattern.compile(PATTERN_IP);
    Pattern patternVlan = Pattern.compile(PATTERN_VLAN);
    Matcher matchIp, matchVlan = null;
    int physIfCount = 0;

    try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
      String currLine, prevLine = null;
      while ((currLine = reader.readLine()) != null) {
        matchIp = patternIp.matcher(currLine);
        if (prevLine != null) {
          matchVlan = patternVlan.matcher(prevLine);
        }
        if (matchIp.find() && !matchVlan.find()) {
          physIfCount++;
        }
        prevLine = currLine;
      }
    } catch (IOException e) {
      Logger.getLogger(CNetMapUI.class.getName()).log(Level.SEVERE, null, e);
    }

    if (physIfCount == 0) {
      return TYPE_SWITCH;
    } else {
      return TYPE_ROUTER;
    }
  }

}