package com.convenient.android.common.utils

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import com.convenient.android.common.R
import com.convenient.android.common.extension.getUri
import java.io.File

/**
 * @classname IntentUtil
 * @author LiuXiaoLong
 * @date 2022-07-19 PM
 * @description 意图相关的跳转工具类
 */
object IntentUtil {


    /**
     * @description 打开浏览器
     * @param context 上下文
     * @param url 需要打开的链接
     * @param isOutside 在新的栈中打开
     */
    fun openBrowser(context: Context, url: String, isOutside: Boolean) {
        try {
            context.startActivity(Intent(Intent.ACTION_VIEW).apply {
                data = Uri.parse(url)
                if (isOutside) {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
            ToastUtil.Companion.showToast(context, R.string.activity_not_found)
        }
    }

    /**
     * 打开手机市场APP
     * @param packageName 包名
     * @param isOutside 是否新的栈中打开
     */
    fun openMarket(context: Context, packageName: String?, isOutside: Boolean) {
        try {
            context.startActivity(Intent(Intent.ACTION_VIEW).apply {
                data = Uri.parse("market://details?id=$packageName")
                if (isOutside) {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
            ToastUtil.Companion.showToast(context, R.string.activity_not_found)
        }
    }

    /**
     * @param ：[context, packageName]
     * @return : void
     * @methodName ：readyToAppOrMarket created by luozhipeng on 5/12/17 13:28.
     * @description ：传入包名，如果应该已安装就打开应用 如果没有安装就进入应用市场
     */
    fun readyToAppOrMarket(context: Context, packageName: String) {
        if (PackageUtil.isInstallSoftware(context, packageName)) {
            var intent: Intent? = context.packageManager.getLaunchIntentForPackage(packageName)
            if (intent != null) {
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED or Intent.FLAG_ACTIVITY_CLEAR_TOP
                context.applicationContext.startActivity(intent)
            }
        } else {
            openMarket(context, packageName, true)
        }
    }



    /**
     * 发送邮件
     * @param email 收件人邮箱
     * @param content 邮件内容
     */
    fun sendEmail(context: Context, email: String, content : String) {
        try {
            context.startActivity(Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:${email}")).apply {
                putExtra(Intent.EXTRA_SUBJECT, content)
                putExtra(Intent.EXTRA_TEXT, "")
            })
        } catch (e: Exception) {
            ToastUtil.Companion.showToast(context, R.string.activity_not_found)
        }
    }


    /**
     * 发送邮件并携带附件
     * @param addressee 收件人
     * @param title 主题
     * @param content 邮件正文
     * @param file 附件
     */
    fun sendEmailWithFile(context: Context, addressee: String, title: String?, content: String?, file: File?, chooserTitle: String = "") {
        try {
            val intent = Intent(Intent.ACTION_SEND).apply {
                type = "plain/text"
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
                putExtra(Intent.EXTRA_EMAIL, arrayOf(addressee))
                putExtra(Intent.EXTRA_SUBJECT, title)
                putExtra(Intent.EXTRA_TEXT, content)
                file?.also { putExtra(Intent.EXTRA_STREAM, it.getUri(context)) }
            }
            context.startActivity(Intent.createChooser(intent, chooserTitle))
        } catch (e: Exception) {
            e.printStackTrace()
            ToastUtil.Companion.showToast(context, R.string.activity_not_found)
        }
    }


    /**
     * 分享文件
     * @param title 分享内容
     * @param type 分享的文件类型
     * @param file 分享的文件
     *
     */
    fun shareFile(context: Context, title: String, type: String, file: File?) {
        try {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = type
            intent.putExtra(Intent.EXTRA_SUBJECT, title)
            intent.putExtra(Intent.EXTRA_STREAM, file?.getUri(context))
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(Intent.createChooser(intent, title))
        } catch (e: ActivityNotFoundException) {
            e.printStackTrace()
            ToastUtil.Companion.showToast(context, R.string.activity_not_found)
        }
    }




    /**
     * @param ：[activity]
     * @return : void
     * @methodName ：openAppSetting created by luozhipeng on 5/12/17 13:32.
     * @description ：跳转到系统设置界面
     */
    fun openAppSetting(activity: Activity) {
        val uri = Uri.fromParts("package", activity.packageName, null)
        activity.startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri))
    }


    /**
     * 打开文件界面
     *
     * @param context
     * @param fileFormat 文件类型
     * @param filePath 要打开的文件路径
     */
    fun openFileActivity(context: Context, fileFormat: String, filePath: String?) {
        try {
            val intent = Intent(Intent.ACTION_VIEW).apply {
                val uri = File(filePath).getUri(context)
                setDataAndType(uri, if ("music" == fileFormat) "audio/*" else "video/*")
            }
            context.startActivity(intent)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 跳转到QQ
     */
    fun openQQ(context: Context){
        try {
            val uri = Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D" + "Yd-l3BU6PltgAh7OcqSFSMEcCNOogFL0")
            context.startActivity(Intent(Intent.ACTION_VIEW, uri).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            })
        }catch (e : Exception){
            e.printStackTrace()
            ToastUtil.showToast(context, R.string.not_found_app)
        }
    }


}