export default (configuration) => {
  return `
package ${configuration.packageName}.extensions

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.provider.MediaStore
import android.util.Patterns
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*


fun String.checkIsEmpty(): Boolean =
    isNullOrEmpty() || "" == this || this.equals("null", ignoreCase = true)

fun getDate(date: Date): String {
    return try {
        val simple = SimpleDateFormat("dd MMM yyyy hh:MM", Locale.ENGLISH)
        return simple.format(date)
    } catch (e: Exception) {
        "0"
    }
}

fun EditText.textToString(): String = this.text.toString()

fun EditText.checkIsEmpty(): Boolean {
    return if (text == null || "" == textToString() || text.toString()
            .equals("null", ignoreCase = true)
    ) {
        this.error = "Should not be empty"
        true
    } else false

}

fun EditText.isEmailValid(): Boolean {
    return if (Patterns.EMAIL_ADDRESS.matcher(this.text).matches()) true
    else {
        this.error = "Invalid email"
        false
    }
}

//fun EditText.isValidPhoneNumber(): Boolean = text.matches("^((\\+92)?(0092)?(92)?(0)?)(3)([0-9]{9})\$".toRegex())

fun EditText.validPassword(): Boolean {
    return if (text.length <= 5) {
        error = "Password is too short"
        false
    } else true
}

fun View.showSoftKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    this.requestFocus()
    imm.showSoftInput(this, 0)
}

fun String.formatNumber(): String? {
    var num = this.replace(" ", "")
        .replace("-", "")
        .replace("(", "")
        .replace(")", "")
        .replace("+1", "")
    num = "+1$num"
    return if (num.length == 12) num else null
}

fun Fragment.copyText(text: String, toast: Boolean = true) {
    val clipboard: ClipboardManager? =
        requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
    val clip = ClipData.newPlainText("Account Number", text)
    clipboard?.setPrimaryClip(clip)
    if (toast) Toast.makeText(context, "Copied to clipboard!", Toast.LENGTH_SHORT).show()
}

fun String.getTimeInAgo(): String {
    val date = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH).parse(this)
    val calendar = Calendar.getInstance()
    if (date != null) {
        calendar.time = date
    } else return ""

    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_MONTH)
    val hour = calendar.get(Calendar.HOUR_OF_DAY)
    val minute = calendar.get(Calendar.MINUTE)

    val currentCalendar = Calendar.getInstance()
    val currentYear = currentCalendar.get(Calendar.YEAR)
    val currentMonth = currentCalendar.get(Calendar.MONTH)
    val currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH)
    val currentHour = currentCalendar.get(Calendar.HOUR_OF_DAY)
    val currentMinute = currentCalendar.get(Calendar.MINUTE)

    return if (year < currentYear) {
        val interval = currentYear - year
        if (interval == 1) "$interval year ago" else "$interval years ago"
    } else if (month < currentMonth) {
        val interval = currentMonth - month
        if (interval == 1) "$interval month ago" else "$interval months ago"
    } else if (day < currentDay) {
        val interval = currentDay - day
        if (interval == 1) "$interval day ago" else "$interval days ago"
    } else if (hour < currentHour) {
        val interval = currentHour - hour
        if (interval == 1) "$interval hour ago" else "$interval hours ago"
    } else if (minute < currentMinute) {
        val interval = currentMinute - minute
        if (interval == 1) "$interval minute ago" else "$interval minutes ago"
    } else "a moment ago"

}

fun Double.currentFormat(): String {
    return "$" + String.format("%.2f", this)
}

fun Double.formatDecimal(places: Int = 2): String {
    return String.format("%.\${places}f", this)
}

fun String.appendHttpsIfMissing(): String {
    if (!startsWith("http://") && !startsWith("https://")) {
        return "https://$this"
    }
    return this
}

fun Bitmap.getImageUri(context: Context): Uri {
    val bytes = ByteArrayOutputStream()
    compress(Bitmap.CompressFormat.JPEG, 100, bytes)
    val path = MediaStore.Images.Media.insertImage(
        context.contentResolver,
        this,
        "Image Description",
        null
    )
    return Uri.parse(path)
}

fun String.getBitmapFromUrl(): Bitmap? {
    return try {
        val url = URL(this)
        BitmapFactory.decodeStream(url.openStream())
    } catch (e: IOException) {
        null
    }
}

fun String.convertToLocalTime(): Date? {
    val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
    inputFormat.timeZone = TimeZone.getTimeZone("UTC")
    val outputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
    return inputFormat.parse(this)
}

  `;
};
