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

import ${configuration.name}.R
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.DownloadManager
import android.app.PendingIntent
import android.app.TaskStackBuilder
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Environment
import android.os.Build
import android.provider.Settings
import android.view.View
import androidx.fragment.app.Fragment
import com.saksham.customloadingdialog.CustomLoader
import java.net.URL
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.math.*
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.content.ContextCompat
import ${configuration.packageName}.MainActivity
import ${configuration.packageName}.viewmodels.*
import ${configuration.packageName}.data.local.AppPreferences
import ${configuration.packageName}.network.Status
import androidx.navigation.NavController
import androidx.navigation.findNavController
import android.app.AlertDialog



fun Fragment.openSettings() {
    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
    val uri = Uri.fromParts("package", requireActivity().packageName, null)
    intent.data = uri
    requireActivity().startActivityForResult(intent, 3)
}

@SuppressLint("NewApi")
fun String.parseDate(pattern: String): String {
    val default = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    val dateTime: LocalDateTime = LocalDateTime.parse(this, default)
    val formatter = DateTimeFormatter.ofPattern(pattern)
    return dateTime.format(formatter)
}

fun View.getColor(colorInt: Int): Int {
    return ContextCompat.getColor(this.context, colorInt)
}

@SuppressLint("HardwareIds")
fun Activity.getDeviceId(): String {
    return Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
}

