package com.ttiglobal.inttilectgpchat.blog.Helper;

import java.text.SimpleDateFormat;

import static java.lang.Long.valueOf;

public class TimeFormatter {

    public String getTime(String time) {
        long lg = valueOf(time);
        long currentTime = System.currentTimeMillis();
        long timeDifference = currentTime - lg;

        SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
        SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
        SimpleDateFormat sdfDay = new SimpleDateFormat("dd");
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yy");

        String timeString = "";
        if (timeDifference <= 600000) {
            return "Just now";
        } else if (timeDifference <= 3.6e+6) {
            timeString = sdfMinute.format(timeDifference);
            return timeString.replaceFirst("0", "") + " minutes ago";
        } else if (timeDifference <= 8.64e+7) {
            timeString = sdfHour.format(timeDifference);
            return timeString.replaceFirst("0", "") + " hours ago";
        } else if (timeDifference <= 2.592e+9) {
            timeString = sdfDay.format(timeDifference);
            return timeString.replaceFirst("0", "") + " days ago";
        } else {
            timeString = sdf.format(time);
            return timeString;
        }
    }
}
