package db;

import hu.elte.inf.pszt.prt.javalib.db.jpa.EntityWithID;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 *
 * @author trust56
 */
@Entity
public class Task implements Serializable, EntityWithID {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Override
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Task)) {
            return false;
        }
        Task other = (Task) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "vehiclerepository.Rental[ id=" + id + " ]";
    }
    @OneToOne
    private Person partner;

    public Person getPartner() {
        return partner;
    }

    public void setPartner(Person partner) {
        this.partner = partner;
    }
    @OneToOne
    private Category vehicle;

    public Category getVehicle() {
        return vehicle;
    }

    public void setVehicle(Category vehicle) {
        this.vehicle = vehicle;
    }
    private Date startOfRental;

    public Date getStartOfRental() {
        return startOfRental;
    }

    public void setStartOfRental(Date startOfRental) {
        this.startOfRental = startOfRental;
    }
    private Date endOfRental;

    public Date getEndOfRental() {
        return endOfRental;
    }

    public void setEndOfRental(Date endOfRental) {
        this.endOfRental = endOfRental;
    }
    private Date bringBackRental;

    public Date getBringBackRental() {
        return bringBackRental;
    }

    public void setBringBackRental(Date bringBackRental) {
        this.bringBackRental = bringBackRental;
    }

    public int getPriceOfRental() {
        int pricePerDay = vehicle.getPricePerDay();
        int days;
        int unusedDays;
        int lateDays;
        if (bringBackRental.after(endOfRental)) {
            days = (int) ((endOfRental.getTime() - startOfRental.getTime()) / 1000 / 60 / 60 / 24);
            unusedDays = 0;
            lateDays = (int) ((bringBackRental.getTime() - endOfRental.getTime()) / 1000 / 60 / 60 / 24);
        } else {
            days = (int) ((bringBackRental.getTime() - startOfRental.getTime()) / 1000 / 60 / 60 / 24);
            unusedDays = (int) ((endOfRental.getTime() - bringBackRental.getTime()) / 1000 / 60 / 60 / 24);
            lateDays = 0;
        }
        return days * pricePerDay + (unusedDays * pricePerDay / 2) + (lateDays * pricePerDay * 2);
    }
}
