package com.adealink.weparty.push

import android.Manifest
import android.annotation.SuppressLint
import android.os.Build
import androidx.fragment.app.FragmentActivity
import com.adealink.frame.aab.util.getCompatString
import com.adealink.weparty.R
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.permission.PermissionUtils

object NotificationUtil {

    private fun getNotificationSmallIcon(): Int {
        return if (Build.VERSION.SDK_INT < 21) {
            R.drawable.ic_launcher
        } else {
            R.drawable.notification_ic
        }
    }

    fun isNotificationPermissionGranted(activity: FragmentActivity): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            return true
        }

        if (PermissionUtils.hasPermissions(activity, Manifest.permission.POST_NOTIFICATIONS)) {
            return true
        }
        return false
    }

    @SuppressLint("CheckResult")
    fun requestPostNotificationPermission(activity: FragmentActivity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            return
        }

        if (PermissionUtils.hasPermissions(activity, Manifest.permission.POST_NOTIFICATIONS)) {
            return
        }

        PermissionUtils.getRxPermissions(activity)
            .request(Manifest.permission.POST_NOTIFICATIONS).subscribe { granted ->
                if (!granted) {
                    CommonDialog.Builder()
                        .message(getCompatString(R.string.common_open_notification_tip))
                        .onPositive {
                            PermissionUtils.goNotificationSettingActivity(activity)
                        }
                        .positiveText(getCompatString(R.string.common_open))
                        .setShowDefaultCancel(true)
                        .dismissAfterClick(true)
                        .build()
                        .show(activity.supportFragmentManager, "POST_NOTIFICATIONS_PERMISSION_NO_GRANT")
                }
            }
    }
}