/*
 * Copyright 2024 Mifos Initiative
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
 */
package org.mifospay.feature.accounts.savingsaccount.details

import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SuggestionChip
import androidx.compose.material3.SuggestionChipDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.launch
import mobile_wallet.feature.accounts.generated.resources.Res
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_account_balance
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_account_details
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_account_summary
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_arrow
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_available_balance
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_interest_not_posted
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_product_name
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_total_deposits
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_total_interest_posted
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_total_overdraft
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_total_withdrawals
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_detail_wallet_balance
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_error_oops
import mobile_wallet.feature.accounts.generated.resources.feature_accounts_loading
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel
import org.mifospay.core.common.CurrencyFormatter
import org.mifospay.core.designsystem.component.MfLoadingWheel
import org.mifospay.core.designsystem.component.MifosScaffold
import org.mifospay.core.model.account.Account
import org.mifospay.core.model.savingsaccount.SavingAccountDetail
import org.mifospay.core.model.savingsaccount.Status
import org.mifospay.core.model.savingsaccount.Summary
import org.mifospay.core.model.savingsaccount.formatAmount
import org.mifospay.core.model.savingsaccount.toAccount
import org.mifospay.core.ui.EmptyContentScreen
import org.mifospay.core.ui.MifosDivider
import org.mifospay.core.ui.TransactionHistoryCard
import org.mifospay.core.ui.utils.EventsEffect
import org.mifospay.feature.accounts.SavingAccountStatus
import org.mifospay.feature.accounts.color

@Composable
internal fun SavingAccountDetailScreen(
    navigateBack: () -> Unit,
    onViewTransaction: (Long, Long) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: SavingAccountDetailViewModel = koinViewModel(),
) {
    val scope = rememberCoroutineScope()
    val snackbarHostState = remember { SnackbarHostState() }

    val state by viewModel.stateFlow.collectAsStateWithLifecycle()

    EventsEffect(viewModel) { event ->
        when (event) {
            is SADEvent.NavigateBack -> navigateBack.invoke()

            is SADEvent.ShowToast -> {
                scope.launch {
                    snackbarHostState.showSnackbar(event.message)
                }
            }

            is SADEvent.OnViewTransaction -> {
                onViewTransaction(event.clientId, event.accountId)
            }
        }
    }

    SavingAccountDetailScreen(
        state = state.viewState,
        snackbarHostState = snackbarHostState,
        modifier = modifier,
        onAction = remember(viewModel) {
            { viewModel.trySendAction(it) }
        },
    )
}

@Composable
@VisibleForTesting
internal fun SavingAccountDetailScreen(
    state: SADState.ViewState,
    snackbarHostState: SnackbarHostState,
    modifier: Modifier = Modifier,
    onAction: (SADAction) -> Unit,
) {
    MifosScaffold(
        backPress = {
            onAction(SADAction.NavigateBack)
        },
        topBarTitle = stringResource(Res.string.feature_accounts_detail_account_details),
        snackbarHost = { SnackbarHost(snackbarHostState) },
        modifier = modifier,
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(it),
            contentAlignment = Alignment.Center,
        ) {
            when (state) {
                is SADState.ViewState.Loading -> {
                    MfLoadingWheel(
                        contentDesc = stringResource(Res.string.feature_accounts_loading),
                        backgroundColor = MaterialTheme.colorScheme.surface,
                    )
                }

                is SADState.ViewState.Error -> {
                    EmptyContentScreen(
                        title = stringResource(Res.string.feature_accounts_error_oops),
                        subTitle = state.message,
                        modifier = Modifier,
                        iconTint = MaterialTheme.colorScheme.error,
                    )
                }

                is SADState.ViewState.Content -> {
                    SavingAccountDetailScreenContent(
                        state = state,
                        onAction = onAction,
                    )
                }
            }
        }
    }
}

@Composable
private fun SavingAccountDetailScreenContent(
    state: SADState.ViewState.Content,
    modifier: Modifier = Modifier,
    onAction: (SADAction) -> Unit,
) {
    SavingAccountDetails(
        savingAccountDetail = state.data,
        modifier = modifier,
        onViewTransaction = { clientId, accountId ->
            onAction(SADAction.ViewTransaction(clientId, accountId))
        },
    )
}

