package com.xty.common

import android.text.SpannableString
import com.tamsiree.rxkit.RxTimeTool
import com.xty.common.util.SpannableUtils
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*


object TimeUtils {

    fun secToHour(sec: Long): String {
        return  String.format("%.1f",sec / 60.0)
      // return BigDecimal(str).setScale(1,RoundingMode.UP).toString()
      /*var h = sec / 60
        var s = sec % 60
        return if (h > 0) {
            if (s > 0) {
                "$h.$s"
            } else {
                "$h"
            }
        } else {
            "0.$s"


        }*/
    }

    fun secToStrHour(sec: Long): String {
        return "${sec / 60}小时${ String.format("%02d",sec % 60)}分"
    }

    fun secToSpan(sec: Long, spanSize: Int, fontColor: Int = -1,
                  styleSpan: Int = -1): SpannableString{
        val hour = sec / 60
        val minute = String.format("%02d",sec % 60)
        val str = "${hour}小时${minute}分"
        return SpannableUtils.setStrSpan(str, spanSize, fontColor, styleSpan, 0, hour.toString().length, hour.toString().length + 2, str.length - 1)
    }

    fun secToStrMinutes(sec: Long): Array<String>{
        return arrayOf("${sec / 60}", "${sec % 60}")
    }

    /**
     * 获取当前日期的上一周的日期时间段
     */
    fun getPreviousWeekDateRange(): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        val calendar = Calendar.getInstance()

        // 设置 Calendar 的日期为当前日期，并将其移到上一周的同一天
        calendar.add(Calendar.WEEK_OF_YEAR, -1)

        // 将 Calendar 的日期设置为上一周的周一
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
        val startDate = calendar.time

        // 将 Calendar 的日期设置为上一周的周日
        calendar.add(Calendar.DAY_OF_WEEK, 6)
        val endDate = calendar.time

        // 格式化日期为字符串
        val startDateString = dateFormat.format(startDate)
        val endDateString = dateFormat.format(endDate)

        return "$startDateString 至$endDateString"
    }

    /**
     * 获取日报时间
     */
    fun getDayReportTime(format:String = "yyyy-MM-dd"): String{
        val calendar = Calendar.getInstance()
        if (calendar[Calendar.HOUR_OF_DAY] < 11){//如果为11点前，则用昨天的日期
            calendar.add(Calendar.DAY_OF_MONTH, -1)
        }
        return RxTimeTool.milliseconds2String(calendar.timeInMillis, SimpleDateFormat(format))
    }

    /**
     * 获取年筛选时间
     */
    fun getYearTime(format:String = "yyyy"): String{
        val calendar = Calendar.getInstance()
        if (calendar[Calendar.HOUR_OF_DAY] < 11){//如果为11点前，则用昨天的日期
            calendar.add(Calendar.DAY_OF_MONTH, -1)
        }
        return RxTimeTool.milliseconds2String(calendar.timeInMillis, SimpleDateFormat(format))
    }
    /**
     * 获取周筛选时间
     */
    fun getWeekTime(): String{
        return get7DaysBefore()+"至"+getCurrentTime()
    }
    fun getCurrentTime(): String {
        val calendar = Calendar.getInstance()
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        return formatter.format(calendar.time)
    }
    fun get7DaysBefore(): String {
         val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_MONTH, -6)
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        return formatter.format(calendar.time)
    }
    fun getYearBefore(): String {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.YEAR, -1)
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        return formatter.format(calendar.time)
    }
    fun getYearBeforeCalender(): Calendar {
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.YEAR, -1)
        return calendar
    }
    /**
     * 获取月筛选时间
     */
    fun getMonthTime(format:String = "yyyy-MM"): String{
        val calendar = Calendar.getInstance()
        if (calendar[Calendar.HOUR_OF_DAY] < 11){//如果为11点前，则用昨天的日期
            calendar.add(Calendar.DAY_OF_MONTH, -1)
        }
        return RxTimeTool.milliseconds2String(calendar.timeInMillis, SimpleDateFormat(format))
    }
    /**
     * 获取当前时间前一天
     */
    fun getBeforDay(format:String = "yyyy-MM-dd"): String{
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_MONTH, -1)
        return RxTimeTool.milliseconds2String(calendar.timeInMillis, SimpleDateFormat(format))
    }

    /**
     * 根据出生年月日计算年龄
     * @param birthday
     * @return: int
     */
    fun getAgeByBirth(birthday: String): Int{
        return getAgeByBirth(RxTimeTool.string2Date("yyyy-MM-dd", birthday))
    }

    fun getAgeByBirth(birthday: Date?): Int {

        if (birthday == null){
            return 0
        }

        //获取当前时间
        val cal: Calendar = Calendar.getInstance()

        //获取出生日期的Calendar对象
        val bir: Calendar = Calendar.getInstance()
        bir.time = birthday
        //如果出生日期大于当前日期，则返回0
        if (cal.before(birthday)) {
            return 0
        }
        //取出当前年月日
        val nowYear: Int = cal.get(Calendar.YEAR)
        val nowMonth: Int = cal.get(Calendar.MONTH)
        val nowDay: Int = cal.get(Calendar.DAY_OF_MONTH)

        //取出出生日期的年月日
        val birthYear: Int = bir.get(Calendar.YEAR)
        val birthMonth: Int = bir.get(Calendar.MONTH)
        val birthDay: Int = bir.get(Calendar.DAY_OF_MONTH)

        //计算年份
        var age = nowYear - birthYear

        //计算月份和日，看看是否大于当前月日，如果小于则减去一岁
        if (nowMonth < birthMonth || nowMonth == birthMonth && nowDay < birthDay) {
            age--
        }
        return age
    }


}