package com.example.proxibuy

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.squareup.picasso.Picasso
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "custom_notification"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "showCustomNotification") {
                val title = call.argument<String>("title")
                val content = call.argument<String>("content")
                val imageUrl = call.argument<String>("imageUrl")
                if (title != null && content != null && imageUrl != null) {
                    showNotificationWithActions(title, content, imageUrl)
                    result.success("Notification Sent")
                } else {
                    result.error("ERROR", "Missing data", null)
                }
            } else {
                result.notImplemented()
            }
        }
    }

    private fun showNotificationWithActions(title: String, content: String, imageUrl: String) {
        val channelId = "custom_channel"
        val notificationId = 101

        // Intent for notification click (MainActivity)
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

        // Create notification channel
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, "Custom Notifications", NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        }

        // Action buttons
        val actionIntent1 = Intent(this, ActionReceiver::class.java).apply { putExtra("action", "Shop") }
        val pendingAction1 = PendingIntent.getBroadcast(this, 0, actionIntent1, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

        val actionIntent2 = Intent(this, ActionReceiver::class.java).apply { putExtra("action", "Direction") }
        val pendingAction2 = PendingIntent.getBroadcast(this, 1, actionIntent2, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

        // Build notification with action buttons
        Picasso.get().load(imageUrl).into(object : com.squareup.picasso.Target {
            @RequiresApi(Build.VERSION_CODES.S)
            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                val notification = NotificationCompat.Builder(this@MainActivity, channelId)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setContentIntent(pendingIntent) // Notification click
                    .addAction(R.drawable.ic_action1, "Shop", pendingAction1) // First action button
                    .addAction(R.drawable.ic_action2, "Direction", pendingAction2) // Second action button
                    .setAutoCancel(true)

                if (bitmap != null) {
                    notification.setStyle(NotificationCompat.BigPictureStyle()
                        .showBigPictureWhenCollapsed(true)
                        .bigPicture(bitmap) // Set the large image
                    )
                }

                notificationManager.notify(notificationId, notification.build())
            }

            override fun onBitmapFailed(e: Exception?, errorDrawable: android.graphics.drawable.Drawable?) {
                // Handle error if image loading fails
                e?.printStackTrace()
            }

            override fun onPrepareLoad(placeHolderDrawable: android.graphics.drawable.Drawable?) {
                // You can show a placeholder if needed
            }
        })
    }
}