@Composable
private fun SavingAccountDetails(
    savingAccountDetail: SavingAccountDetail,
    modifier: Modifier = Modifier,
    lazyListState: LazyListState = rememberLazyListState(),
    onViewTransaction: (Long, Long) -> Unit,
) {
    LazyColumn(
        modifier = modifier.fillMaxSize(),
        state = lazyListState,
        contentPadding = PaddingValues(12.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        item {
            SavingAccountCard(
                account = savingAccountDetail.toAccount(),
                status = savingAccountDetail.status,
            )
        }

        item {
            SavingAccountSummaryCard(
                summary = savingAccountDetail.summary,
            )
        }

        item {
            TransactionHistoryCard(
                transactions = savingAccountDetail.transactions,
                onViewTransaction = onViewTransaction,
                showLeadingIcon = true,
                showViewAll = false,
            )
        }
    }
}

@Composable
private fun SavingAccountSummaryCard(
    summary: Summary,
    modifier: Modifier = Modifier,
) {
    Card(
        modifier = modifier
            .fillMaxWidth(),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface,
        ),
    ) {
        Column(
            verticalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            Text(
                text = stringResource(Res.string.feature_accounts_detail_account_summary),
                color = MaterialTheme.colorScheme.primary,
                fontWeight = FontWeight(500),
                modifier = Modifier.padding(8.dp),
            )

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_account_balance))
                Text(
                    text = summary.formatAmount(summary.accountBalance),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_total_deposits))
                Text(
                    text = summary.formatAmount(summary.totalDeposits),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_total_withdrawals))
                Text(
                    text = summary.formatAmount(summary.totalWithdrawals),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_available_balance))
                Text(
                    text = summary.formatAmount(summary.availableBalance),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_total_interest_posted))
                Text(
                    text = summary.totalInterestPosted.toString(),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_total_overdraft))
                Text(
                    text = summary.totalOverdraftInterestDerived.toString(),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }

            MifosDivider()

            RowBlock {
                Text(text = stringResource(Res.string.feature_accounts_detail_interest_not_posted))
                Text(
                    text = summary.interestNotPosted.toString(),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.onSurface,
                )
            }
        }
    }
}

@Composable
private inline fun RowBlock(
    crossinline content: @Composable (RowScope.() -> Unit),
) {
    Box {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp, vertical = 8.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            content()
        }
    }
}

@Composable
private fun SavingAccountCard(
    account: Account,
    status: Status,
    modifier: Modifier = Modifier,
) {
    Box(
        modifier = modifier
            .fillMaxWidth()
            .height(200.dp)
            .background(
                brush = Brush.linearGradient(
                    colors = listOf(
                        MaterialTheme.colorScheme.primary,
                        MaterialTheme.colorScheme.secondary,
                    ),
                ),
                shape = RoundedCornerShape(16.dp),
            ),
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(12.dp),
            verticalArrangement = Arrangement.SpaceBetween,
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Column {
                    Text(
                        text = stringResource(Res.string.feature_accounts_detail_product_name),
                        fontWeight = FontWeight(300),
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.surface,
                    )

                    Text(
                        text = account.name,
                        fontWeight = FontWeight(400),
                        color = MaterialTheme.colorScheme.surface,
                    )
                }

                SavingAccountStatusCard(status)
            }

            Box(
                modifier = Modifier.fillMaxWidth(),
                contentAlignment = Alignment.Center,
            ) {
                Text(
                    text = account.number,
                    fontWeight = FontWeight.Bold,
                    color = MaterialTheme.colorScheme.surface,
                    style = MaterialTheme.typography.headlineMedium,
                    letterSpacing = 0.50.sp,
                )
            }

            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.Bottom,
            ) {
                Column {
                    Text(
                        text = stringResource(Res.string.feature_accounts_detail_wallet_balance),
                        fontWeight = FontWeight(300),
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.surface,
                    )

                    val accountBalance = CurrencyFormatter.format(
                        balance = account.balance,
                        currencyCode = account.currency.code,
                        maximumFractionDigits = null,
                    )

                    Text(
                        text = accountBalance,
                        color = MaterialTheme.colorScheme.surface,
                        style = MaterialTheme.typography.headlineLarge,
                    )
                }

                Icon(
                    modifier = Modifier
                        .graphicsLayer(rotationZ = 90f)
                        .padding(4.dp),
                    imageVector = Icons.Filled.KeyboardArrowUp,
                    contentDescription = stringResource(Res.string.feature_accounts_detail_arrow),
                    tint = MaterialTheme.colorScheme.surface,
                )
            }
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun SavingAccountStatusCard(
    status: Status,
    modifier: Modifier = Modifier,
) {
    val activeStatuses = SavingAccountStatus.entries.filter { it.isActive(status) }

    FlowRow(
        horizontalArrangement = Arrangement.spacedBy(4.dp),
        verticalArrangement = Arrangement.spacedBy(4.dp),
        modifier = modifier,
    ) {
        activeStatuses.forEach { statusEnum ->
            StatusChip(
                label = stringResource(statusEnum.labelRes),
                color = statusEnum.color,
            )
        }
    }
}

@Composable
private fun StatusChip(label: String, color: Color) {
    SuggestionChip(
        onClick = { /* Handle click if needed */ },
        label = { Text(label) },
        border = SuggestionChipDefaults.suggestionChipBorder(
            enabled = true,
            borderColor = color,
        ),
        colors = SuggestionChipDefaults.suggestionChipColors(
            labelColor = color,
        ),
        modifier = Modifier,
    )
}
