/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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";

    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 
    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);

        if(TYPE_CLIGEN.equals(typeName)){
            return CligenStation.deserialize(stationText);
        }
        else if(TYPE_WINDGEN.equals(typeName)){
            return WindgenStation.deserialize(stationText);
        }
        else if(TYPE_FILE.equals(typeName)){
            return FileStation.deserialize(stationText);
        }
        else if(TYPE_INTERPOLATED.equals(typeName)){
            return InterpolatedStation.deserialize(stationText);
        }
        else{
            throw new IllegalArgumentException("Unsupported Station type: " + typeName);
        }
    }
}
