package eu.mhsl.marianum.mobile.client

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.os.Bundle
import eu.mhsl.marianum.mobile.client.widgets.WidgetRefreshRequester
import eu.mhsl.marianum.mobile.client.widgets.WidgetRenderer

class TimetableWeekWidget : AppWidgetProvider() {
    override fun onEnabled(context: Context) {
        // First widget of this kind placed → fetch fresh data right away.
        WidgetRefreshRequester.enqueueOneOffRefresh(context)
    }

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray,
    ) {
        for (id in appWidgetIds) {
            val options = appWidgetManager.getAppWidgetOptions(id)
            val views = WidgetRenderer.buildWeek(context, context.packageName, options)
            appWidgetManager.updateAppWidget(id, views)
        }
    }

    override fun onAppWidgetOptionsChanged(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetId: Int,
        newOptions: Bundle,
    ) {
        val views = WidgetRenderer.buildWeek(context, context.packageName, newOptions)
        appWidgetManager.updateAppWidget(appWidgetId, views)
    }
}
