package com.tangem.features.txhistory.ui

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.tangem.core.ui.R
import com.tangem.core.ui.components.list.InfiniteListHandler
import com.tangem.core.ui.components.transactions.PendingTxsBlock
import com.tangem.core.ui.components.transactions.Transaction
import com.tangem.core.ui.components.transactions.TxHistoryGroupTitle
import com.tangem.core.ui.components.transactions.TxHistoryTitle
import com.tangem.core.ui.components.transactions.empty.EmptyTransactionBlockLegacy
import com.tangem.core.ui.components.transactions.empty.EmptyTransactionsBlockState
import com.tangem.core.ui.components.transactions.state.TransactionState
import com.tangem.core.ui.decorations.roundedShapeItemDecoration
import com.tangem.core.ui.res.TangemTheme
import com.tangem.features.txhistory.entity.TxHistoryUM

private const val LOAD_ITEMS_BUFFER = 20

fun LazyListScope.txHistoryItemsLegacy(listState: LazyListState, state: TxHistoryUM) {
    when (state) {
        is TxHistoryUM.Content -> contentItems(listState, state)
        is TxHistoryUM.Empty -> nonContentItem(
            state = EmptyTransactionsBlockState.Empty(
                onExplore = state.onExploreClick,
                exploreIconResId = R.drawable.ic_arrow_top_right_24,
            ),
        )
        is TxHistoryUM.Error -> nonContentItem(
            state = EmptyTransactionsBlockState.FailedToLoad(
                onReload = state.onReloadClick,
                onExplore = state.onExploreClick,
                reloadIconResId = R.drawable.ic_refresh_24,
                exploreIconResId = R.drawable.ic_arrow_top_right_24,
            ),
        )
        is TxHistoryUM.Loading -> loadingItems(state)
        is TxHistoryUM.NotSupported -> {
            if (state.pendingTransactions.isNotEmpty()) {
                item(key = "PendingTxsBlock", contentType = "PendingTxsBlock") {
                    PendingTxsBlock(pendingTxs = state.pendingTransactions, isBalanceHidden = state.isBalanceHidden)
                }
            }

            nonContentItem(
                state = EmptyTransactionsBlockState.NotImplemented(
                    onExplore = state.onExploreClick,
                    exploreIconResId = R.drawable.ic_arrow_top_right_24,
                ),
            )
        }
    }
}

private fun LazyListScope.nonContentItem(state: EmptyTransactionsBlockState, modifier: Modifier = Modifier) {
    item(key = state::class.java, contentType = state::class.java) {
        EmptyTransactionBlockLegacy(
            state = state,
            modifier = modifier
                .animateItem(fadeInSpec = null, fadeOutSpec = null)
                .padding(horizontal = TangemTheme.dimens.spacing16, vertical = TangemTheme.dimens.spacing12)
                .fillMaxWidth(),
        )
    }
}

private fun LazyListScope.loadingItems(state: TxHistoryUM.Loading) {
    itemsIndexed(
        items = state.items,
        key = { _, item ->
            when (item) {
                is TxHistoryUM.TxHistoryItemUM.GroupTitle -> item.itemKey
                is TxHistoryUM.TxHistoryItemUM.Title -> item.onExploreClick.hashCode()
                is TxHistoryUM.TxHistoryItemUM.Transaction ->
                    item.state.txHash + (item.state as? TransactionState.Content)?.hashCode()
            }
        },
        contentType = { _, item -> item::class.java },
        itemContent = { index, item ->
            TxHistoryListItemLegacy(
                state = item,
                isBalanceHidden = true,
                modifier = Modifier.roundedShapeItemDecoration(
                    currentIndex = index,
                    lastIndex = state.items.lastIndex,
                ),
            )
        },
    )
}

private fun LazyListScope.contentItems(listState: LazyListState, state: TxHistoryUM.Content) {
    itemsIndexed(
        items = state.items,
        key = { _, item ->
            when (item) {
                is TxHistoryUM.TxHistoryItemUM.GroupTitle -> item.itemKey
                is TxHistoryUM.TxHistoryItemUM.Title -> item.onExploreClick.hashCode()
                is TxHistoryUM.TxHistoryItemUM.Transaction ->
                    item.state.txHash + (item.state as? TransactionState.Content)?.hashCode()
            }
        },
        contentType = { _, item -> item::class.java },
        itemContent = { index, item ->
            TxHistoryListItemLegacy(
                state = item,
                isBalanceHidden = state.isBalanceHidden,
                modifier = Modifier.roundedShapeItemDecoration(
                    currentIndex = index,
                    lastIndex = state.items.lastIndex,
                ),
            )
        },
    )
    item {
        InfiniteListHandler(
            listState = listState,
            buffer = LOAD_ITEMS_BUFFER,
            onLoadMore = state.loadMore,
        )
    }
}

@Composable
internal fun TxHistoryListItemLegacy(
    state: TxHistoryUM.TxHistoryItemUM,
    isBalanceHidden: Boolean,
    modifier: Modifier = Modifier,
) {
    when (state) {
        is TxHistoryUM.TxHistoryItemUM.GroupTitle -> {
            TxHistoryGroupTitle(config = state.legacyGroupTitle, modifier = modifier)
        }
        is TxHistoryUM.TxHistoryItemUM.Title -> {
            TxHistoryTitle(onExploreClick = state.onExploreClick, modifier = modifier)
        }
        is TxHistoryUM.TxHistoryItemUM.Transaction -> {
            Transaction(
                state = state.state,
                isBalanceHidden = isBalanceHidden,
                modifier = modifier,
            )
        }
    }
}