package net.pokeranalytics.android.util.extensions

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.net.Uri
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
import net.pokeranalytics.android.BuildConfig
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.util.ModelUtils
import net.pokeranalytics.android.util.URL


// Sizes
val Int.dp: Int
	get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.px: Int
	get() = (this * Resources.getSystem().displayMetrics.density).toInt()
val Float.dp: Float
	get() = (this / Resources.getSystem().displayMetrics.density)
val Float.px: Float
	get() = (this * Resources.getSystem().displayMetrics.density)


// Toast
fun PokerAnalyticsActivity.toast(message: String) {
	Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

fun PokerAnalyticsFragment.toast(message: String) {
	Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
}

// Open Play Store for rating
fun PokerAnalyticsActivity.openPlayStorePage() {
	val uri = Uri.parse("market://details?id=$packageName")
	val goToMarket = Intent(Intent.ACTION_VIEW, uri)
	goToMarket.addFlags(
		Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_NEW_DOCUMENT or
				Intent.FLAG_ACTIVITY_MULTIPLE_TASK
	)
	try {
		startActivity(goToMarket)
	} catch (e: ActivityNotFoundException) {
		startActivity(
			Intent(
				Intent.ACTION_VIEW, Uri.parse(
					"http://play.google.com/store/apps/details?id=$packageName"
				)
			)
		)
	}
}

// Open email for "Contact us"
fun PokerAnalyticsActivity.openContactMail(subjectStringRes: Int) {
	val info = "v${BuildConfig.VERSION_NAME}(${BuildConfig.VERSION_CODE}), Android ${android.os.Build.VERSION.SDK_INT}, ${ModelUtils.getDeviceName()}"
	val intent = Intent(Intent.ACTION_SENDTO)
	intent.data = Uri.parse("mailto:${URL.SUPPORT_EMAIL.value}")
	intent.putExtra(Intent.EXTRA_SUBJECT, getString(subjectStringRes))
	intent.putExtra(Intent.EXTRA_EMAIL, URL.SUPPORT_EMAIL.value)
	intent.putExtra(Intent.EXTRA_TEXT, "\n\n$info")
	startActivity(Intent.createChooser(intent, getString(R.string.contact)))
}

// Open custom tab
fun PokerAnalyticsActivity.openUrl(url: String) {
	val builder: CustomTabsIntent.Builder = CustomTabsIntent.Builder()
	builder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary))
	val customTabsIntent = builder.build()
	customTabsIntent.launchUrl(this, Uri.parse(url))
}

// Display Alert Dialog
fun PokerAnalyticsActivity.showAlertDialog(title: Int? = null, message: Int? = null) {
	showAlertDialog(this, title, message)
}
fun PokerAnalyticsFragment.showAlertDialog(title: Int? = null, message: Int? = null) {
	context?.let {
		showAlertDialog(it, title, message)
	}
}

/**
 * Create and show an alert dialog
 */
fun showAlertDialog(context: Context, title: Int? = null, message: Int? = null) {
	val builder = AlertDialog.Builder(context)
	title?.let {
		builder.setTitle(title)
	}
	message?.let {
		builder.setMessage(message)
	}
	builder.setPositiveButton(R.string.ok, null)
	builder.show()
}