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

import TestRest.converter.OriginConverter;
import TestRest.QuakemlView;
import java.math.BigDecimal;
import java.net.URI;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author aparnab
 */

public class QuakemlViewConverter {
    
    private QuakemlView entity;
    private URI uri;
    private int expandLevel;
    @XmlAttribute
    private String publicID = "smi:data.scec.org/catalog/1";;
     
    //@XmlElement(name="preferredOriginID")
    //String preferredOriginId = "smi:data.scec.org/origin"; //+ entity.getPrefor().toString();

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

    
    
/** Creates a new instance of QuakemlViewConverter */
    
    public QuakemlViewConverter() {
        //event = new EventParameterConverter();
        entity = new QuakemlView();
        
    }

    /**
     * Creates a new instance of QuakemlViewConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */

     public QuakemlViewConverter(QuakemlView entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        //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(entity.getPrefor().toString());
        //temp.setValue(0.57);
        temp.setType((expandLevel > 0) ? entity.getMagtype() : null);
        temp.setPublicID((expandLevel > 0) ? entity.getEvid() : 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);
    }

    /**
     * Creates a new instance of QuakemlViewConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */

    public QuakemlViewConverter(QuakemlView entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }
    /**
     * Getter for datetime.
     *
     * @return value for datetime
     */
   
    @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);
    }


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

    }


    public void setPrefor(Long value) {
        entity.setPrefor(value);
    }


    @XmlElement(name = "preferredMagnitudeID")
    //@XmlElement(name = "clickandclack")
    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;
    }


    public void setPrefmag(Long value) {
        entity.setPrefmag(value);
    }

    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    public void setUri(URI uri) {
        this.uri = uri;
    }

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

    public QuakemlView resolveEntity(EntityManager em) {
        return entity;
    }
     
}

    
    