package usda.weru.weps.location;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class StationUtil {

    private static final String TYPE_CLIGEN = "cligen";
    private static final String TYPE_WINDGEN = "windgen";
    private static final String TYPE_FILE = "file";
    private static final String TYPE_INTERPOLATED = "interpolated";

    /**
     *
     * @param station
     * @return
     */
    public static String serializeStation(Station station) {
        if (station == null) {
            return null;
        }
        if (station instanceof CligenStation) {
            return TYPE_CLIGEN + "|" + CligenStation.serialize((CligenStation) station);
        } else if (station instanceof WindgenStation) {
            return TYPE_WINDGEN + "|" + WindgenStation.serialize((WindgenStation) station);
        } else if (station instanceof FileStation) {
            return TYPE_FILE + "|" + FileStation.serialize((FileStation) station);
        } else if (station instanceof InterpolatedStation) {
            return TYPE_INTERPOLATED + "|" + InterpolatedStation.serialize((InterpolatedStation) station);
        } else {
            throw new IllegalArgumentException("Unsupported Station type: " + station.getClass().getCanonicalName());
        }
    }

    /**
     * Have to handle changign versions of the
     *
     * @param serValue
     * @return
     */
    public static Station deserializeStation(final String serValue) {
        if (serValue == null || serValue.trim().length() == 0) {
            return null;
        }
        String[] parts = serValue.trim().split("\\|");

        //first part is the Station type
        String typeName = parts[0].trim();

        String stationText = serValue.substring(typeName.length() + 1);
        switch (typeName) {
            case TYPE_CLIGEN:
                return CligenStation.deserialize(stationText);
            case TYPE_WINDGEN:
                return WindgenStation.deserialize(stationText);
            case TYPE_FILE:
                return FileStation.deserialize(stationText);
            case TYPE_INTERPOLATED:
                return InterpolatedStation.deserialize(stationText);
            default:
                throw new IllegalArgumentException("Unsupported Station type: " + typeName);
        }
    }
}
