package com.convenient.android.common.utils.date

import java.util.*

/**
 * @author: hubowen
 * @date: 2022/8/3
 * @description:
 */

/**
 * @param: format 时间格式
 * @description: 时间戳转换成默认时间格式
 */
fun Long.toDate(format: String): String = DateUtils.getFormatDate(this, format)

/**
 * @param: singleDateFormat 单独时间格式
 * @description: 获得当前时间信息
 */
fun getSingleDate(singleDateFormat: DateUtils.SingleDateFormat): Int {
    Calendar.getInstance().apply {
        return when (singleDateFormat) {
            DateUtils.SingleDateFormat.YEAR -> get(Calendar.YEAR)
            DateUtils.SingleDateFormat.MONTH -> get(Calendar.MONTH) + 1
            DateUtils.SingleDateFormat.DAY -> get(Calendar.DAY_OF_MONTH)
            DateUtils.SingleDateFormat.HOUR -> get(Calendar.HOUR_OF_DAY)
            DateUtils.SingleDateFormat.MINUTE -> get(Calendar.MINUTE)
            DateUtils.SingleDateFormat.SECOND -> get(Calendar.SECOND)
            DateUtils.SingleDateFormat.WEEK -> get(Calendar.DAY_OF_WEEK) - 1
        }
    }
}

/**
 * @param: endStr 结尾字符
 * @description: 获得当前年份
 */
fun year(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.YEAR)}$endStr"

/**
 * @param: endStr 结尾字符
 * @param: isAbbreviation 是否简写
 * @description: 获得当前月份
 */
fun month(endStr: String? = "", isAbbreviation: Boolean = false): String {
    fun getAbbreviation(month: Int): String {
        return when (month) {
            1 -> "Jan"
            2 -> "Feb"
            3 -> "Mar"
            4 -> "Apr"
            5 -> "May"
            6 -> "Jun"
            7 -> "Jul"
            8 -> "Aug"
            9 -> "Sept"
            10 -> "Oct"
            11 -> "Nov"
            else -> "Dec"
        }
    }

    return when (isAbbreviation) {
        true -> "${getAbbreviation(getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MONTH))}$endStr"
        else -> "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MONTH)}$endStr"
    }
}

/**
 * @param: endStr 结尾字符
 * @description: 获得当前日期
 */
fun day(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.DAY)}$endStr"

/**
 * @param: endStr 结尾字符
 * @description: 获得当前小时
 */
fun hour(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.HOUR)}$endStr"

/**
 * @param: endStr 结尾字符
 * @description: 获得当前分钟
 */
fun minute(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MINUTE)}$endStr"

/**
 * @param: endStr 结尾字符
 * @description: 获得当前秒数
 */
fun second(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.SECOND)}$endStr"

/**
 * @param: endStr 结尾字符
 * @param: isAbbreviation 是否简写
 * @description: 获得当前星期
 */
fun week(endStr: String? = "", isAbbreviation: Boolean = false): String {
    fun getAbbreviation(week: Int): String {
        return when (week) {
            1 -> "Mon"
            2 -> "Tues"
            3 -> "Wed"
            4 -> "Thur"
            5 -> "Fri"
            6 -> "Sat"
            else -> "Sun"
        }
    }

    return when (isAbbreviation) {
        true -> "${getAbbreviation(getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.WEEK))}$endStr"
        else -> "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.WEEK)}$endStr"
    }
}
