/*
 * 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 android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import org.strongswan.android.R
import org.strongswan.android.data.VpnProfile
import org.strongswan.android.data.VpnProfileDataSource
import org.strongswan.android.data.VpnProfileSource
import org.strongswan.android.ui.compose.ProfileFormScreen
import org.strongswan.android.ui.compose.isValid
import org.strongswan.android.ui.compose.toFormState
import org.strongswan.android.ui.compose.toProfile
import org.strongswan.android.ui.compose.validate
import org.strongswan.android.ui.compose.theme.ShxTheme
import org.strongswan.android.utils.Constants

class VpnProfileDetailActivity : AppCompatActivity()
{
	private lateinit var dataSource: VpnProfileDataSource
	private var existing: VpnProfile? = null

	override fun onCreate(savedInstanceState: Bundle?)
	{
		super.onCreate(savedInstanceState)
		enableEdgeToEdge()
		dataSource = VpnProfileSource(this).open()
		val uuid = savedInstanceState?.getString(VpnProfileDataSource.KEY_UUID)
			?: intent.extras?.getString(VpnProfileDataSource.KEY_UUID)
		if (uuid != null)
		{
			existing = dataSource.getVpnProfile(uuid)
			if (existing == null)
			{
				finish()
				return
			}
		}
		val initial = existing?.toFormState() ?: org.strongswan.android.ui.compose.ProfileFormState()
		val title = if (existing != null) existing!!.name ?: getString(R.string.add_profile)
		else getString(R.string.add_profile)
		setContent {
			ShxTheme {
				var state by remember { mutableStateOf(initial) }
				ProfileFormScreen(
					title = title,
					state = state,
					onChange = { state = it },
					onCancel = { finish() },
					onSave = {
						val checked = state.validate(this)
						state = checked
						if (!checked.isValid())
						{
							Toast.makeText(this, R.string.alert_text_no_input_gateway, Toast.LENGTH_SHORT).show()
							return@ProfileFormScreen
						}
						val profile = checked.toProfile(existing)
						if (existing != null) dataSource.updateVpnProfile(profile)
						else dataSource.insertProfile(profile)
						val intent = Intent(Constants.VPN_PROFILES_CHANGED)
						intent.putExtra(Constants.VPN_PROFILES_SINGLE, profile.getUUID().toString())
						LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
						setResult(RESULT_OK, Intent().putExtra(VpnProfileDataSource.KEY_UUID, profile.getUUID().toString()))
						finish()
					},
				)
			}
		}
	}

	override fun onSaveInstanceState(outState: Bundle)
	{
		if (existing != null)
		{
			outState.putString(VpnProfileDataSource.KEY_UUID, existing!!.getUUID().toString())
		}
		super.onSaveInstanceState(outState)
	}

	override fun onDestroy()
	{
		if (::dataSource.isInitialized)
		{
			dataSource.close()
		}
		super.onDestroy()
	}
}
