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 DailyTipWidget : AppWidgetProvider() {

    private val tips = arrayOf(
        "Always read a contract fully before signing — verbal promises are rarely enforceable.",
        "In most countries, you have the right to remain silent when questioned by police.",
        "Keep copies of all legal documents — digital and physical backup is recommended.",
        "A notarized document carries more legal weight than a simple signature.",
        "Know your local statute of limitations — delayed action can void your claim.",
        "Tenants generally have the right to habitable living conditions by law.",
        "Employment contracts override verbal agreements in most jurisdictions.",
        "You can request a lawyer before answering questions during an investigation.",
        "Consumer protection laws often allow returns within 14-30 days of purchase.",
        "Witnesses strengthen any legal case — document interactions when possible."
    )

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

            val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
            val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
            views.setOnClickPendingIntent(R.id.widget_root, pendingIntent)

            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }
}
