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

package TestRest.converter;
import TestRest.QuakemlView;
import TestRest.Assocaro;
import TestRest.Assocamo;
import TestRest.converter.NetMagConverter;
import TestRest.converter.OriginConverter;
import TestRest.converter.PickConverter;
import java.math.BigDecimal;
import java.net.URI;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.ws.rs.core.UriBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import javax.persistence.EntityManager;

/**
 *
 * @author aparnab
 */
@XmlType(propOrder={"prefor", "prefmag","category","origin","mag","pick","amplitude"})
public class EvConverter {
    private QuakemlView entity;
    private  Collection<Assocaro> picks;
    private  Collection<Assocamo> amplitudes;
    private Assocaro arr;
    private URI uri;
    private int expandLevel;
    @XmlAttribute
    private String publicID;

    @XmlElement(name="magnitude")
    private List<NetMagConverter> mag;

    @XmlElement(name="origin")
    private List<OriginConverter> origin;

    @XmlElement(name="pick")
    private List<PickConverter> pick;

    @XmlElement(name="amplitude")
    private List<AmplitudeConverter> amplitude;

    public EvConverter(){
        entity = new QuakemlView();
    }

    //Event w/ picks
    
    //public EvConverter(QuakemlView entity, Collection<AssocaroPK> picks, URI uri, int expandLevel, boolean isUriExtendable) {
    public EvConverter(QuakemlView entity, Collection<Assocaro> picks, Collection<Assocamo> amplitudes,
            URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.picks = picks;
        this.amplitudes = amplitudes;
        this.pick = new ArrayList<PickConverter>();
        this.amplitude = new ArrayList<AmplitudeConverter>();
        for (Assocaro arr : picks) {
            PickConverter tempPick = new PickConverter(arr.getArrival().getArid(), entity.getPrefor(), uri);
            //System.out.println("arid = " + arr.getArrival().getArid());
            //System.out.println("uri = " + uri);
            this.pick.add(tempPick);
        }
        for (Assocamo amp : amplitudes) {
            AmplitudeConverter tempAmp = new AmplitudeConverter(amp.getAmp().getAmpid(), uri);
            //System.out.println("arid = " + arr.getArrival().getArid());
            //System.out.println("uri = " + uri);
            this.amplitude.add(tempAmp);
        }
        this.setPublicID();
        //this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getEvid() + "/").build() : uri;
        this.expandLevel = expandLevel;
        this.mag = new ArrayList<NetMagConverter>();
        NetMagConverter temp = new NetMagConverter(((expandLevel > 0) ? entity.getMagnitude() : null).floatValue());
        //NetMagConverter temp = new NetMagConverter(((expandLevel > 0) ? entity.getPrefmag() : null).floatValue());
        temp.setOriginID(entity.getPrefor().toString());
        //temp.setValue(0.57);
        temp.setType((expandLevel > 0) ? entity.getMagtype() : null);
        //temp.setPublicID((expandLevel > 0) ? entity.getPrefor() : null);
        temp.setPublicID((expandLevel > 0) ? entity.getPrefmag() : null);
        this.mag.add(temp);

        this.origin = new ArrayList<OriginConverter>();
        OriginConverter temp2 = new OriginConverter(((expandLevel > 0) ? entity.getLat() : null).floatValue(),
                ((expandLevel > 0) ? entity.getLon() : null).floatValue(),
                ((expandLevel > 0) ? entity.getDepth() : null).floatValue()*1000,
                ((expandLevel > 0) ? entity.getTotalarr() : null).longValue(),
                ((expandLevel > 0) ? entity.getDatetime() : null).longValue());
        temp2.setPublicID(entity.getPrefor());
        this.origin.add(temp2);
    }

    /*
    public EvConverter(QuakemlView entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.setPublicID();
        //this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getEvid() + "/").build() : uri;
        this.expandLevel = expandLevel;
        this.mag = new ArrayList<NetMagConverter>();
        NetMagConverter temp = new NetMagConverter(((expandLevel > 0) ? entity.getMagnitude() : null).floatValue());
        temp.setOriginID("12345");
        //temp.setValue(0.57);
        temp.setType((expandLevel > 0) ? entity.getMagtype() : null);
        temp.setPublicID((expandLevel > 0) ? entity.getPrefor() : null);
        this.mag.add(temp);

        this.origin = new ArrayList<OriginConverter>();
        OriginConverter temp2 = new OriginConverter(((expandLevel > 0) ? entity.getLat() : null).floatValue(),
                ((expandLevel > 0) ? entity.getLon() : null).floatValue(),
                ((expandLevel > 0) ? entity.getDepth() : null).floatValue()*1000,
                ((expandLevel > 0) ? entity.getTotalarr() : null).longValue(),
                ((expandLevel > 0) ? entity.getDatetime() : null).longValue());
        this.origin.add(temp2);
        this.pick =  new ArrayList<PickConverter>();
        PickConverter temp3 = new PickConverter(1234567L);
        this.pick.add(temp3);
    }
    */
    //public EvConverter(QuakemlView entity, URI uri, int expandLevel) {
    //public EvConverter(QuakemlView entity, Collection<AssocaroPK> picks, URI uri, int expandLevel) {
    public EvConverter(QuakemlView entity, Collection<Assocaro> picks, Collection<Assocamo> amplitudes, URI uri, int expandLevel) {
        this(entity, picks, amplitudes, uri, expandLevel, false);
    }

