/*
 * Copyright (c) 2026 QPQ IaaS AG <info@qpq.swiss>. All rights reserved.
 * Project: Gajumaru Core Java Libraries <gajumaru.io>
 *
 * This program is dual-licensed:
 * 1) Under the GNU Affero General Public License as published by the Free
 *    Software Foundation, either version 3 of the License, or (at your option)
 *    any later version (AGPL-3.0-or-later).
 *
 * 2) Under a commercial/proprietary license available directly from QPQ IaaS AG.
 *    If you wish to use this software outside the strict constraints of the
 *    AGPLv3 (e.g. within a closed-source or proprietary product), you must
 *    purchase a commercial license from QPQ IaaS AG.
 *
 * Authors:
 *   - Craig Everett <craigeverett@qpq.swiss>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-QPQ-Commercial
 */

package swiss.qpq.gajumobile.ui.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.res.stringResource
import swiss.qpq.gajumobile.R
import swiss.qpq.gajumobile.ui.components.GajuButton
import swiss.qpq.gajumobile.ui.components.GajuHeader
import swiss.qpq.gajumobile.ui.components.GajuTextField
import swiss.qpq.gajumaru.core.formatting.GajuFormat

@Composable
fun SendFormScreen(
    fromAccount: swiss.qpq.gajumobile.data.models.Account,
    balanceFormat: GajuFormat.Type,
    balanceUnit: GajuFormat.Unit,
    initialRecipient: String = "",
    initialAmount: String = "",
    initialPayload: String = "",
    initialGas: String = "20000",
    initialGasPrice: String = "1000000000",
    initialTTL: String = "500",
    onReview: (recipient: String, amount: String, payload: String, gas: String, gasPrice: String, ttl: String) -> Unit,
    onBack: () -> Unit,
) {
    var toAddress by remember { mutableStateOf(value = initialRecipient) }
    var amount by remember { mutableStateOf(value = initialAmount) }
    var payload by remember { mutableStateOf(value = initialPayload) }
    var gas by remember { mutableStateOf(value = initialGas) }
    var gasPrice by remember { mutableStateOf(value = initialGasPrice) }
    var ttl by remember { mutableStateOf(value = initialTTL) }
    var showAdvanced by remember { mutableStateOf(value = false) }

    Scaffold(
        modifier = Modifier.fillMaxSize(),
        bottomBar = {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .windowInsetsPadding(WindowInsets.navigationBars)
                    .padding(24.dp),
            ) {
                GajuButton(
                    text = stringResource(id = R.string.back_button_caps),
                    onClick = onBack,
                    modifier = Modifier.weight(1f),
                    containerColor = MaterialTheme.colorScheme.secondary,
                    contentColor = MaterialTheme.colorScheme.onSecondary,
                )
                Spacer(modifier = Modifier.width(16.dp))
                GajuButton(
                    text = stringResource(id = R.string.review_button_caps),
                    onClick = { onReview(toAddress, amount, payload, gas, gasPrice, ttl) },
                    modifier = Modifier.weight(1f),
                    enabled = toAddress.isNotBlank() && amount.isNotBlank(),
                )
            }
        },
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(innerPadding)
                .imePadding()
                .padding(horizontal = 24.dp)
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            GajuHeader()
            
            Text(
                text = stringResource(id = R.string.send_title),
                color = MaterialTheme.colorScheme.onBackground,
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(vertical = 16.dp),
            )

            // 0. Sender Account (Read-only)
            Column(modifier = Modifier.fillMaxWidth()) {
                Text(stringResource(id = R.string.sending_from_label), color = MaterialTheme.colorScheme.onSurfaceVariant, fontSize = 14.sp)
                Spacer(modifier = Modifier.height(4.dp))
                Surface(
                    color = MaterialTheme.colorScheme.surfaceVariant,
                    shape = RoundedCornerShape(8.dp),
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Column(modifier = Modifier.padding(12.dp)) {
                        Text(
                            text = fromAccount.label,
                            color = MaterialTheme.colorScheme.primary,
                            fontWeight = FontWeight.Bold,
                            fontSize = 16.sp,
                        )
                        Text(
                            text = stringResource(id = R.string.balance_with_value, fromAccount.formattedBalance(balanceFormat, balanceUnit)),
                            color = MaterialTheme.colorScheme.onSurfaceVariant,
                            fontSize = 14.sp,
                            fontWeight = FontWeight.Medium,
                        )
                        Text(
                            text = fromAccount.gajuId,
                            color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f),
                            fontSize = 12.sp,
                            fontFamily = FontFamily.Monospace,
                        )
                    }
                }
            }

            Spacer(modifier = Modifier.height(24.dp))

            // 1. Recipient Account ID
            GajuTextField(
                value = toAddress,
                onValueChange = { toAddress = it },
                label = stringResource(id = R.string.recipient_account_label),
                placeholder = stringResource(id = R.string.recipient_account_placeholder),
                modifier = Modifier.fillMaxWidth(),
                singleLine = true,
            )

            Spacer(modifier = Modifier.height(24.dp))

            // 2. Amount (String input)
            val unitLabel = if (balanceUnit == GajuFormat.Unit.GAJU) "GAJU" else "PUCK"
            GajuTextField(
                value = amount,
                onValueChange = { amount = it },
                label = stringResource(id = R.string.amount_label),
                placeholder = stringResource(id = R.string.amount_placeholder),
                modifier = Modifier.fillMaxWidth(),
                trailingIcon = { Text(unitLabel, color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold, fontSize = 12.sp) },
                singleLine = true,
            )

            Spacer(modifier = Modifier.height(24.dp))

            // 3. Payload/Message
            GajuTextField(
                value = payload,
                onValueChange = { payload = it },
                label = stringResource(id = R.string.payload_label),
                placeholder = stringResource(id = R.string.payload_placeholder),
                modifier = Modifier.fillMaxWidth(),
            )

            Spacer(modifier = Modifier.height(24.dp))

            Text(
                text = if (showAdvanced) stringResource(id = R.string.hide_details) else stringResource(id = R.string.show_details),
                color = MaterialTheme.colorScheme.primary,
                fontWeight = FontWeight.Bold,
                fontSize = 14.sp,
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable { showAdvanced = !showAdvanced }
                    .padding(vertical = 8.dp),
                textAlign = TextAlign.Center,
            )

            if (showAdvanced) {
                Spacer(modifier = Modifier.height(16.dp))

                // 4. Gas
                GajuTextField(
                    value = gas,
                    onValueChange = { gas = it },
                    label = stringResource(id = R.string.gas_label),
                    modifier = Modifier.fillMaxWidth(),
                    singleLine = true,
                )

                Spacer(modifier = Modifier.height(16.dp))

                Row(modifier = Modifier.fillMaxWidth()) {
                    // 5. Gas Price
                    GajuTextField(
                        value = gasPrice,
                        onValueChange = { gasPrice = it },
                        label = stringResource(id = R.string.gas_price_label),
                        modifier = Modifier.weight(1f),
                        singleLine = true,
                    )
                    Spacer(modifier = Modifier.width(16.dp))
                    // 6. TTL
                    GajuTextField(
                        value = ttl,
                        onValueChange = { ttl = it },
                        label = stringResource(id = R.string.ttl_label),
                        modifier = Modifier.weight(1f),
                        singleLine = true,
                    )
                }
            }
            
            Spacer(modifier = Modifier.height(32.dp))
        }
    }
}
