package com.rgt.superr.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.ContactsContract
import android.provider.Settings
import androidx.core.content.FileProvider
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
    }

//    fun intentAddCalendar(eventUIBean: EventUIBean): Intent {
//        val intent = Intent(Intent.ACTION_INSERT)
//        intent.setData(CalendarContract.Events.CONTENT_URI)
//        // 设置事件标题
//        intent.putExtra(CalendarContract.Events.TITLE, eventUIBean.summary)
//
//        val startCalendar = GregorianCalendar()
//        startCalendar.timeInMillis = eventUIBean.startTime
//        startCalendar.setTimeZone(TimeZone.getTimeZone("UTC")) // 设置时区为UTC
//        intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startCalendar.timeInMillis)
//
//        val endCalendar = GregorianCalendar()
//        endCalendar.timeInMillis = eventUIBean.endTime
//        endCalendar.setTimeZone(TimeZone.getTimeZone("UTC")) // 设置时区为UTC
//        intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endCalendar.timeInMillis)
//
//        intent.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY)
//        intent.putExtra(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
//        // 设置事件描述（可选）
//        intent.putExtra(CalendarContract.Events.DESCRIPTION, eventUIBean.description)
//        // 设置事件地点
//        intent.putExtra(CalendarContract.Events.EVENT_LOCATION, eventUIBean.location)
//        return intent
//    }


    fun intentPickContacts(): Intent {
        val intent = Intent(Intent.ACTION_PICK)
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE)
        val chooser = Intent.createChooser(intent, null)
        return chooser
    }

    fun intentCallPhone(tel: String): Intent {
        val callIntent = Intent(Intent.ACTION_CALL, Uri.parse("tel:$tel"))
        val shareIntent = Intent.createChooser(callIntent, null)
        return shareIntent
    }

//    fun intentWriteContract(bean: ContractUIBean): Intent {
//        val intent = Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI)
//        intent.putExtra(ContactsContract.Intents.Insert.NAME, bean.n)
//        intent.putExtra(ContactsContract.Intents.Insert.PHONE, bean.tel)
//        intent.putExtra(ContactsContract.Intents.Insert.EMAIL, bean.email)
//        //竞品拿个公司地址，受不了
//        intent.putExtra(ContactsContract.Intents.Insert.COMPANY, bean.adr)
////        val values = ContentValues().apply {
////            put(
////                ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
////                ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER
////            )
////            put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, bean.adr)
////        }
////        val dataArrayList = ArrayList<ContentValues>()
////        dataArrayList.add(values)
////
////        intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA,dataArrayList)
//
//        val shareIntent = Intent.createChooser(intent, null)
//        return shareIntent
//    }

//    @SuppressLint("IntentReset")
//    fun intentSendSms(bean: MessageUIBean): Intent {
//        val smsIntent = Intent(Intent.ACTION_VIEW)
//        smsIntent.setType("vnd.android-dir/mms-sms")
//        smsIntent.setData(Uri.parse("smsto:" + bean.smsto))
//        smsIntent.putExtra("sms_body", bean.msg)
//        val chooserIntent = Intent.createChooser(smsIntent, null)
//        return chooserIntent
//    }
}