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

package usda.weru.weps.location.elevation;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.measure.Measurable;
import javax.measure.quantity.Length;
import org.apache.log4j.Logger;
import usda.weru.util.LoadingContext;
import usda.weru.weps.RunFileBean;
import usda.weru.weps.location.ElevationModeController;
import usda.weru.weps.location.Station;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class CligenElevationModeController implements ElevationModeController, PropertyChangeListener{
    private static final Logger LOGGER = Logger.getLogger(CligenElevationModeController.class);

	/**
	 *
	 * @param rfb
	 */
	@Override
    public void installModel(RunFileBean rfb) {
        rfb.addPropertyChangeListener(RunFileBean.PROP_CLIGEN_STATION, this);
    }

	/**
	 *
	 * @param rfb
	 */
	@Override
    public void uninstallModel(RunFileBean rfb) {
        rfb.removePropertyChangeListener(RunFileBean.PROP_CLIGEN_STATION, this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    public void propertyChange(PropertyChangeEvent evt) {
/**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable specific assertions (feed the virtual machine 
                 * the -ea argument).
                 */
//        assert RunFileBean.PROP_CLIGEN_STATION.equals(evt.getPropertyName()) : "Unexpected property event.";

        if(evt.getSource() instanceof RunFileBean){
            //Source is a RunFileBean
            RunFileBean rfb = (RunFileBean) evt.getSource();

            if(LoadingContext.isLoading()){
                //don't care what happens when loading, we'll accept the value
                return;
            }

            //we're not loading so now we care if the cligen station has changed.
            Station station = rfb.getCligenStation();
            if(station == null)
            {
                LOGGER.error("Cligen Station is null.  Cannot set the elevation.");
                return;
            }
            Measurable<Length> elevation = station.getElevation();
            if(elevation != null){
                //set the elevation from the cligen station
                rfb.setElevation(station.getElevation());
            }
            else{
                LOGGER.info("No elevation for cligen station: " + station.getDisplayName());
            }
        }
    }

	/**
	 *
	 * @return
	 */
	@Override
    public String getName() {
        return "cligen";
    }

	/**
	 *
	 * @return
	 */
	@Override
    public String getDisplayName() {
        return "Cligen";
    }



}
