package com.tangem.features.account.archived.ui

import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.datasource.CollectionPreviewParameterProvider
import com.tangem.common.ui.account.AccountIconPreviewData
import com.tangem.common.ui.account.AccountRow
import com.tangem.core.res.R
import com.tangem.core.ui.components.appbar.AppBarWithBackButton
import com.tangem.core.ui.components.buttons.SecondarySmallButton
import com.tangem.core.ui.components.buttons.SmallButtonConfig
import com.tangem.core.ui.decorations.roundedShapeItemDecoration
import com.tangem.core.ui.extensions.resolveReference
import com.tangem.core.ui.extensions.resourceReference
import com.tangem.core.ui.extensions.stringReference
import com.tangem.core.ui.extensions.stringResourceSafe
import com.tangem.core.ui.res.TangemTheme
import com.tangem.core.ui.res.TangemThemePreview
import com.tangem.core.ui.test.accounts.ArchivedAccountsScreenTestTags
import com.tangem.features.account.archived.entity.AccountArchivedUM
import com.tangem.features.account.archived.entity.ArchivedAccountUM
import kotlinx.collections.immutable.toImmutableList

@Composable
internal fun ArchivedAccountListContent(state: AccountArchivedUM, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .background(color = TangemTheme.colors.background.secondary)
            .fillMaxSize()
            .imePadding()
            .systemBarsPadding(),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        AppBarWithBackButton(
            text = stringResourceSafe(R.string.account_archived_title),
            onBackClick = state.onCloseClick,
            modifier = Modifier.height(TangemTheme.dimens.size56),
        )

        Column(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f),

        ) {
            when (state) {
                is AccountArchivedUM.Content -> ArchiveAccountContent(state)
                is AccountArchivedUM.Error -> ArchiveAccountError(state)
                is AccountArchivedUM.Loading -> ArchiveAccountLoading()
            }
        }
    }
}

@Composable
private fun ArchiveAccountLoading(modifier: Modifier = Modifier) {
    Box(
        modifier = modifier.fillMaxSize(),
        contentAlignment = Alignment.Center,
    ) {
        CircularProgressIndicator(
            color = TangemTheme.colors.icon.primary1,
            modifier = Modifier,
        )
    }
}

@Composable
private fun ArchiveAccountError(state: AccountArchivedUM.Error, modifier: Modifier = Modifier) {
    Box(
        modifier = modifier.fillMaxSize(),
        contentAlignment = Alignment.Center,
    ) {
        Column(
            verticalArrangement = Arrangement.spacedBy(TangemTheme.dimens.spacing12),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Text(
                color = TangemTheme.colors.text.tertiary,
                style = TangemTheme.typography.caption2,
                text = stringResourceSafe(R.string.common_unable_to_load),
            )
            SecondarySmallButton(
                config = SmallButtonConfig(
                    text = resourceReference(R.string.try_to_load_data_again_button_title),
                    onClick = state.onRetryClick,
                ),
            )
        }
    }
}

@Composable
private fun ArchiveAccountContent(state: AccountArchivedUM.Content, modifier: Modifier = Modifier) {
    LazyColumn(modifier = modifier.testTag(ArchivedAccountsScreenTestTags.ARCHIVED_ACCOUNTS_SCREEN_CONTAINER)) {
        itemsIndexed(
            items = state.accounts,
            key = { index, item -> item.accountId },
        ) { index, account ->
            ArchivedAccountRow(
                item = account,
                modifier = Modifier.roundedShapeItemDecoration(
                    backgroundColor = TangemTheme.colors.background.primary,
                    radius = TangemTheme.dimens.radius20,
                    currentIndex = index,
                    addDefaultPadding = true,
                    lastIndex = state.accounts.lastIndex,
                ),
            )
        }
    }
}

@Composable
private fun ArchivedAccountRow(item: ArchivedAccountUM, modifier: Modifier = Modifier) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .clickable(enabled = !item.isLoading, onClick = item.onClick)
            .padding(all = TangemTheme.dimens.spacing12)
            .testTag(ArchivedAccountsScreenTestTags.ARCHIVED_ACCOUNT_ITEM),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(TangemTheme.dimens.spacing12),
    ) {
        val subtitle = stringResourceSafe(
            id = R.string.account_label_tokens_info,
            formatArgs = arrayOf(
                item.tokensInfo.resolveReference(),
                item.networksInfo.resolveReference(),
            ),
        )
        AccountRow(
            title = item.accountName,
            subtitle = stringReference(subtitle),
            icon = item.accountIconUM,
            modifier = Modifier.weight(1f),
        )

        SecondarySmallButton(
            config = SmallButtonConfig(
                text = resourceReference(R.string.account_archived_recover),
                isLoading = item.isLoading,
                onClick = item.onClick,
            ),
            modifier = Modifier.testTag(ArchivedAccountsScreenTestTags.RESTORE_BUTTON),
        )
    }
}

@Preview(showBackground = true, widthDp = 360)
@Preview(showBackground = true, widthDp = 360, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun WcConnectionsContentPreview(@PreviewParameter(PreviewStateProvider::class) params: AccountArchivedUM) {
    TangemThemePreview {
        ArchivedAccountListContent(state = params)
    }
}

@Suppress("MagicNumber")
private class PreviewStateProvider : CollectionPreviewParameterProvider<AccountArchivedUM>(
    buildList {
        fun portfolioIcon() = AccountIconPreviewData.randomAccountIcon()
        val accountName = stringReference("Account name")

        val firstList = List(10) { id ->
            ArchivedAccountUM(
                accountId = id.toString(),
                accountName = accountName,
                accountIconUM = portfolioIcon(),
                tokensInfo = stringReference("10 tokens"),
                networksInfo = stringReference("2 networks"),
                onClick = {},
                isLoading = id % 2 == 0,
            )
        }.toImmutableList()
        val first = AccountArchivedUM.Content(
            onCloseClick = {},
            accounts = firstList,
        )
        add(first)
        add(AccountArchivedUM.Loading {})
        add(AccountArchivedUM.Error({}, {}))
    },
)