package com.nearle.gear

import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetProvider as BaseHomeWidgetProvider

class HomeWidgetProvider : BaseHomeWidgetProvider() {

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray,
        widgetData: SharedPreferences
    ) {
        val path = widgetData.getString("banner_path", null) ?: return
        val bitmap = BitmapFactory.decodeFile(path) ?: return

        appWidgetIds.forEach { widgetId ->
            val views = RemoteViews(context.packageName, R.layout.home_widget_layout).apply {
                setImageViewBitmap(R.id.widget_banner_image, bitmap)
            }
            appWidgetManager.updateAppWidget(widgetId, views)
        }
    }
}