/*
 * Copyright (C) 2026 SHX VPN
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 */

package org.strongswan.android.ui.compose

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Language
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.strongswan.android.R
import org.strongswan.android.data.VpnProfile
import org.strongswan.android.ui.compose.theme.LocalShxStatusColors

@Composable
fun ProfileAvatar(icon: ImageVector, connected: Boolean, modifier: Modifier = Modifier)
{
	val status = LocalShxStatusColors.current
	val container = if (connected) status.connectedContainer else MaterialTheme.colorScheme.primaryContainer
	val tint = if (connected) status.connected else MaterialTheme.colorScheme.primary
	Box(
		modifier = modifier
			.size(40.dp)
			.background(container, CircleShape),
		contentAlignment = Alignment.Center,
	) {
		Icon(
			imageVector = icon,
			contentDescription = null,
			tint = tint,
			modifier = Modifier.size(22.dp),
		)
	}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ShxProfileRow(
	profile: VpnProfile,
	active: Boolean,
	leadingIcon: ImageVector = Icons.Rounded.Language,
	onClick: () -> Unit,
	onLongClick: () -> Unit = {},
	trailing: (@Composable () -> Unit)? = null,
)
{
	Row(
		verticalAlignment = Alignment.CenterVertically,
		modifier = Modifier
			.fillMaxWidth()
			.combinedClickable(onClick = onClick, onLongClick = onLongClick)
			.padding(horizontal = 16.dp, vertical = 12.dp),
	) {
		ProfileAvatar(icon = leadingIcon, connected = active)
		Column(
			modifier = Modifier
				.weight(1f)
				.padding(horizontal = 12.dp),
		) {
			Text(
				text = profile.name ?: "",
				style = MaterialTheme.typography.titleSmall.copy(fontSize = 16.sp),
				fontWeight = FontWeight.Medium,
				maxLines = 1,
				overflow = TextOverflow.Ellipsis,
			)
			Text(
				text = profile.gateway ?: "",
				style = MaterialTheme.typography.bodySmall,
				color = MaterialTheme.colorScheme.onSurfaceVariant,
				maxLines = 1,
				overflow = TextOverflow.Ellipsis,
			)
		}
		if (trailing != null)
		{
			trailing()
		}
		else if (active)
		{
			ActiveBadge()
		}
	}
}

@Composable
fun ActiveBadge(modifier: Modifier = Modifier)
{
	val status = LocalShxStatusColors.current
	Surface(
		color = status.connectedContainer,
		contentColor = status.connected,
		shape = CircleShape,
		modifier = modifier,
	) {
		Text(
			text = stringResource(R.string.badge_connected),
			style = MaterialTheme.typography.labelSmall,
			modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp),
		)
	}
}

@Composable
fun ShxDivider(modifier: Modifier = Modifier, indent: Boolean = true)
{
	Surface(
		color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.6f),
		modifier = modifier
			.fillMaxWidth()
			.padding(start = if (indent) 68.dp else 0.dp, end = if (indent) 16.dp else 0.dp)
			.height(1.dp),
	) {}
}
