package com.xuqm.sdhbwfu.core.extensions

import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.livinglifetechway.quickpermissions_kotlin.runWithPermissions
import com.livinglifetechway.quickpermissions_kotlin.util.QuickPermissionsOptions
import com.xuqm.base.common.LogHelper
import com.xuqm.base.common.ToolsHelper
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

private class NotNullSingleValue<T> : ReadWriteProperty<Any?, T> {

    private var value: T? = null

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value ?: throw IllegalStateException("not initialized")
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        if (this.value == null) {
            this.value = value
        } else {
            throw IllegalStateException("already initialized")
        }
    }

}

/**
 * 不为空且唯一
 */
fun <T> notNullSingleValue(): ReadWriteProperty<Any?, T> = NotNullSingleValue()


/**
 * 打印log
 */
//fun Any.log(message: CharSequence) {
//    KLog.d(this.javaClass.simpleName, message)
//}

fun Any.log(message: Any) {
    LogHelper.d(this.javaClass.simpleName, message)
}

fun Any.loge(message: Any) {
    LogHelper.e("=====>" + this.javaClass.simpleName, message)
}

fun Any.log() {
    LogHelper.d("=====>" + this.javaClass.simpleName, this)
}

fun Any.loge() {
    LogHelper.e("=====>" + this.javaClass.simpleName, this)
}

fun Any.showMessage(content: CharSequence) {
    ToolsHelper.showMessage(content)
}

fun Any.showMessage() {
    ToolsHelper.showMessage(this.toString())
}

fun Any.toStrings() =
    ToolsHelper.toString(this)

fun Any.toLong() =
    ToolsHelper.toLong(this)
fun Any.toInt() =
    ToolsHelper.toInt(this)

fun Any.isNull() =
    ToolsHelper.isNull(this)


/**
 * 弹出窗口
 * @param title 标题
 * @param message 提示信息
 * @param confirm 点击确认按钮
 * @param cancel 点击取消按钮的事件
 */
fun Activity.showDialog(title: String, message: String, confirm: () -> Unit, cancel: () -> Unit) {
    AlertDialog.Builder(this).setTitle(title)
        .setMessage(message)
        .setPositiveButton(
            "确定"
        ) { _, _ ->
            confirm()
        }
        .setNegativeButton(
            "取消"
        ) { _, _ ->
            cancel()
        }.create().show()
}

/**
 * 弹出窗口---只有一个确认按钮
 * @param title 标题
 * @param message 提示信息
 * @param confirm 点击确认按钮
 */
fun Activity.showDialog(title: String, message: String, confirm: () -> Unit) {
    AlertDialog.Builder(this).setTitle(title)
        .setMessage(message)
        .setPositiveButton(
            "确定"
        ) { _, _ ->
            confirm()
        }.create().show()
}

/**
 * 权限检测--申请
 * @param rationaleMethod 权限被拒绝时候的回调，提供重新申请，打开设置界面等方法
 * @param permanentDeniedMethod 拒绝权限并且选中了不在提示选项的回调，可以指导用户打开设置页面授予权限
 */
fun Context?.runWithPermission(
    vararg permissions: String,
    callback: () -> Unit
): Any? = this.runWithPermissions(
    *permissions
) { callback() }


fun Context?.runWithPermission(
    vararg permissions: String,
    rationaleMethod: ((com.livinglifetechway.quickpermissions_kotlin.util.QuickPermissionsRequest) -> Unit),
    permanentDeniedMethod: ((com.livinglifetechway.quickpermissions_kotlin.util.QuickPermissionsRequest) -> Unit),
    callback: () -> Unit
): Any? {

    val quickPermissionsOption = QuickPermissionsOptions(
        rationaleMethod = rationaleMethod,
        permanentDeniedMethod = permanentDeniedMethod
    )

    return this.runWithPermissions(
        *permissions, options = quickPermissionsOption
    ) { callback() }
}

/*
隐藏软键盘
 */
fun Activity.hideSoftInput() {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(window.decorView.windowToken, 0)
}

fun Fragment.hideSoftInput() {
    activity?.hideSoftInput()
}
