package com.swago.baseswago.util

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.fragment.app.FragmentActivity
import com.facebook.share.model.ShareLinkContent
import com.facebook.share.widget.ShareDialog
import com.google.android.gms.common.GoogleApiAvailability

/**
 *@date 2022/3/29 21:25
 *description:
 */
object SwagoShareUtils {

    val FACEBOOK = "com.facebook.katana"
    val GOOGLE = "google"
    val LINE = "jp.naver.line.android"
    val WHATSAPP = "com.whatsapp"

    fun isInstall(activity: Activity, platform:String):Boolean{
        if (platform == GOOGLE){
            val available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
            return available == 0
        }
        val packageManager = activity.packageManager
        val info = packageManager.getInstalledPackages(0)
        if (info.isEmpty()){
            return false
        }
        info.forEach {
            if (platform == it.packageName){
                return true
            }
        }
        return false
    }

    fun shareToLINE(shareUrl:String,context: Context){
        val component = ComponentName("jp.naver.line.android", "jp.naver.line.android.activity.selectchat.SelectChatActivityLaunchActivity")
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "text/plain"
        shareIntent.putExtra(Intent.EXTRA_TEXT,shareUrl)
        shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Share to")
        shareIntent.component = component
        context.startActivity(Intent.createChooser(shareIntent,""))
    }

    fun shareToWhatsApp(url:String,context: Context){
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.setPackage("com.whatsapp")
        shareIntent.type = "text/plain"
        shareIntent.putExtra(Intent.EXTRA_TEXT,url)
        context.startActivity(Intent.createChooser(shareIntent,"Share to"))
    }
    
    fun shareToFacebook(url:String,activity: FragmentActivity?){
        activity?.let {
            val shareDialog = ShareDialog(it)
            if (ShareDialog.canShow(ShareLinkContent::class.java)){
                val linkeContent = ShareLinkContent.Builder()
                    .setContentUrl(Uri.parse(url))
                    .build()
                shareDialog.show(linkeContent)
            }
        }
    }
}