package com.cheng.blzb.utils

import android.text.TextUtils
import com.example.base.extensions.getYYYYMMDD
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

object DateUtils {
    //获取今天的开始时间
    fun getDayStartTime(time: Long = System.currentTimeMillis()): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取今天的结束时间
    fun getDayEndTime(time: Long = System.currentTimeMillis()): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 23:59:59")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取昨天的开始时间
    fun getYesterdayStartTime(time: Long = System.currentTimeMillis()): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.add(Calendar.DAY_OF_MONTH, -1)
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取昨天的结束时间
    fun getYesterdayEndTime(time: Long = System.currentTimeMillis()): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 23:59:59")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.add(Calendar.DAY_OF_MONTH, -1)
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取本周第一天开始时间
    fun getWeekFirstDayTime(): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${System.currentTimeMillis().getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.firstDayOfWeek = Calendar.MONDAY
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
//            L.d("weekFirst>>${simpleDateFormat.format(calendar.time)}")
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取本周最后一天时间
    fun getWeekEndDayTime(): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${System.currentTimeMillis().getYYYYMMDD()} 23:59:59")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.firstDayOfWeek = Calendar.MONDAY
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
//            L.d("weekEnd>>${simpleDateFormat.format(calendar.time)}")
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取本月第一天开始时间
    fun getMonthFirstDayTime(time: Long = System.currentTimeMillis()): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.set(Calendar.DAY_OF_MONTH, 1)
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    //获取本月最后一天时间
    fun getMonthEndDayTime(time: Long = System.currentTimeMillis()): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 23:59:59")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     * @desc 获取以当前时间为结束时间的之前一周的开始时间
     */
    fun getBeforeWholeWeekTime(endTime: Long): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${endTime.getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.add(Calendar.DAY_OF_MONTH, -6)
            return calendar.timeInMillis / 1000
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     * @desc 获取以当前时间为开始时间的之后一周的每一天时间
     * @param startTime 开始时间
     * @param day 第几天
     */
    fun getDayOfWeekTimeMillisByStartTime(startTime: Long, day: Int): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${startTime.getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.add(Calendar.DAY_OF_MONTH, day - 1)
            return calendar.timeInMillis
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     * @desc 获取以当前时间为结束时间的之前一周的每一天时间
     * @param endTime 结束时间
     * @param day 第几天
     */
    fun getDayOfWeekTimeMillisByEndTime(endTime: Long, day: Int): Long {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${endTime.getYYYYMMDD()} 23:59:59")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            calendar.add(Calendar.DAY_OF_MONTH, day - 7)
            return calendar.timeInMillis
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     * @desc 获取当前月份的天数
     */
    fun getDayOfMonthCount(time: Long): Int {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        try {
            val date = simpleDateFormat.parse("${time.getYYYYMMDD()} 00:00:00")
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            return calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     * 通过指定的年份和月份获取当月有多少天.
     *
     * @param year  年.
     * @param month 月.
     * @return 天数.
     */
    fun getMonthDays(year: Int, month: Int): Int {
        val calendar = Calendar.getInstance()
        calendar.clear()
        calendar.set(Calendar.YEAR, year)
        calendar.set(Calendar.MONTH, month)
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
    }

    /**
     * 获取指定年月的 1 号位于周几.
     * @param year  年.
     * @param month 月.
     * @return      周.
     */
    fun getFirstDayWeek(year: Int, month: Int): Int {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR, year)
        calendar.set(Calendar.MONTH, month)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.get(Calendar.DAY_OF_WEEK)
    }

    /**
     * 获取指定年月的最后一天位于周几.
     *
     * @param year  年.
     * @param month 月.
     * @return      周.
     */
    fun getLastDayWeek(year: Int, month: Int): Int {
        val calendar = Calendar.getInstance(Locale.CHINA)
        calendar.clear()
        calendar.set(Calendar.YEAR, year)
        calendar.set(Calendar.MONTH, month)
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
        return calendar.get(Calendar.DAY_OF_WEEK)
    }

    fun getHour(): Int {
        val calendar = Calendar.getInstance()
        return calendar.get(Calendar.HOUR_OF_DAY)
    }

    fun getMinute(): Int {
        val calendar = Calendar.getInstance()
        return calendar.get(Calendar.MINUTE)
    }

    //日期转时间戳
    fun strDateToLong(str: String, pattern: String = "yyyy-MM-dd HH:mm"): Long {
        if (TextUtils.isEmpty(str)) return 0

        val simpleDateFormat = SimpleDateFormat(pattern, Locale.CHINA)
        try {
            val date = simpleDateFormat.parse(str)
            val calendar = Calendar.getInstance()
            calendar.time = date!!
            return calendar.timeInMillis
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

}