/*
 * 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

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import org.strongswan.android.R
import org.strongswan.android.data.VpnProfile
import org.strongswan.android.data.VpnProfileSource
import org.strongswan.android.ui.compose.SecondaryScaffold
import org.strongswan.android.ui.compose.theme.ShxTheme

class VpnProfileSelectActivity : AppCompatActivity()
{
	override fun onCreate(savedInstanceState: Bundle?)
	{
		super.onCreate(savedInstanceState)
		enableEdgeToEdge()
		setResult(RESULT_CANCELED)
		val source = VpnProfileSource(this).open()
		val profiles = source.allVpnProfiles.sortedBy { it.name?.lowercase() ?: "" }
		source.close()
		setContent {
			ShxTheme {
				ProfileSelectScreen(
					profiles = profiles,
					onBack = { finish() },
					onSelect = { profile -> selectProfile(profile) },
				)
			}
		}
	}

	private fun selectProfile(profile: VpnProfile)
	{
		val shortcut = Intent(VpnProfileControlActivity.START_PROFILE)
		shortcut.putExtra(VpnProfileControlActivity.EXTRA_VPN_PROFILE_UUID, profile.getUUID().toString())
		val builder = ShortcutInfoCompat.Builder(this, profile.getUUID().toString())
		builder.setIntent(shortcut)
		builder.setShortLabel(profile.name ?: profile.gateway ?: "")
		builder.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_shortcut))
		setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, builder.build()))
		finish()
	}
}

@Composable
private fun ProfileSelectScreen(
	profiles: List<VpnProfile>,
	onBack: () -> Unit,
	onSelect: (VpnProfile) -> Unit,
)
{
	SecondaryScaffold(title = stringResource(R.string.strongswan_shortcut), onBack = onBack) { padding ->
		if (profiles.isEmpty())
		{
			Text(stringResource(R.string.no_profiles), modifier = Modifier.padding(padding).padding(24.dp))
		}
		else
		{
			LazyColumn(Modifier.fillMaxSize().padding(padding)) {
				items(profiles, key = { it.getUUID().toString() }) { profile ->
					ListItem(
						headlineContent = { Text(profile.name ?: "") },
						supportingContent = { Text(profile.gateway ?: "") },
						modifier = Modifier.clickable { onSelect(profile) },
					)
				}
			}
		}
	}
}
