package usda.weru.weps.location;

import javax.measure.Measurable;
import javax.measure.quantity.Length;
import org.apache.log4j.Logger;
import org.jscience.geography.coordinates.LatLong;
import usda.weru.gis.GISUtil;

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

    private static final Logger LOGGER = Logger.getLogger(WindgenStation.class);
    private final LatLong c_latlong;
    private final long c_wban;
    private final String c_country;
    private final String c_state;
    private final String c_name;

    /**
     *
     * @param latlong
     * @param wban
     * @param country
     * @param state
     * @param name
     */
    public WindgenStation(LatLong latlong, long wban, String country, String state, String name) {
        if (latlong == null) {
            LOGGER.warn("Latlong is null.  Attempting to find value in Windgen data model for wban=" + wban + ".");
            WindgenStation temp = WindgenDataModel.getInstance().getStation(wban);
            if (temp != null) {
                latlong = temp.getLatLong();
                LOGGER.warn("Latlong value found. " + latlong);
            } else {
                LOGGER.error("No latlong found for wban=" + wban + ".");
            }
        }
        c_latlong = latlong;
        c_wban = wban;
        c_country = country;
        c_state = state;
        c_name = name;
    }

    @Override
    public String getDisplayName() {
        return c_name;
    }

    @Override
    public LatLong getLatLong() {
        return c_latlong;
    }

    /**
     *
     * @return
     */
    @Override
    public Measurable<Length> getElevation() {
        return null;
    }

    /**
     *
     * @return
     */
    public long getWBan() {
        return c_wban;
    }

    /**
     *
     * @return
     */
    public String getName() {
        return c_name;
    }

    /**
     *
     * @return
     */
    protected String getCountry() {
        return c_country;
    }

    /**
     *
     * @return
     */
    protected String getState() {
        return c_state;
    }

    /**
     *
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final WindgenStation other = (WindgenStation) obj;
        if (this.c_wban != other.c_wban) {
            return false;
        }
        return true;
    }

    /**
     *
     * @return
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (int) (this.c_wban ^ (this.c_wban >>> 32));
        return hash;
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return c_state + " " + c_name;
    }

    /**
     *
     * @param station
     * @return
     */
    public static String serialize(WindgenStation station) {
        if (station != null) {
            StringBuilder buffer = new StringBuilder();

            //latlong
            buffer.append(station.getLatLong() != null ? GISUtil.format(station.getLatLong()) : "");
            buffer.append("|");
            //id
            buffer.append(Long.valueOf(station.getWBan()));
            buffer.append("|");
            //country
            buffer.append(station.getCountry() != null ? station.getCountry() : "");
            buffer.append("|");
            //state
            buffer.append(station.getState() != null ? station.getState() : "");
            buffer.append("|");
            //name
            buffer.append(station.getDisplayName() != null ? station.getDisplayName() : "");

            return buffer.toString();
        }
        return null;
    }

    /**
     *
     * @param text
     * @return
     */
    public static WindgenStation deserialize(String text) {
        if (text != null) {
            String[] parts = text.split("\\|");
            int i = 0;
            LatLong latlong = GISUtil.parse(parts[i++]);
            long id = Long.valueOf(parts[i++].trim());
            String country = parts[i++].trim();
            country = country.length() > 0 ? country : null;
            String state = parts[i++].trim();
            state = state.length() > 0 ? state : null;
            String name = parts[i++].trim();
            name = name.length() > 0 ? name : null;
            return new WindgenStation(latlong, id, country, state, name);

        }
        return null;
    }

}
