package com.kelthos.reasonabledoubt

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

class RightsWidget : AppWidgetProvider() {

    private val rights = arrayOf(
        "Right to Legal Representation — Everyone has the right to an attorney.",
        "Right to Privacy — Your personal data is protected by law in most countries.",
        "Freedom of Expression — You can voice opinions within legal boundaries.",
        "Right to Fair Trial — Every accused person deserves a fair hearing.",
        "Right to Property — Your property cannot be seized without due process.",
        "Right to Education — Access to basic education is a fundamental right.",
        "Freedom from Discrimination — Equal treatment regardless of background.",
        "Right to Health — Access to healthcare is recognized internationally."
    )

    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        for (appWidgetId in appWidgetIds) {
            val views = RemoteViews(context.packageName, R.layout.rights_widget)
            val dayIndex = (System.currentTimeMillis() / 86400000).toInt() % rights.size
            views.setTextViewText(R.id.widget_right, rights[dayIndex])

            val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
            intent?.putExtra("route", "/rights")
            val pendingIntent = PendingIntent.getActivity(context, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
            views.setOnClickPendingIntent(R.id.widget_root, pendingIntent)

            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }
}
