package com.remax.visualnovel.utils

import android.content.Context
import com.remax.visualnovel.R
import com.remax.visualnovel.app.base.app.CommonApplicationProxy
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

/**
 * Created by HJW on 2025/7/11
 */
object TimeUtils {

    private val context by lazy {
        CommonApplicationProxy.application
    }

    const val ONE_SECOND: Long = 1000


    const val ONE_MINUTE: Long = (60 * 1000).toLong()


    const val ONE_HOUR: Long = 60 * ONE_MINUTE


    const val ONE_DAY: Long = 24 * ONE_HOUR


    const val ONE_WEEK: Long = 7 * ONE_DAY


    const val ONE_MONTH: Long = 30 * ONE_DAY

    const val ONE_YEAR: Long = 365 * ONE_DAY

    const val YM_PATTERN: String = "yyyy-MM"
    const val YMD_PATTERN: String = "yyyy-MM-dd"
    const val YMD_PATTERN_2: String = "yyyyMMdd"
    const val YMDHM_PATTERN: String = "yyyy-MM-dd HH:mm"

    fun getBirthDateString(context: Context, datetime: String, needYear: Boolean): String {
        try {
            val formatter = SimpleDateFormat(YMD_PATTERN, Locale.getDefault())
            val date = formatter.parse(datetime)
            val timeMillis = date?.time ?: 0

            val formatter2 = if (TimeUtils.isThisYear(timeMillis) && !needYear)
                SimpleDateFormat(context.getString(R.string.time_format_m_d), Locale.getDefault())
            else
                SimpleDateFormat(context.getString(R.string.time_format_y_m_d), Locale.getDefault())

            return if (date != null) formatter2.format(date) else ""
        } catch (e: ParseException) {
            return datetime
        }
    }

    fun getBirthDate(datetime: String): Date {
        try {
            val formatter = SimpleDateFormat(YMD_PATTERN, Locale.getDefault())
            val date = formatter.parse(datetime)
            return date ?: Date()
        } catch (e: ParseException) {
            return Date()
        }
    }

    /**
     * 消息时间：
     * 今天 hh：mm  AM/PM
     * 昨天 YDA hh：mm  AM/PM
     * 之前 MM-dd hh：mm  AM/PM
     */
    fun formatMsgTime(time: Long): String? {
        var txtPreRes = 0
        val patternRes = when {
            System.currentTimeMillis() - time < ONE_MINUTE -> {
                txtPreRes = R.string.time_just_now
                0
            }

            //不在一年以内
            !isThisYear(time) -> {
                R.string.time_format_y_m_d_h_m
            }

            isToday(time) -> {
                R.string.time_format_h_m
            }

            isYesterday(time) -> {
                txtPreRes = R.string.yesterday
                R.string.time_format_h_m
            }

            else -> {
                R.string.time_format_m_d_h_m
            }
        }

        val txtPre = if (txtPreRes != 0) "${context.getString(txtPreRes)} " else ""
        val txtTime = if (patternRes != 0) {
            SimpleDateFormat(context.getString(patternRes), Locale.getDefault()).format(Date(time))
        } else ""

        return txtPre + txtTime
    }

    /**
     * 是否是昨天
     */
    fun isYesterday(time: Long): Boolean {
        val pre = Calendar.getInstance()
        val predate = Date(System.currentTimeMillis())
        pre.setTime(predate)

        val cal = Calendar.getInstance()
        val date = Date(time)
        cal.setTime(date)

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            val diffDay = (cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR))
            return diffDay == -1
        }
        return false
    }

    /**
     * 是否是同一天
     */
    fun isSameDay(firstTime: Long, secondTime: Long): Boolean {
        val pre = Calendar.getInstance()
        val predate = Date(firstTime)
        pre.setTime(predate)

        val cal = Calendar.getInstance()
        val date = Date(secondTime)
        cal.setTime(date)

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            val diffDay = (cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR))

            return diffDay == 0
        }
        return false
    }

    /**
     * 是否是今天
     */
    fun isToday(time: Long): Boolean {
        val pre = Calendar.getInstance()
        val predate = Date(System.currentTimeMillis())
        pre.setTime(predate)

        val cal = Calendar.getInstance()
        val date = Date(time)
        cal.setTime(date)

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            val diffDay = (cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR))

            return diffDay == 0
        }
        return false
    }

    /**
     * 当前时间加1年
     * 是否是今年
     */
    fun isThisYear(time: Long): Boolean {
        val date = Date()
        val sdf = SimpleDateFormat(YMDHM_PATTERN, Locale.getDefault())
        val now = sdf.format(date)
        val handleTime = sdf.format(Date(time))
        return now.substring(0, 4) == handleTime.substring(0, 4)
    }

    fun format_y_m_d_h_m(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_y_m_d_h_m), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_m_d_h_m_s(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_m_d_h_m_s), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_h_m_a(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_h_m), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_y_m_d_h_m_s(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_y_m_d_h_m_s), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_y_m_d(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_y_m_d), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_y_m(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_y_m), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_m_d(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_m_d), Locale.getDefault())
        return format.format(Date(date))
    }

    fun format_y(date: Long): String {
        val format = SimpleDateFormat(context.getString(R.string.time_format_y), Locale.getDefault())
        return format.format(Date(date))
    }

}