package de.fu_berlin.cdv.chasingpictures.api;

import android.location.Location;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * This class represents a place, a.k.a. the thing users have to search for.
 *
 * @author Simon Kalt
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class Place implements Serializable {
    private int id;
    private String name;
    private double latitude;
    private double longitude;
    private String description;
    private List<Picture> pictures;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Picture> getPictures() {
        return pictures;
    }

    public void setPictures(List<Picture> pictures) {
        this.pictures = pictures;
    }

    /**
     * Get the first picture.
     *
     * @return The first picture associated with this place.
     * @deprecated Use {@link #getFirstPicture()} instead
     */
    @JsonIgnore
    @Deprecated
    public Picture getPicture() {
        return getFirstPicture();
    }

    /**
     * Get the first picture.
     *
     * @return The first picture associated with this place.
     */
    private Picture getFirstPicture() {
        return pictures == null || pictures.isEmpty() ? null : pictures.get(0);
    }

    /**
     * Sets the list of pictures associated with this place
     * to the single picture provided.
     *
     * @param picture The picture to set
     * @deprecated Use {@link #setPictures(List)} instead
     */
    @JsonIgnore
    @Deprecated
    public void setPicture(Picture picture) {
        this.pictures = Collections.singletonList(picture);
    }

    /**
     * Returns a {@link Location} describing latitude and longitude of this place.
     *
     * @return A {@link Location} object describing this place
     */
    public Location getLocation() {
        Location location = new Location("");
        location.setLatitude(latitude);
        location.setLongitude(longitude);

        return location;
    }

    /**
     * Calculates the distance of this place to another location.
     *
     * @param location (Own) location
     * @return Distance to the given location (in meters)
     */
    public float distanceTo(Location location) {
        return getLocation().distanceTo(location);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Place place = (Place) o;

        if (Double.compare(place.latitude, latitude) != 0) return false;
        if (Double.compare(place.longitude, longitude) != 0) return false;
        if (name != null ? !name.equals(place.name) : place.name != null) return false;
        if (description != null ? !description.equals(place.description) : place.description != null)
            return false;
        return !(pictures != null ? !pictures.equals(place.pictures) : place.pictures != null);

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = name != null ? name.hashCode() : 0;
        temp = Double.doubleToLongBits(latitude);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(longitude);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (pictures != null ? pictures.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Place{" +
                "name='" + name + '\'' +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                ", description='" + description + '\'' +
                ", pictures=" + pictures +
                '}';
    }
}

@@ -643,16 +643,73 @@
 ictures;
+%0A    private Double bearing;%0A    private Double distance;
 %0A%0A    pu
@@ -1590,32 +1590,1036 @@
 ictures;%0A    %7D%0A%0A
+    /**%0A     * If this place was requested from the API with a given location,%0A     * returns the bearing from that location to this place.%0A     */%0A    public Double getBearing() %7B%0A        return bearing;%0A    %7D%0A%0A    /**%0A     * Set the bearing to this place. Value will be%0A     * automatically wrapped into the range %5B0, 360).%0A     */%0A    public void setBearing(double bearing) %7B%0A        while (bearing %3C 0.0f) %7B%0A            bearing += 360.0f;%0A        %7D%0A        while (bearing %3E= 360.0f) %7B%0A            bearing -= 360.0f;%0A        %7D%0A        this.bearing = bearing;%0A    %7D%0A%0A    /**%0A     * If this place was requested from the API with a given location,%0A     * returns the distance from that location to this place.%0A     * %3Cp/%3E%0A     * Since this is the distance to the location at request time,%0A     * consider using %7B@link #distanceTo(Location)%7D.%0A     */%0A    public Double getDistance() %7B%0A        return distance;%0A    %7D%0A%0A    public void setDistance(double distance) %7B%0A        this.distance = distance;%0A    %7D%0A%0A
     /**%0A     * G

Add distance and bearing attributes from API to Place class.