/*
 * 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.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.heightIn
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
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 swiss.qpq.gajumobile.R
import swiss.qpq.gajumobile.data.models.GridsSignRequest
import swiss.qpq.gajumobile.data.models.GridsSignType
import swiss.qpq.gajumobile.ui.components.GajuButton
import swiss.qpq.gajumobile.ui.components.GajuHeader

@Composable
fun SignRequestScreen(
    request: GridsSignRequest,
    originUrl: String,
    accountLabel: String,
    accountId: String,
    onSign: () -> Unit,
    onCancel: () -> Unit,
) {
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        bottomBar = {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .windowInsetsPadding(WindowInsets.navigationBars)
                    .padding(24.dp)
            ) {
                GajuButton(
                    text = stringResource(R.string.cancel),
                    onClick = onCancel,
                    modifier = Modifier.weight(1f),
                    containerColor = MaterialTheme.colorScheme.secondary,
                    contentColor = MaterialTheme.colorScheme.onSecondary,
                )
                Spacer(modifier = Modifier.width(16.dp))
                GajuButton(
                    text = stringResource(R.string.sign_button_caps),
                    onClick = onSign,
                    modifier = Modifier.weight(1f),
                )
            }
        },
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(innerPadding)
                .padding(horizontal = 24.dp)
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            GajuHeader()

            // NOTE: CRE 2026-08-25
            //  This eventually needs to expand to complete handling of contract call requests.
            //  That will involve some additions to GRIDS dead drop message forms to include
            //  contract call requests where the intent should be expressed and the signer
            //  requestee should be burdened with forming the TX from the contract name/ID,
            //  function name, and args. This would be significantly more robust to abuse than
            //  the current "TX signature request" where the TX internals are opaque.
            Text(
                text = when (request.type) {
                    GridsSignType.MESSAGE -> stringResource(R.string.msg_sig_req)
                    GridsSignType.BINARY  -> stringResource(R.string.bin_sig_req)
                    GridsSignType.TX      -> stringResource(R.string.tx_sig_req)
                    else                  -> stringResource(R.string.generic_sig_req)
                },
                color = MaterialTheme.colorScheme.onBackground,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(vertical = 16.dp),
                textAlign = TextAlign.Center
            )

            Text(
                stringResource(R.string.sig_req_description),
                fontSize = 12.sp,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                textAlign = TextAlign.Center
            )

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

            SectionLabel(stringResource(R.string.sig_account_label))
            Text(accountLabel, color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Bold)
            Text(accountId, fontSize = 10.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace)

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

            SectionLabel(stringResource(R.string.originating_url_label))
            Text(originUrl, fontSize = 10.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, textAlign = TextAlign.Center)

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

            SectionLabel(when (request.type) {
                GridsSignType.MESSAGE -> stringResource(R.string.payload_type_message)
                GridsSignType.BINARY  -> stringResource(R.string.payload_type_base64)
                GridsSignType.TX      -> stringResource(R.string.payload_type_tx)
                else                  -> stringResource(R.string.payload_type_generic)
            })

            Surface(
                modifier = Modifier.fillMaxWidth().heightIn(min = 100.dp, max = 300.dp),
                color = MaterialTheme.colorScheme.surfaceVariant,
                shape = RoundedCornerShape(8.dp),
            ) {
                Text(
                    text = request.payload,
                    modifier = Modifier.padding(16.dp).verticalScroll(rememberScrollState()),
                    fontSize = 12.sp,
                    fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
            
            Spacer(modifier = Modifier.height(24.dp))
        }
    }
}

@Composable
private fun SectionLabel(text: String) {
    Text(
        text = text,
        style = MaterialTheme.typography.labelSmall,
        color = MaterialTheme.colorScheme.primary.copy(alpha = 0.7f),
        modifier = Modifier.padding(bottom = 4.dp)
    )
}