    @XmlTransient
    public BigDecimal getDatetime() {
        return (expandLevel > 0) ? entity.getDatetime() : null;
    }

    /**
     * Setter for datetime.
     *
     * @param value the value to set
     */
    public void setDatetime(BigDecimal value) {
        entity.setDatetime(value);
    }

    /**
     * Getter for depth.
     *
     * @return value for depth
     */
    @XmlTransient
    public BigDecimal getDepth() {
        return (expandLevel > 0) ? entity.getDepth() : null;
    }

    /**
     * Setter for depth.
     *
     * @param value the value to set
     */
    public void setDepth(BigDecimal value) {
        entity.setDepth(value);
    }

    /**
     * Getter for category.
     *
     * @return value for category
     */
    @XmlElement(name = "type")
    public String getCategory() {
        return (expandLevel > 0) ? entity.getCategory() : null;
    }

    /**
     * Setter for category.
     *
     * @param value the value to set
     */
    public void setCategory(String value) {
        entity.setCategory(value);
    }

    /**
     * Getter for magnitude.
     *
     * @return value for magnitude
     */

    @XmlTransient
    public BigDecimal getMagnitude() {
    return (expandLevel > 0) ? entity.getMagnitude() : null;
    }

    /**
     * Setter for magnitude.
     *
     * @param value the value to set
     */
    public void setMagnitude(BigDecimal value) {
        entity.setMagnitude(value);
    }

    /**
     * Getter for lat.
     *
     * @return value for lat
     */
    @XmlTransient
    public BigDecimal getLat() {
        return (expandLevel > 0) ? entity.getLat() : null;
    }

    /**
     * Setter for lat.
     *
     * @param value the value to set
     */
    public void setLat(BigDecimal value) {
        entity.setLat(value);
    }

    /**
     * Getter for lon.
     *
     * @return value for lon
     */
    @XmlTransient
    public BigDecimal getLon() {
        return (expandLevel > 0) ? entity.getLon() : null;
    }

    /**
     * Setter for lon.
     *
     * @param value the value to set
     */
    public void setLon(BigDecimal value) {
        entity.setLon(value);
    }

    /**
     * Getter for evid.
     *
     * @return value for evid
     */
    @XmlTransient
    public Long getEvid() {
        return (expandLevel > 0) ? entity.getEvid() : null;
    }

    /**
     * Setter for evid.
     *
     * @param value the value to set
     */
    public void setEvid(Long value) {
        entity.setEvid(value);
    }

    /**
     * Getter for magtype.
     *
     * @return value for magtype
     */
    @XmlTransient
    public String getMagtype() {
        return (expandLevel > 0) ? entity.getMagtype() : null;
    }

    /**
     * Setter for magtype.
     *
     * @param value the value to set
     */
    public void setMagtype(String value) {
        entity.setMagtype(value);
    }

    /**
     * Getter for etype.
     *
     * @return value for etype
     */
    @XmlTransient
    public String getEtype() {
        return (expandLevel > 0) ? entity.getEtype() : null;
    }

    /**
     * Setter for etype.
     *
     * @param value the value to set
     */
    public void setEtype(String value) {
        entity.setEtype(value);
    }

    /**
     * Getter for totalarr.
     *
     * @return value for totalarr
     */
    @XmlTransient
    public Integer getTotalarr() {
        return (expandLevel > 0) ? entity.getTotalarr() : null;
    }

    /**
     * Setter for totalarr.
     *
     * @param value the value to set
     */
    public void setTotalarr(Integer value) {
        entity.setTotalarr(value);
    }

    /**
     * Getter for prefor.
     *
     * @return value for prefor
     */
    @XmlElement(name = "preferredOriginID")
    public String getPrefor() {
        Long temp;
        System.out.println("prefor = " + entity.getPrefor().toString());
        //return (expandLevel > 0) ? entity.getPrefor() : null;
        temp = (expandLevel > 0) ? entity.getPrefor() : null;
        return "smi:data.scec.org/origin/" + temp.toString();

    }

    /**
     * Setter for prefor.
     *
     * @param value the value to set
     */
    public void setPrefor(Long value) {
        entity.setPrefor(value);
    }

    /**
     * Getter for prefmag.
     *
     * @return value for prefmag
     */
    @XmlElement(name = "preferredMagnitudeID")
    //@XmlElement(name = "gobledygook")
    public String getPrefmag() {
        Long temp;
        temp = (expandLevel > 0) ? entity.getPrefmag() : null;
        return "smi:data.scec.org/magnitude/" + temp.toString();
        //return (expandLevel > 0) ? entity.getPrefmag() : null;
    }

    /**
     * Setter for prefmag.
     *
     * @param value the value to set
     */
    public void setPrefmag(Long value) {
        entity.setPrefmag(value);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the QuakemlView entity.
     *
     * @return an entity
     */
    @XmlTransient
    public QuakemlView getEntity() {
        //if (entity.getEvid() == null) {
        if (entity.getEvid() < 0) {
            //EventParameterConverter converter = UriResolver.getInstance().resolve(EventParameterConverter.class, uri);
            EvConverter converter = UriResolver.getInstance().resolve(EvConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved QuakemlView entity.
     *
     * @return an resolved entity
     */
    public QuakemlView resolveEntity(EntityManager em) {
        return entity;
    }

    public void setPublicID(){
        this.publicID = "smi:data.scec.org/event/" + this.entity.getEvid();


    }
}
