package org.hso.ecommerce.supplier.data;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class Delivery {

    private String[] states = {"Bestellung eingegangen","Bestellung auf dem Weg","Lieferung erfolgreich"};
    private int[] timeBorder = {4,24};

    private String name;
    private String address;
    private String estimatedArrival;
    private Date creationTime;
    private UUID uuid;

    public Delivery(String name, String address)
    {
        this.name = name;
        this.address = address;
        this.uuid = UUID.randomUUID();
        this.creationTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        this.estimatedArrival = formatter.format(addDays((Date)this.creationTime.clone(),1));
    }


    public String getStatus()
    {
        Date now = new Date();
        Long timeNow = now.getTime();
        Long creationTime = this.creationTime.getTime();

        Long diff = timeNow - creationTime;
        double hour = (((diff / 1000.0) / 3600.0));

        for (int i = 0; i <  timeBorder.length; i++) {

            if(hour < timeBorder[i])
                return states[i];
        }

        return states[timeBorder.length];
    }

    public String getEstimatedArrival() {

        return estimatedArrival;
    }

    private Date addDays(Date date, int days)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public UUID getUuid() {
        return uuid;
    }

    public String getName() {
        return name;
    }
}
