package com.android.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by chengzhenyu on 16/1/17.
 */
public class TimeUtils {

    private static final long SECONDS_INTERVAL = 60;

    private static final long MINUTES_INTERVAL = SECONDS_INTERVAL * 60;

    private static final long HOURS_INTERVAL = MINUTES_INTERVAL * 24;

    private static final long DAYS_INTERVAL = HOURS_INTERVAL * 31;

    private static final long MONTHS_INTERVAL = DAYS_INTERVAL * 12;

    private static final String SECONDS_SUFFIX = "秒前";

    private static final String MINUTES_SUFFIX = "分钟前";

    private static final String HOURS_SUFFIX = "小时前";

    private static final String DAYS_SUFFIX = "天前";

    private static final String MONTHS_SUFFIX = "月前";

    private static final String YEARS_SUFFIX = "年前";

    public static String getGatherTimeStr(long gatherTime){
        String gatherStr = "";
        try{
            SimpleDateFormat localFormatter = new SimpleDateFormat("HH:mm");
            gatherStr = localFormatter.format(gatherTime);
        }catch (Exception e){
            e.printStackTrace();
        }
        return gatherStr;
    }

    public static String utc2Local(String utcTime, String utcTimePatten,
                                   String localTimePatten) {
        String localTime = "";
        try {
            SimpleDateFormat utcFormatter = new SimpleDateFormat(utcTimePatten);
            utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date gpsUTCDate = utcFormatter.parse(utcTime);
            SimpleDateFormat localFormatter = new SimpleDateFormat(localTimePatten);
            localFormatter.setTimeZone(TimeZone.getDefault());
            localTime = localFormatter.format(gpsUTCDate.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localTime;
    }

    public static String getStandardTime(String utcTime) {
        return utc2Local(utcTime, "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd HH:mm");
    }

    public static long getStandardTimeInMiliSec(String utcTime){
        long time = 0;
        String standardTimeStr = getStandardTime(utcTime);
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(standardTimeStr);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String getFormattedTime(String utcTime) {
        String formattedTime = utc2Local(utcTime, "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd HH:mm");
        return getHumanisticTime(formattedTime, "yyyy-MM-dd HH:mm");
    }

    private static String getHumanisticTime(String formattedTime, String format) {

        String result = formattedTime;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            long second = df.parse(formattedTime).getTime() / 1000;
            long curSecond = new Date().getTime() / 1000;
            long distance = curSecond - second;
            if (distance <= 0) {
                result = formattedTime;
            } else if (distance < SECONDS_INTERVAL) {
                result = distance + SECONDS_SUFFIX;
            } else if (distance < MINUTES_INTERVAL) {
                result = (int) Math.ceil(distance / SECONDS_INTERVAL) + MINUTES_SUFFIX;
            } else if (distance < HOURS_INTERVAL) {
                long count = distance / MINUTES_INTERVAL;
                count = (count == 0 ? 1 : count);
                result = count + HOURS_SUFFIX;
            } else if (distance < DAYS_INTERVAL) {
                long count = distance / HOURS_INTERVAL;
                count = (count == 0 ? 1 : count);
                result = count + DAYS_SUFFIX;
            } else if (distance < MONTHS_INTERVAL) {
                long count = distance / DAYS_INTERVAL;
                count = (count == 0 ? 1 : count);
                result = count + MONTHS_SUFFIX;
            } else {
                long count = distance / MONTHS_INTERVAL;
                count = (count == 0 ? 1 : count);
                result = count + YEARS_SUFFIX;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
