package com.wdkl.ncs.android.lib.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 *  事件处理器
 */
object TimeHandle {

    fun getCurrentDay24Mill() :Long{
        val cal = Calendar.getInstance()
        cal.set(Calendar.HOUR_OF_DAY, 24)
        cal.set(Calendar.SECOND, 0)
        cal.set(Calendar.MINUTE, 0)
        cal.set(Calendar.MILLISECOND, 0)
        return cal.timeInMillis
    }

    /**
     * @author LDD
     * @From   TimeEngine
     * @Date   2018/5/10 上午9:20
     * @Note   获取小时
     * @param  time 总时间
     */
    fun getHour(time: Int): Int {
        return time % (60 * 60 * 24) / (60 * 60)
    }

    /**
     * @author LDD
     * @From   TimeEngine
     * @Date   2018/5/10 上午9:21
     * @Note   获取分钟
     * @param  time 总时间
     */
    fun getMinute(time: Int): Int {
        return time % (60 * 60 * 24) % (60 * 60) / 60
    }

    /**
     * @author LDD
     * @From   TimeEngine
     * @Date   2018/5/10 上午9:21
     * @Note   获取秒
     * @param  time 总时长
     */
    fun getSecond(time: Int): Int {
        return time % (60 * 60 * 24) % (60 * 60) % 60
    }

    /**
     * @author LDD
     * @From   TimeEngine
     * @Date   2018/5/10 上午9:22
     * @Note   获取天数
     * @param  time 总时长
     */
    fun getDay(time: Int): Int {
        return time / (60 * 60 * 24)
    }

    //按指定格式获取当前日期时间
    fun getDateTime(format: String): String {
        val date = Date(System.currentTimeMillis())
        val sdf = SimpleDateFormat(format)
        sdf.timeZone = TimeZone.getTimeZone("GMT+8")
        return sdf.format(date)
    }

    //按指定格式获取指定日期时间
    fun getDateTime(timeMillis: Long, format: String): String {
        if (timeMillis > 0) {
            val date = Date(timeMillis)
            val sdf = SimpleDateFormat(format)
            sdf.timeZone = TimeZone.getTimeZone("GMT+8")
            return sdf.format(date)
        }
        return "null"
    }

    /**
     * 将时间转换为时间戳
     */
    fun dateToStamp(s: String, pattern: String): Long {
        val simpleDateFormat = SimpleDateFormat(pattern)
        var date: Date? = null
        try {
            simpleDateFormat.timeZone = TimeZone.getTimeZone("GMT+8")
            date = simpleDateFormat.parse(s)

            return date!!.time
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return 0
    }
}