private var customLoading: CustomLoader? = null
fun Fragment.showProgressBar() {
    hideProgressBar()
    if (context != null) {
        try {
            // Initialize the class
            //customLoading = CustomLoader(context!!, R.raw.loader)
            //setting dialog properties
//            customLoading?.let {
//                it.setCanceledOnTouchOutside(true)
//                it.setCancelable(false)
//                it.show()
//            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}


fun hideProgressBar() {
    if (customLoading != null && customLoading?.isShowing!!) {
        customLoading = try {
            customLoading?.dismiss()
            null
        } catch (e: Exception) {
            null
        }
    }
}

fun Fragment.downloadFile(pdfUrl: String) {
    val url = URL(pdfUrl)
    val path = url.path
    val extension = path.substringAfterLast(".", "")
    val request = DownloadManager.Request(Uri.parse(pdfUrl))
    request.setTitle("Deal File")
    request.setMimeType("image/jpeg")
    request.setDescription("Downloading file...")
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
    request.setDestinationInExternalPublicDir(
        Environment.DIRECTORY_DOWNLOADS,
        "Deal File.$extension"
    )
    val manager = requireActivity().getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    manager.enqueue(request)
}



//Notification
 fun AppCompatActivity.getNotificationPermission() {
    try {
        if (Build.VERSION.SDK_INT > 32) {
            ActivityCompat.requestPermissions(
                this, arrayOf(Manifest.permission.POST_NOTIFICATIONS),
                RequestCodes.NOTIFICATION_PERMISSION
            )
        }
    } catch (e: Exception) {
    }
}

fun AppCompatActivity.askForCameraPermission() {
    ActivityCompat.requestPermissions(
        this, arrayOf(Manifest.permission.CAMERA), RequestCodes.CAMERA_PERMISSION
    )
}

fun Fragment.askForCameraPermission() {
    ActivityCompat.requestPermissions(
        requireActivity(), arrayOf(Manifest.permission.CAMERA), RequestCodes.CAMERA_PERMISSION
    )
}

 fun Fragment.displayNotification(title: String = "", message: String = "", priority: Int = NotificationCompat.PRIORITY_DEFAULT, id: Int = 1 ) {
    println("Notifying")
    val intent= Intent(requireContext(), MainActivity::class.java)
    val pendingIntent = TaskStackBuilder.create(requireContext()).run {
        addNextIntentWithParentStack(intent)
        getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)
    }

    val notification = NotificationCompat.Builder(requireContext(), "baas")
        .setContentTitle(title)
        .setContentText(message)
        .setSmallIcon(R.drawable.ic_loc_active)
        .setPriority(priority)
        .setContentIntent(pendingIntent)
        .build()


    val notificationManger = NotificationManagerCompat.from(requireContext())


    if (ActivityCompat.checkSelfPermission(
            requireContext(),
            Manifest.permission.POST_NOTIFICATIONS
        ) != PackageManager.PERMISSION_GRANTED
    ) {

    }
     notificationManger.notify(id,notification)

}

fun AppCompatActivity.displayNotification(title: String = "", message: String = "", priority: Int = NotificationCompat.PRIORITY_DEFAULT, id: Int = 1 ) {
    val intent= Intent(this, MainActivity::class.java)
    val pendingIntent = TaskStackBuilder.create(this).run {
        addNextIntentWithParentStack(intent)
        getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)
    }

    val notification = NotificationCompat.Builder(this, "baas")
        .setContentTitle(title)
        .setContentText(message)
        .setSmallIcon(R.drawable.ic_loc_active)
        .setPriority(priority)
        .setContentIntent(pendingIntent)
        .build()


    val notificationManger = NotificationManagerCompat.from(this)


    if (ActivityCompat.checkSelfPermission(
            this,
            Manifest.permission.POST_NOTIFICATIONS
        ) != PackageManager.PERMISSION_GRANTED
    ) {

    }
    notificationManger.notify(id,notification)

}
fun AppCompatActivity.observeFCMToken(preferences: AppPreferences, viewModel: BaasViewModel) {
    Constants.FCM_TOKEN.observe(this) {
        preferences.fcmToken = it
        updateFcmToken(it, viewModel)
    }

}

fun AppCompatActivity.updateFcmToken(token: String, viewModel: BaasViewModel) {
    println("Updating fcm token...")
    val payload = mutableMapOf<String, Any>()
    payload["fcm_token"] = token

    viewModel.preferenceUpdate(payload).observe(this) {
        when (it.status) {
            Status.ERROR -> snackBar(
                it.message ?: getString(
                    R.string.something_went_wrong)
                )

            Status.LOADING -> {}
            Status.SUCCESS -> {}
        }
    }
}

// Dialog

fun Fragment.showDialog(title: String = "",
                            message: String = "",
                            positiveButtonText: String = "Ok",
                            negativeButtonText: String = "Cancel",
                            onPositiveButtonClick: () -> Unit = {},
                            onNegativeButtonClick: () -> Unit = {}

) {
    val alertDialogBuilder = AlertDialog.Builder(requireContext())

    alertDialogBuilder.setTitle(title)
    alertDialogBuilder.setMessage(message)

    alertDialogBuilder.setPositiveButton(positiveButtonText) { dialog, _ ->
        // Handle the OK button click
        onPositiveButtonClick()
        dialog.dismiss()
    }

    alertDialogBuilder.setNegativeButton(negativeButtonText) { dialog, _ ->
        // Handle the Cancel button click
        onNegativeButtonClick()
        dialog.dismiss()
    }

    val alertDialog = alertDialogBuilder.create()
    alertDialog.show()
}


fun AppCompatActivity.showDialog(title: String = "",
                        message: String = "",
                        positiveButtonText: String = "Ok",
                        negativeButtonText: String = "Cancel",
                        onPositiveButtonClick: () -> Unit = {},
                        onNegativeButtonClick: () -> Unit = {}

) {
    val alertDialogBuilder = AlertDialog.Builder(this)

    alertDialogBuilder.setTitle(title)
    alertDialogBuilder.setMessage(message)

    alertDialogBuilder.setPositiveButton(positiveButtonText) { dialog, _ ->
        // Handle the OK button click
        onPositiveButtonClick()
        dialog.dismiss()
    }

    alertDialogBuilder.setNegativeButton(negativeButtonText) { dialog, _ ->
        // Handle the Cancel button click
        onNegativeButtonClick()
        dialog.dismiss()
    }

    val alertDialog = alertDialogBuilder.create()
    alertDialog.show()
}



  

  `;
};
