/*
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 */

package at.bitfire.davdroid.ui.widget

import android.content.Context
import android.widget.Toast
import androidx.compose.material3.ColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.glance.ColorFilter
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.Image
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.action.clickable
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.provideContent
import androidx.glance.background
import androidx.glance.layout.Alignment
import androidx.glance.layout.Row
import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.padding
import androidx.glance.layout.size
import androidx.glance.material3.ColorProviders
import androidx.glance.text.Text
import androidx.glance.text.TextDefaults
import at.bitfire.davdroid.R

/**
 * A widget with a "Sync all" button displaying an icon and a label.
 */
class LabeledSyncButtonWidget(
    private val model: SyncWidgetViewModel,
    private val lightColorScheme: ColorScheme,
    private val darkColorScheme: ColorScheme
) : GlanceAppWidget() {

    override suspend fun provideGlance(context: Context, id: GlanceId) {
        // will be called when the widget is updated
        provideContent {
            GlanceTheme(
                colors = ColorProviders(
                    light = lightColorScheme,
                    dark = darkColorScheme
                )
            ) {
                WidgetContent(model)
            }
        }
    }

    @Composable
    private fun WidgetContent(model: SyncWidgetViewModel) {
        val context = LocalContext.current

        Row(
            modifier = GlanceModifier
                .fillMaxWidth()
                .background(GlanceTheme.colors.primary)
                .cornerRadius(16.dp)
                .padding(4.dp)
                .clickable {
                    model.requestSync()
                    Toast.makeText(context, R.string.sync_started, Toast.LENGTH_SHORT).show()
                },
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                provider = ImageProvider(R.drawable.ic_sync),
                contentDescription = context.getString(R.string.widget_sync_all_accounts),
                modifier = GlanceModifier
                    .padding(vertical = 8.dp, horizontal = 8.dp)
                    .size(32.dp),
                colorFilter = ColorFilter.tint(GlanceTheme.colors.onPrimary)
            )
            Text(
                text = context.getString(R.string.widget_sync_all),
                modifier = GlanceModifier
                    .defaultWeight()
                    .padding(end = 8.dp),
                style = TextDefaults.defaultTextStyle.copy(
                    color = GlanceTheme.colors.onPrimary,
                    fontSize = 16.sp
                )
            )
        }
    }

}