@file:JvmName("ContextUtils")

package com.felipeporge.utility


import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri

/**
 * Starts an activity.
 * @param clazz Activity class.
 */
fun Context.startActivity(clazz: Class<*>): Boolean = fireIntent(Intent(this, clazz))

/**
 * Fires an intent.
 * @param intent    The [Intent] to fire.
 * @return  [Boolean] True (successfully fired) or false (otherwise).
 */
fun Context.fireIntent(intent: Intent): Boolean {
    intent.resolveActivity(packageManager)?.let {
        startActivity(intent)
        return true
    }
    return false
}

/**
 * Opens an URL.
 * @param url   Url to open.
 * @return      False if there is no intent to resolve it.
 */
fun Context.openUrl(url: String): Boolean = openUrl(Uri.parse(url))

/**
 * Opens an URL.
 * @param uri   Url to open.
 * @return      False if there is no intent to resolve it.
 */
fun Context.openUrl(uri: Uri): Boolean = fireIntent(Intent(Intent.ACTION_VIEW, uri))

/**
 * Opens app in Google Play.
 * @return  False if there is no intent to resolve it.
 */
fun Context.openAppInGooglePlay(): Boolean = openUrl("https://play.google.com/store/apps/details?id=" + applicationInfo.packageName)

/**
 * Opens publisher page in Google Play.
 * @param pubName   Publisher name.
 * @return  False if there is no intent to resolve it.
 */
fun Context.openPubAppsInGooglePlay(pubName: String): Boolean {
    val uri = Uri.parse("https://play.google.com/store/search")
            .buildUpon()
            .appendQueryParameter("q", "pub:" + pubName)
            .appendQueryParameter("c", "apps")
            .build()

    return openUrl(uri)
}

/**
 * Opens an intent to send an ic_email.
 * @param destinationEmail  The destination ic_email.
 * @param subject   Email subject.
 * @param body  Email body.
 * @return  False if there is no intent to resolve it.
 */
fun Context.sendEmail(
        destinationEmail: String,
        subject: String,
        body: String): Boolean {

    val intent = Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", destinationEmail, null))
    intent.putExtra(Intent.EXTRA_SUBJECT, subject)
    intent.putExtra(Intent.EXTRA_TEXT, body)
    return fireIntent(intent)
}

/**
 * Opens an intent to make a call.
 * @param phone  Phone number.
 * @return  False if there is no intent to resolve it.
 */
fun Context.openDialer(phone: String): Boolean {
    val intent = Intent(Intent.ACTION_DIAL)
    intent.data = Uri.parse("tel:" + phone)
    return fireIntent(intent)
}

/**
 * Copies a text to clipboard.
 * @param textToCopy    The text to copy.
 * @return  False if there is no clipboard manager ready.
 */
fun Context.copyToClipboard(textToCopy: String): Boolean {
    val clipboard = (getSystemService(Activity.CLIPBOARD_SERVICE) as? ClipboardManager)
    val clip = ClipData.newPlainText(packageName, textToCopy)
    clipboard?.let {
        clipboard.primaryClip = clip
        return true
    }
    return false
}

/**
 * Directions to an specific location.
 * @param latitude - Destination latitude.
 * @param longitude - Destination longitude.
 * @return - False if there is no intent to resolve it.
 */
fun Context.directionsTo(
        latitude: Double,
        longitude: Double): Boolean {

    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=$latitude,$longitude"))
    return fireIntent(intent)
}