package com.duben.microtribe.utils

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Context
import android.widget.EditText
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit

object TimeRender {
    /**
     * 默认日期格式
     */
    var DEFAULT_FORMAT = "yyyy-MM-dd"
    var DEFAULT_FORMAT_MM = "yyyyMMdd"
    var DEFAULT_FORMAT_DAY = "dd"
    var DEFAULT_FORMAT_MONTH_DAY = "MM月dd日"
    var DEFAULT_FORMAT_LINE = "yyyy/MM/dd"
    var DEFAULT_FORMAT_TIME = "HH:mm:ss"
    var DEFAULT_FORMAT_DOT = "yyyy.MM.dd"

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

    /**
     * 年月日时分秒
     */
    var ALL_FORMAT = "yyyyMMddHHmmss"

    /**
     * time
     */
    var TIME_FORMAT = "yyyyMMdd_HHmmssSS"

    /**
     * 显示年月日
     */
    var DEFAULT_FORMAT_SHOW = "yyyy年MM月dd日"
    private var formatBuilder: SimpleDateFormat? = null
    fun getDate(format: String?): String {
        formatBuilder = SimpleDateFormat(format)
        return formatBuilder!!.format(Date())
    }

    val date: String
        get() = getDate(ALL_FORMAT_LINE)

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    fun formatDate(date: Date?): String {
        val f = SimpleDateFormat(DEFAULT_FORMAT)
        return f.format(date)
    }

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    fun formatTime(date: Date?): String {
        val f = SimpleDateFormat(TIME_FORMAT)
        return f.format(date)
    }

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    fun formatDay(date: Date?): String {
        val f = SimpleDateFormat(DEFAULT_FORMAT_DAY)
        return f.format(date)
    }

    /**
     * 格式化日期
     *
     * @param date 日期对象
     * @return String 日期字符串
     */
    fun formatMonthDay(date: Date?): String {
        val f = SimpleDateFormat(DEFAULT_FORMAT_MONTH_DAY)
        return f.format(date)
    }

    /**
     * 格式化日期
     *
     * @param date   日期对象
     * @param format 日期格式
     * @return
     */
    fun formatDate(date: Date?, format: String?): String {
        val f = SimpleDateFormat(format)
        return f.format(date)
    }

    fun formatDate(time: Long, format: String?): String {
        val f = SimpleDateFormat(format)
        return f.format(Date(time * 1000))
    }

    /**
     * string 获取时间data
     *
     * @param dates
     * @param format
     * @return
     */
    @Throws(ParseException::class)
    fun parseStringDate(dates: String?, format: String?): Date {
        val f = SimpleDateFormat(format)
        return f.parse(dates)
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    fun getCurrYearFirst(year: Int): Date {
        val calendar = Calendar.getInstance()
        calendar.clear()
        calendar[Calendar.YEAR] = year
        return calendar.time
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    fun getCurrYearLast(year: Int): Date {
        val calendar = Calendar.getInstance()
        calendar.clear()
        calendar[Calendar.YEAR] = year
        calendar.roll(Calendar.DAY_OF_YEAR, -1)
        return calendar.time
    }

    /**
     * 获取当前月的第一天
     *
     * @return string
     */
    val monthStart: String
        get() {
            val calendar: Calendar = GregorianCalendar()
            calendar[Calendar.DAY_OF_MONTH] = 1
            return formatDate(calendar.time)
        }

    /**
     * 获取当前月的最后一天
     *
     * @return string
     */
    val monthEnd: String
        get() {
            val calendar: Calendar = GregorianCalendar()
            calendar.add(Calendar.MONTH, 1)
            calendar[Calendar.DAY_OF_MONTH] = 0
            return formatDate(calendar.time)
        }

    /**
     * 获取当天的日期
     *
     * @return string
     */
    val dayNew: String
        get() {
            val calendar = Calendar.getInstance()
            return formatDate(calendar.time)
        }

    private var nqMonth: String? = null
    private var nqDay: String? = null
    fun GetTime(context: Context?, data: String, editText: EditText) {
        val split = data.split("-").toTypedArray()
        val yearZ = split[0].toInt()
        val monthZ = split[1].toInt() - 1
        val dayZ = split[2].toInt()
        val datePickerDialog =
            DatePickerDialog(context!!, OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
                nqMonth = if (monthOfYear + 1 < 10) {
                    "0" + (monthOfYear + 1)
                } else {
                    (monthOfYear + 1).toString()
                }
                nqDay = if (dayOfMonth < 10) {
                    "0$dayOfMonth"
                } else {
                    dayOfMonth.toString()
                }
                editText.setText("$year-$nqMonth-$nqDay")
            }, yearZ, monthZ, dayZ)
        datePickerDialog.show()
    }

    /**
     * 判断两个时间间的间隔天数
     */
    fun dateCompare(s1: String?, s2: String?): Int {
        // 设定时间的模板
        formatBuilder = SimpleDateFormat(DEFAULT_FORMAT)
        // 得到指定模范的时间
        var d1: Date? = null
        var d2: Date? = null
        try {
            d1 = formatBuilder!!.parse(s1)
            d2 = formatBuilder!!.parse(s2)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        val abs = (d2!!.time - d1!!.time) / (24 * 3600 * 1000)
        return abs.toInt()
    }

    /**
     * 时间格式化
     *
     * @param _ms
     * @return
     */
    fun ms2HMS(_ms: Int): String {
        var _ms = _ms
        val HMStime: String
        _ms /= 1000
        val hour = _ms / 3600
        val mint = _ms % 3600 / 60
        val sed = _ms % 60
        var hourStr = hour.toString()
        if (hour < 10) {
            hourStr = "0$hourStr"
        }
        var mintStr = mint.toString()
        if (mint < 10) {
            mintStr = "0$mintStr"
        }
        var sedStr = sed.toString()
        if (sed < 10) {
            sedStr = "0$sedStr"
        }
        HMStime = "$hourStr:$mintStr:$sedStr"
        return HMStime
    }

    /**
     * 时间戳转换成时间格式
     *
     * @param duration
     * @return
     */
    fun formatDurationTime(duration: Long): String? {
        return String.format(
            Locale.getDefault(), "%02d:%02d",
            TimeUnit.MILLISECONDS.toMinutes(duration), TimeUnit.MILLISECONDS.toSeconds(duration)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
        )
    }

    /**
     * 是否超过outMin分钟
     *
     * @param adPreLoadTime 广告预加载时间
     * @return true-超过outMin分钟
     */
    fun isOverspedMin2(adPreLoadTime: Long, outMin: Int): Boolean {
        if (adPreLoadTime == 0L) return true
        val time = (System.currentTimeMillis() - adPreLoadTime) / (1000 * 60)
        return time > outMin
    }

    fun formatDateDot(date: Date?): String {
        val f = SimpleDateFormat(DEFAULT_FORMAT_DOT)
        return f.format(date)
    }

    fun getDateAfter(date: Date, day: Int): String {
        val now = Calendar.getInstance()
        now.time = date
        now[Calendar.DATE] = now[Calendar.DATE] + day
        return formatDateDot(now.time)
    }

    /**
     * 是否超过outMin分钟
     *
     * @param adPreLoadTime 广告预加载时间
     * @return true-超过outMin分钟
     */
    fun isOverspedMin(adPreLoadTime: Long, outMin: Int): Boolean {
        val time = (System.currentTimeMillis() - adPreLoadTime) / (1000 * 60)
        //       LogUtil.d("PreCsjGroMoreVideoAdManager","adPreLoadTime="+adPreLoadTime+"time="+time);
        return time > outMin && adPreLoadTime > 0
    }

}