package cn.org.bjca.trust.push.extensions
import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
import androidx.annotation.NonNull
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import cn.org.bjca.trust.push.BuildConfig
import cn.org.bjca.trust.push.api.HttpResult
import cn.org.bjca.trust.push.common.LogHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch


fun Any.log(message: Any) {
    LogHelper.d(message)
}

fun Any.loge(message: Any) {
    LogHelper.e(message)
}

fun Any.log() {
    LogHelper.d(this)
}

fun Any.loge() {
    LogHelper.e(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()
}


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

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

fun Context.putInt(key: String, value: Int) {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE) ?: return
    with(sharedPref.edit()) {
        putInt(key, value)
        apply()
    }
}

fun Context.getIntForPreferences(key: String): Int {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE)
    return sharedPref?.getInt(key, -1) ?: -1
}

fun Context.getIntForPreferences(key: String, @NonNull defValue: Int): Int {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE)
    return sharedPref?.getInt(key, defValue) ?: defValue
}

fun Context.putLong(key: String, value: Long) {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE) ?: return
    with(sharedPref.edit()) {
        putLong(key, value)
        apply()
    }
}

fun Context.getLongForPreferences(key: String): Long {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE)
    return sharedPref?.getLong(key, -1) ?: -1
}

fun Context.getLongForPreferences(key: String, @NonNull defValue: Long): Long {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE)
    return sharedPref?.getLong(key, defValue) ?: defValue
}

fun Context.putString(key: String, value: String) {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE) ?: return
    with(sharedPref.edit()) {
        putString(key, value)
        apply()
    }
}

fun Context.getStringForPreferences(key: String): String {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE)
    return sharedPref?.getString(key, "") ?: ""
}

fun Context.getStringForPreferences(key: String, @NonNull defValue: String): String {
    val sharedPref =
        getSharedPreferences(BuildConfig.versionName, Context.MODE_PRIVATE)
    return sharedPref?.getString(key, defValue) ?: defValue
}

/**
 *  http 请求扩展
 */
fun <T> CoroutineScope.http(
    start: () -> Unit = {},
    request: suspend CoroutineScope.() -> HttpResult<T>,
    resp: (T?) -> Unit,
    err: (String) -> Unit = {},
    end: () -> Unit = {}
): Job {
    return launch {
        try {
            start()
            val data = request()
            if (data.status == "200") {
                resp(data.data)
            } else {
                err(data.msg)
            }
        } catch (e: Exception) {
            err(e.message ?: "")  //可根据具体异常显示具体错误提示
            LogHelper.e(e, "CoroutineScope::")
        } finally {
            end()
        }
    }
}

