package com.agentui.desktop

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.RemoteViews

class TranscriptWidgetProvider : AppWidgetProvider() {

    companion object {
        const val ACTION_REFRESH = "com.agentui.desktop.WIDGET_REFRESH"
        const val ACTION_ITEM_CLICK = "com.agentui.desktop.WIDGET_ITEM_CLICK"
    }

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        for (appWidgetId in appWidgetIds) {
            val views = RemoteViews(context.packageName, R.layout.widget_transcript)

            // ListView adapter
            val serviceIntent = Intent(context, TerminalListWidgetService::class.java).apply {
                putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
                data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
            }
            views.setRemoteAdapter(R.id.terminal_list, serviceIntent)
            views.setEmptyView(R.id.terminal_list, R.id.empty_view)

            // Item click template → opens app
            val itemClickIntent = Intent(context, MainActivity::class.java).apply {
                action = ACTION_ITEM_CLICK
            }
            val itemClickPending = PendingIntent.getActivity(
                context, 0, itemClickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
            )
            views.setPendingIntentTemplate(R.id.terminal_list, itemClickPending)

            // Refresh button
            val refreshIntent = Intent(context, TranscriptWidgetProvider::class.java).apply {
                action = ACTION_REFRESH
            }
            val refreshPending = PendingIntent.getBroadcast(
                context, 1, refreshIntent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            views.setOnClickPendingIntent(R.id.btn_refresh, refreshPending)

            // Title tap → open app
            val appIntent = Intent(context, MainActivity::class.java)
            val appPending = PendingIntent.getActivity(
                context, 2, appIntent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            views.setOnClickPendingIntent(R.id.widget_title, appPending)

            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)

        if (intent.action == ACTION_REFRESH) {
            // Notify the ListView adapter to refresh its data
            val appWidgetManager = AppWidgetManager.getInstance(context)
            val widgetIds = appWidgetManager.getAppWidgetIds(
                ComponentName(context, TranscriptWidgetProvider::class.java)
            )
            appWidgetManager.notifyAppWidgetViewDataChanged(widgetIds, R.id.terminal_list)
        }
    }

    override fun onEnabled(context: Context) {
        // Periodic refresh via WorkManager for background updates
        TranscriptWidgetWorker.enqueue(context)
    }

    override fun onDisabled(context: Context) {
        TranscriptWidgetWorker.cancel(context)
    }
}
