/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import csip.Config;
import csip.ServiceException;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class IdxStations {

  public static final int US_PR = 1;
  public static final int US_PR_NRCS = 2;

  protected static ConcurrentHashMap<Integer, Station> stationData_US_PR;
  protected static ConcurrentHashMap<Integer, Station> stationData_US_PR_NRCS;

  public static synchronized ConcurrentHashMap<Integer, Station> getUsPrFile() throws IOException {
    if (stationData_US_PR == null) {
      stationData_US_PR = new ConcurrentHashMap();
      IdxParser.parsewindgenIdxFile(stationData_US_PR, new File(Config.getString("csip.dir") + "/data/wind_gen_his_upper_US_PR.idx"));
    }

    return stationData_US_PR;
  }

  public static synchronized ConcurrentHashMap<Integer, Station> getUsPrNrcsFile() throws IOException {
    if (stationData_US_PR_NRCS == null) {
      stationData_US_PR_NRCS = new ConcurrentHashMap();
      IdxParser.parsewindgenIdxFile(stationData_US_PR_NRCS, new File(Config.getString("csip.dir") + "/data/wind_gen_his_upper_US_PR_NRCS.idx"));
    }
    return stationData_US_PR_NRCS;
  }

  public static Station getStation(int stationId, int idxFileType) throws IOException, ServiceException {
    ConcurrentHashMap<Integer, Station> stations;
    switch (idxFileType) {
      case US_PR:
        stations = getUsPrFile();
        break;
      case US_PR_NRCS:
        stations = getUsPrNrcsFile();
        break;
      default:
        stations = getUsPrNrcsFile();
    }

//    if (!stations.contains(stationId)) {
//      throw new ServiceException("The chosen idx file does not contain the station id requested.  Idx File: " + ((idxFileType == US_PR)? "US_PR":"US_PR_NRCS") + " Station Id requested: '" + stationId + "'");
//    }
    
    return stations.get(stationId);
  }
}
