package com.base.pdfviewerscannerwhite.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.content.ContextCompat.startActivity
import androidx.core.content.FileProvider
import com.base.pdfviewerscannerwhite.bean.DocumentBean
import com.base.pdfviewerscannerwhite.bean.DocumentBean.Companion.TYPE_PDF
import java.io.File


object IntentShareUtils {

    fun Context.documentShare(documentBean: DocumentBean) {
        val uri = FileProvider.getUriForFile(
            this, this.packageName + ".provider", File(documentBean.path)
        )
        var intent: Intent? = null
        var desc = "Share PDF"
        if (documentBean.type == DocumentBean.TYPE_PPT) {
            intent = sharePptIntent(uri)
            desc = "Share PPT"
        }
        if (documentBean.type == DocumentBean.TYPE_WORD) {
            intent = shareWordIntent(uri)
            desc = "Share Word"
        }
        if (documentBean.type == DocumentBean.TYPE_EXCEL) {
            intent = shareExcelIntent(uri)
            desc = "Share Excel"
        }
        intent?.let { startActivity(Intent.createChooser(it, desc)) }
    }

    fun sharePdfIntent(uri: Uri): Intent {
        val shareIntent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_STREAM, uri)
            type = "application/pdf"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 授权临时权限
        }
        return shareIntent
    }

    fun sharePptIntent(uri: Uri): Intent {
        val shareIntent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_STREAM, uri)
            type = "application/vnd.ms-powerpoint"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 授权临时权限
        }
        return shareIntent
    }


    fun shareWordIntent(uri: Uri): Intent {
        val shareIntent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_STREAM, uri)
            type = "application/msword"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 授权临时权限
        }
        return shareIntent
    }

    fun shareExcelIntent(uri: Uri): Intent {
        val shareIntent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_STREAM, uri)
            type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 授权临时权限
        }
        return shareIntent
    }


    fun sharePdfPrintIntent(uri: Uri): Intent {
        // 创建打印的 Intent
        val intent = Intent(Intent.ACTION_SEND)
        intent.setDataAndType(uri, "application/pdf")

        // 启动打印服务
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        return intent
    }


    fun Context.shareMutDocuments(
        type: String, uris: ArrayList<Uri>
    ) {
        var desc = "Share PDF files"
        val intent = when (type) {
            TYPE_PDF -> {
                shareMutPdfIntent(uris)
            }

            else -> shareMutPdfIntent(uris)
        }
        val chooserIntent = Intent.createChooser(intent, desc)
        startActivity(chooserIntent)
    }

    fun shareMutPdfIntent(uris: ArrayList<Uri>): Intent {
        val shareIntent = Intent().apply {
            action = Intent.ACTION_SEND_MULTIPLE
            type = "application/pdf"
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 授权临时权限
            putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
        }
        return shareIntent
    }


}