package com.remax.visualnovel.extension


import com.remax.visualnovel.utils.TimeUtils
import java.util.Calendar

/**
 * Created by HJW on 2022/7/28
 */

/**
 * 计算年龄
 */
fun Long?.calculateAge(): Int {
    val calendar = Calendar.getInstance()
    val yearNow = calendar.get(Calendar.YEAR)

    val birth = birthDateFormatParse.format(this)
    calendar.time = TimeUtils.getBirthDate(birth)
    val year = yearNow - calendar.get(Calendar.YEAR)

    return year
}

/**
 * 检查年龄是否满18岁
 * @receiver Long
 * @return Boolean
 */
fun Long.checkAge(): Boolean {
    val calendar = Calendar.getInstance()
    val yearNow = calendar.get(Calendar.YEAR)
    val monthNow = calendar.get(Calendar.MONTH)
    val dayNow = calendar.get(Calendar.DATE)
    val birth =birthDateFormatParse.format(this)
    calendar.time = TimeUtils.getBirthDate(birth)
    val year = yearNow - calendar.get(Calendar.YEAR)
    val month = monthNow - calendar.get(Calendar.MONTH)
    val day = dayNow - calendar.get(Calendar.DATE)

    return when {
        year < 18 -> {
            false
        }

        year == 18 -> {
            when {
                month < 0 -> {
                    false
                }

                month == 0 -> {
                    day >= 0
                }

                else -> {
                    true
                }
            }
        }

        else -> {
            true
        }
    }
}