package com.mints.goodmoney.utils;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.EditText;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeRender {
    /**
     * 默认日期格式
     */
    public static String DEFAULT_FORMAT = "yyyy-MM-dd";

    public static String DEFAULT_FORMAT_DAY = "dd";
    public static String DEFAULT_FORMAT_MONTH_DAY = "MM月dd日";
    public static String DEFAULT_FORMAT_LINE= "yyyy/MM/dd";
    public static String DEFAULT_FORMAT_TIME= "HH:mm:ss";

    /**
     * 年月日时分秒加横杠
     */
    public static String ALL_FORMAT_LINE = "yyyy-MM-dd HH:mm:ss";

    /**
     * 年月日时分秒
     */
    public static String ALL_FORMAT = "yyyyMMddHHmmss";

    /**
     * 显示年月日
     */
    public static String DEFAULT_FORMAT_SHOW = "yyyy年MM月dd日";


    private static SimpleDateFormat formatBuilder;

    public static String getDate(String format) {
        formatBuilder = new SimpleDateFormat(format);
        return formatBuilder.format(new Date());
    }

    public static String getDate() {
        return getDate(ALL_FORMAT_LINE);
    }

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    public static String formatDate(Date date) {
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    public static String formatDay(Date date) {
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT_DAY);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    public static String formatMonthDay(Date date) {
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT_MONTH_DAY);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 格式化日期
     *
     * @param date   日期对象
     * @param format 日期格式
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat f = new SimpleDateFormat(format);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * string 获取时间data
     *
     * @param dates
     * @param format
     * @return
     */
    public static Date parseStringDate(String dates, String format) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat(format);
        Date date = f.parse(dates);
        return date;
    }


    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getCurrYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getCurrYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }

    /**
     * 获取当前月的第一天
     *
     * @return string
     */
    public static String getMonthStart() {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return formatDate(calendar.getTime());
    }

    /**
     * 获取当前月的最后一天
     *
     * @return string
     */
    public static String getMonthEnd() {
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return formatDate(calendar.getTime());
    }

    /**
     * 获取当天的日期
     *
     * @return string
     */
    public static String getDayNew() {
        Calendar calendar = Calendar.getInstance();
        return formatDate(calendar.getTime());
    }

    private static String nqMonth;
    private static String nqDay;

    public static void GetTime(Context context, String data, final EditText editText) {
        String[] split = data.split("-");
        int yearZ = Integer.parseInt(split[0]);
        int monthZ = Integer.parseInt(split[1]) - 1;
        int dayZ = Integer.parseInt(split[2]);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                if ((monthOfYear + 1) < 10) {
                    nqMonth = "0" + (monthOfYear + 1);
                } else {
                    nqMonth = String.valueOf((monthOfYear + 1));
                }
                if (dayOfMonth < 10) {
                    nqDay = "0" + dayOfMonth;
                } else {
                    nqDay = String.valueOf(dayOfMonth);
                }
                editText.setText(year + "-" + nqMonth + "-" + nqDay);
            }
        }, yearZ, monthZ, dayZ);
        datePickerDialog.show();
    }

    /**
     * 判断两个时间间的间隔天数
     */
    public static int dateCompare(String s1, String s2) {
        // 设定时间的模板
        formatBuilder = new SimpleDateFormat(DEFAULT_FORMAT);
        // 得到指定模范的时间
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = formatBuilder.parse(s1);
            d2 = formatBuilder.parse(s2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long abs = (d2.getTime() - d1.getTime()) / (24 * 3600 * 1000);
        return (int) abs;
    }

    /**
     * 时间格式化
     *
     * @param _ms
     * @return
     */
    public static String ms2HMS(int _ms) {
        String HMStime;
        _ms /= 1000;
        int hour = _ms / 3600;
        int mint = (_ms % 3600) / 60;
        int sed = _ms % 60;
        String hourStr = String.valueOf(hour);
        if (hour < 10) {
            hourStr = "0" + hourStr;
        }
        String mintStr = String.valueOf(mint);
        if (mint < 10) {
            mintStr = "0" + mintStr;
        }
        String sedStr = String.valueOf(sed);
        if (sed < 10) {
            sedStr = "0" + sedStr;
        }
        HMStime = hourStr + ":" + mintStr + ":" + sedStr;
        return HMStime;
    }
}
