package com.base.scanqr.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.core.content.FileProvider
import com.base.scanqr.bean.EmailUIBean
import java.io.File

object IntentUtils {

    fun intentAppDetailsSettings(context: Context): Intent {
        val intent: Intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        val uri = Uri.fromParts("package", context.packageName, null)
        intent.setData(uri)
        return intent
    }


    fun intentNotificationSettings(context: Context): Intent {
        val intent = Intent()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
        } else {
            intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
            intent.putExtra("app_package", context.packageName)
        }
        return intent
    }

    fun intentShareImage(context: Context, imagePath: String): Intent {
        kotlin.runCatching {
            // 获取图片文件的URI
            val imageUri: Uri = FileProvider.getUriForFile(context, context.packageName + ".provider", File(imagePath))
            val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
            shareIntent.type = "image/jpeg"
            return shareIntent
        }
        return Intent()
    }

    fun intentShareText(text: String): Intent {
        val sendIntent = Intent()
        sendIntent.action = Intent.ACTION_SEND
        sendIntent.putExtra(Intent.EXTRA_TEXT, text) // 添加要分享的文本
        sendIntent.type = "text/plain" // 设置数据类型为纯文本

        // 启动Intent
        val shareIntent = Intent.createChooser(sendIntent, null)
        return shareIntent
    }


    fun intentSafPickImage(): Intent {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.setType("image/*")
        return intent
    }

    fun intentWifiSettings(): Intent {
        val intent = Intent()
        intent.setAction(Settings.ACTION_WIFI_SETTINGS)
        return intent
    }

    fun intentSendEmail(emailUIBean: EmailUIBean): Intent {
        val emailIntent = Intent(Intent.ACTION_SEND)
        emailIntent.setType("text/plain")
        emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(emailUIBean.address)) // 替换为收件人邮箱
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailUIBean.subject) // 替换为邮件主题
        emailIntent.putExtra(Intent.EXTRA_TEXT, emailUIBean.message) // 替换为邮件内容
        return Intent.createChooser(emailIntent, "Select your mail client")
    }

    fun intentViewHttp(url: String): Intent {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setData(Uri.parse(url))
        return intent
    }
}