package app;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Petr Flégl on 19.10.2016.
 */
public class DataUtil {
    public  static int dateToTimestamp(String date) {
        int d = 12;
        int m = 12;
        int y = 2012;
        if(date.length()==8) {
            d = Integer.valueOf(date.substring(0,2));
            m = Integer.valueOf(date.substring(2,4));
            y = Integer.valueOf(date.substring(4,8));
        }else if(date.length()==7) {
            d = Integer.valueOf(date.substring(0,1));
            m = Integer.valueOf(date.substring(1,3));
            y = Integer.valueOf(date.substring(3,7));
        }
        Calendar ts = Calendar.getInstance();
        ts.set(y,m,d,0,0,0);
        ts.add(Calendar.DAY_OF_MONTH,-30);
        return (int) (ts.getTimeInMillis()/1000);

    }

    public static int dateToTimestamp(Date date){
        Calendar ts = Calendar.getInstance();
        ts.setTime(date);
        return (int) (ts.getTimeInMillis()/1000);
    }
    public static int dateToTimestamp(String date, String delimiter){
        int d = 12;
        int m = 12;
        int y = 2012;
        long timestamp = 0;
        String dateparts[] = date.split(delimiter);

        if (dateparts.length == 3) {
            d = Integer.valueOf(dateparts[0]);
            m = Integer.valueOf(dateparts[1]);
            y = Integer.valueOf(dateparts[2]);
        }


    Calendar ts = Calendar.getInstance();
    ts.set(y,m,d,0,0,0);
        ts.add(Calendar.DAY_OF_MONTH,-30);
        timestamp = (ts.getTimeInMillis()/1000);
    return (int) timestamp;
    }

    public static int dateToTimestamp(int day, int month, int year){
        long timestamp = 0;
        Calendar ts = Calendar.getInstance();
        ts.set(year,month,day,0,0,0);
        ts.add(Calendar.DAY_OF_MONTH,-30);
        timestamp = (ts.getTimeInMillis()/1000);

        return (int) timestamp;
    }

    public static int getYear(int yearsBack) {
        Calendar prevYear = Calendar.getInstance();
        prevYear.add(Calendar.YEAR, -yearsBack);
        return prevYear.get(Calendar.YEAR);
    }

    public static int getMonth(int monthsBack){
        Calendar prevMonth = Calendar.getInstance();
        prevMonth.add(Calendar.MONTH, -monthsBack);
        return prevMonth.get(Calendar.MONTH);
    }

    public static int getDay(int daysBack){
        Calendar prevDay = Calendar.getInstance();
        prevDay.add(Calendar.DAY_OF_YEAR, -daysBack);
        return prevDay.get(Calendar.DAY_OF_MONTH);
    }

    public static int getTimestampForDay(int daysBack){
        Calendar tsForDay = Calendar.getInstance();
        tsForDay.add(Calendar.DAY_OF_YEAR,-daysBack);
        tsForDay.set(Calendar.HOUR,0);
        tsForDay.set(Calendar.MINUTE,0);
        tsForDay.set(Calendar.SECOND,0);
        long ts =(tsForDay.getTimeInMillis()/1000);
        return (int) ts;
    }

    public static int secForYears(int numOfYears){
        //          s   m   h   d       nY
        return (    60* 60* 24* 365*    numOfYears);
    }

    public static int timeToTimestamp(String time, String delimiter){
        int h = 12;
        int m = 12;
        int s = 12;
        long timestamp = 0;
        String dateparts[] = time.split(delimiter);

        if (dateparts.length == 3) {
            h = Integer.valueOf(dateparts[0]);
            m = Integer.valueOf(dateparts[1]);
            s = Integer.valueOf(dateparts[2]);
        }


        Calendar ts = Calendar.getInstance();
        ts.set(1,1,1970,h,m,s);
        timestamp = (ts.getTimeInMillis()/1000);
        return (int) timestamp;

    }
}
