/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.weps.location.mode;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.jscience.geography.coordinates.LatLong;
import usda.weru.weps.RunFileBean;
import usda.weru.weps.RunFileBean.StationType;
import usda.weru.weps.location.InterpolatedStation;
import usda.weru.weps.location.StationModeController;
import usda.weru.weps.location.chooser.StationChooser;
import usda.weru.weps.location.mode.InterpolatedHandler.InterpolatedBeanState;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class InterpolatedHandler extends AbstractStationModeHandler<InterpolatedBeanState> implements StationModeController {

    /**
     *
     * @param chooser
     */
    @Override
    protected void setView(StationChooser chooser) {
        chooser.setView(StationChooser.View.Label);
    }

    /**
     *
     * @param bean
     * @param type
     * @return
     */
    @Override
    protected InterpolatedBeanState createBeanState(RunFileBean bean, StationType type) {
        return new InterpolatedBeanState(bean, type);
    }

    /**
     *
     * @param state
     */
    protected void handleLatLongChange(InterpolatedBeanState state) {
        //get the latlong from the state.
        LatLong latlong = state.getBean().getLatLong();

        //create the new station
        InterpolatedStation station = latlong != null ? new InterpolatedStation(latlong) : null;

        //set the station, BeanState takes care of knowing if it needs to set Cligen/Windgen
        state.setSelectedStation(station);

    }

    @Override
    @Deprecated
    public int getId() {
        return 3;
    }

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

    @Override
    public String getName() {
        return "interpolated";
    }

    /**
     *
     */
    protected class InterpolatedBeanState
            extends AbstractStationModeHandler<InterpolatedBeanState>.BeanState
            implements PropertyChangeListener {

        /**
         *
         * @param bean
         * @param type
         */
        public InterpolatedBeanState(RunFileBean bean, RunFileBean.StationType type) {
            super(bean, type);
        }

        /**
         *
         */
        @Override
        public void install() {
            super.install();
            handleLatLongChange(this);
            getBean().addPropertyChangeListener(RunFileBean.PROP_LATLONG, this);
        }

        /**
         *
         */
        @Override
        public void uninstall() {
            super.uninstall();
            getBean().removePropertyChangeListener(RunFileBean.PROP_LATLONG, 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 evt.getSource() == getBean() : "Event from unexpected source.";
            if (RunFileBean.PROP_LATLONG.equals(evt.getPropertyName())) {
                handleLatLongChange(this);
            }
        }

    }

}
