package kr.co.hanwha.hifive.util;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

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

import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 1. 18..
 */

public class HFDateUtil {
    /**
     * 기본 날짜 포맷
     */
    public static String FORMAT_COMMON_SERVER = "yyyy-MM-dd HH:mm:ss";
    /**
     * TASK 기한설정 날짜 포맷 - Server
     */
    public static String FORMAT_TASK_DUEDATE_SERVER = "yyyy-MM-dd HH:mm:ss";
    /**
     * HOME 기본 오늘 날짜 포맷
     */
    public static String FORMAT_DEFAULT_HOME = "d MMMM, EEEE";
    /**
     * TASK 등록시 사용하는 날짜 포맷
     */
    public static String FORMAT_TASK_ADD_DUEDATE_SERVER = "yyyy-MM-dd";
    /**
     * 캘린더용 월 표시 포맷
     */
    public static String FORMAT_CALENDAR_MONTH_DISPLAY = "MMMM";
    /**
     * TASK 기한 설정 날짜 포맷
     */
    public static String FORMAT_TASK_DUEDATE_DISPLAY = "yyyy-MM-dd";
    /**
     * yyyy-MM-dd
     */
    public static String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static String FORMAT_YEAR = "yyyy";
    public static String FORMAT_MONTH = "M";
    public static String FORMAT_DAY = "d";

    /**
     * 오늘 날짜
     *
     * @return 날짜 포맷 d, MMMM, EEEE
     */
    public static String getDateFormatToday() {
        return getDateFormatToday(FORMAT_DEFAULT_HOME);
    }

    /**
     * 오늘 날짜
     *
     * @return 지정 날짜 포맷
     */
    public static String getDateFormatToday(@NonNull String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);

        return formatter.format(calendar.getTime());
    }

    /**
     * 오늘 날짜
     *
     * @return 지정 날짜 포맷
     */
    public static String getDateFormat(int year, int month, int date, String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date);
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);

        return formatter.format(calendar.getTime());
    }

    /**
     * 오늘인지 확인
     *
     * @param inputDate 입력 날짜
     * @param format    입력 날짜 포맷
     * @return Boolean
     */
    public static boolean checkIsToday(@NonNull String inputDate, @NonNull String format) {
        String input = getChangeDateFormat(inputDate, format, FORMAT_YYYY_MM_DD);
        String today = getDateFormatToday(FORMAT_YYYY_MM_DD);
        return (today.equals(input));
    }

    /**
     * @param inputDate String 형식 날짜
     * @return
     */
    public static String getDateFormatCommon(@NonNull Context context, @NonNull String inputDate) {
        String inputFormat = FORMAT_COMMON_SERVER;
        String outputFormat;
        long inputTime = getTimestampFromStringFormat(inputDate, inputFormat);
        long currentTime = getTimestampToday();
        long d = (currentTime - inputTime) / 1000;

        try {
            if (d > 60 * 60 * 24) {
                outputFormat = "yyyy-MM-dd";
                inputDate = getDateFormatFromTimestamp(inputTime, outputFormat);
            } else if (d > 60 * 60 * 3) {
                outputFormat = "yyyy-MM-dd";
                inputDate = getDateFormatFromTimestamp(inputTime, outputFormat);
                String currentDate = getDateFormatFromTimestamp(currentTime, outputFormat);
                if (currentDate.equals(inputDate)) {
                    outputFormat = "HH:mm";
                    inputDate = getDateFormatFromTimestamp(inputTime, outputFormat);
                }
            } else if (d > 60 * 60) {
                inputDate = Long.toString((d / (60 * 60))) + context.getString(R.string.date_hours_ago);
            } else if (d > 60) {
                inputDate = Long.toString((d / 60)) + context.getString(R.string.date_minutes_ago);
            } else {
                inputDate = 1 + context.getString(R.string.date_minutes_ago);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputDate;
    }

    /**
     * 날짜 포맷 변경
     *
     * @param inputDate    String 형식 날짜
     * @param inputFormat  입력 날짜 포맷
     * @param outputFormat 출력 날짜 포맷
     * @return String
     */
    public static String getChangeDateFormat(@NonNull String inputDate, @NonNull String inputFormat, @NonNull String outputFormat) {
        long timestamp = getTimestampFromStringFormat(inputDate, inputFormat);
        return getDateFormatFromTimestamp(timestamp, outputFormat);
    }

    /**
     * 날짜 포맷 변경
     *
     * @param time   Timestamp
     * @param format 지정 포맷
     * @return String
     */
    private static String getDateFormatFromTimestamp(@NonNull long time, @NonNull String format) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA); // TODO - CHECK

        return formatter.format(calendar.getTime());
    }

    /**
     * 마지막 날짜 반환
     *
     * @param year  년도
     * @param month 월
     * @return 일
     */
    public static int getActualMaximum(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return calendar.getActualMaximum(Calendar.DATE);
    }

    /**
     * 요일 반환
     *
     * @param year  년도
     * @param month 월
     * @param date  일
     * @return 요일
     */
    public static int getDayOfWeek(int year, int month, int date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 오늘 포함 2주 이내인지 확인
     *
     * @param year  년도
     * @param month 월
     * @param date  일
     * @return Boolean
     */
    public static boolean checkTwoWeeksFromToday(int year, int month, int date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date);

        Calendar today = Calendar.getInstance();
        today.setTimeInMillis(System.currentTimeMillis());

        if (today.getTimeInMillis() > calendar.getTimeInMillis()) return false;

        today.add(Calendar.DATE, 14);
        return (today.getTime().getTime() > calendar.getTime().getTime());
    }

    /**
     * 날짜 비교
     *
     * @param year
     * @param month
     * @param date
     * @param dueTime
     * @return
     */
    public static boolean checkSelectedDate(int year, int month, int date, String dueTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date);
        String checkDate = getDateFormatFromTimestamp(calendar.getTimeInMillis(), FORMAT_TASK_ADD_DUEDATE_SERVER);
        return checkDate.equals(dueTime);
    }

    /**
     * 오늘 날짜 -> 지정 포맷으로 변경
     *
     * @param format 지정 포맷
     * @return String
     */
    public static String getDateFormatFromToday(String format) {
        long timestamp = getTimestampToday();
        return getDateFormatFromTimestamp(timestamp, format);
    }

    /**
     * String Date -> Timestamp
     *
     * @param inputDate 날짜 문자열
     * @param format    날짜 포맷
     * @return Timestamp
     */
    private static long getTimestampFromStringFormat(@NonNull String inputDate, @NonNull String format) {

        Date date = new Date();
        try {
            if (TextUtils.isEmpty(inputDate) == false) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
                date = simpleDateFormat.parse(inputDate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * D-Day 계산
     *
     * @param inputDate
     * @param format
     * @return
     */
    public static int getCalculateDDayFromToday(@NonNull String inputDate, @NonNull String format) {
        long date = getTimestampFromStringFormat(inputDate, format);
        long today = getTimestampToday();

        long result = date - today;
        int d_day = (int) (result / (1000 * 60 * 60 * 24)) - (result < 0 ? 1 : 0);
        return d_day;
    }

    /**
     * 현재 시각(Timestamp)
     *
     * @return Timestamp
     */
    public static long getTimestampToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        return calendar.getTime().getTime();
    }
}
