package com.zxhl.cms.utils

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import androidx.core.content.FileProvider
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.Constant
import java.io.File

/**
 * Created by gaoleichao on 2018/8/29.
 */
object OpenFiles {

    //android获取一个用于打开HTML文件的intent
    fun getHtmlFileIntent(file: File): Intent {
        val uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build()
        val intent = Intent("android.intent.action.VIEW")
        intent.setDataAndType(uri, "text/html")
        return intent
    }

    //android获取一个用于打开图片文件的intent
    fun getImageFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "image/*")
        return intent
    }

    //android获取一个用于打开PDF文件的intent
    fun getPdfFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "application/pdf")
        return intent
    }

    //android获取一个用于打开文本文件的intent
    fun getTextFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "text/plain")
        return intent
    }


    //android获取一个用于打开音频文件的intent
    fun getAudioFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.putExtra("oneshot", 0)
        intent.putExtra("configchange", 0)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "audio/*")
        return intent
    }

    //android获取一个用于打开视频文件的intent
    fun getVideoFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.putExtra("oneshot", 0)
        intent.putExtra("configchange", 0)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "video/*")
        return intent
    }

    //android获取一个用于打开CHM文件的intent
    fun getChmFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "application/x-chm")
        return intent
    }


    //android获取一个用于打开Word文件的intent
    fun getWordFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "application/msword")
        return intent
    }

    //android获取一个用于打开Excel文件的intent
    fun getExcelFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "application/vnd.ms-excel")
        return intent
    }

    //android获取一个用于打开PPT文件的intent
    fun getPPTFileIntent(file: File): Intent {
        val intent = Intent("android.intent.action.VIEW")
        intent.addCategory("android.intent.category.DEFAULT")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint")
        return intent
    }

    //android获取一个用于打开apk文件的intent
    fun getApkFileIntent(file: File): Intent {
        val intent = Intent()
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.action = Intent.ACTION_VIEW
        var uri: Uri
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//24
            uri = FileProvider.getUriForFile(AppContext.get(), Constant.Param.mPacketName + ".fileprovider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        } else {
            uri = Uri.fromFile(file)
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive")
        return intent
    }

    private fun checkEndsWithInStringArray(checkItsEnd: String,
                                           fileEndings: Array<String>): Boolean {
        for (aEnd in fileEndings) {
            if (checkItsEnd.endsWith(aEnd))
                return true
        }
        return false
    }

    fun openFile(path: String) {
        if (TextUtils.isEmpty(path)) {
            Utils.showToast(AppContext.get(), "文件不存在")
            return
        }
        val currentPath: File? = File(path)
        if (currentPath != null && currentPath.isFile) {
            val fileName = currentPath.toString()
            val intent: Intent
            if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingImage))) {
                intent = OpenFiles.getImageFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingWebText))) {
                intent = OpenFiles.getHtmlFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingPackage))) {
                intent = OpenFiles.getApkFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingAudio))) {
                intent = OpenFiles.getAudioFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingVideo))) {
                intent = OpenFiles.getVideoFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingText))) {
                intent = OpenFiles.getTextFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingPdf))) {
                intent = OpenFiles.getPdfFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingWord))) {
                intent = OpenFiles.getWordFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingExcel))) {
                intent = OpenFiles.getExcelFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else if (checkEndsWithInStringArray(fileName, AppContext.get().resources.getStringArray(R.array.fileEndingPPT))) {
                intent = OpenFiles.getPPTFileIntent(currentPath)
                AppContext.get().startActivity(intent)
            } else {
                Utils.showToast(AppContext.get(), "无法打开，请安装相应的软件！")
            }
        } else {
            Utils.showToast(AppContext.get(), "对不起，这不是文件")
        }
    }

}