/*
 * Book's Story — free and open-source Material You eBook reader.
 * Copyright (C) 2024-2026 Acclorite
 * SPDX-License-Identifier: GPL-3.0-only
 */

package org.dueattendant149.bookshelf.ui.bookshelf

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CloudOff
import androidx.compose.material.icons.filled.CloudQueue
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Download
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import org.dueattendant149.bookshelf.R
import org.dueattendant149.bookshelf.domain.model.cache.CacheState
import org.dueattendant149.bookshelf.domain.model.cache.CacheStatus
import org.dueattendant149.bookshelf.domain.model.library.Book
import org.dueattendant149.bookshelf.presentation.bookshelf.RemoteLibraryModel
import org.dueattendant149.bookshelf.ui.common.components.placeholder.EmptyPlaceholder
import org.dueattendant149.bookshelf.ui.common.components.placeholder.ErrorPlaceholder

@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)
@Composable
fun RemoteLibraryContent(model: RemoteLibraryModel) {
    val state by model.state.collectAsStateWithLifecycle()
    val refreshState = rememberPullRefreshState(
        refreshing = state.isLoading,
        onRefresh = model::loadLibraries
    )

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(stringResource(R.string.bookshelf_screen)) }
            )
        }
    ) { padding ->
        Box(
            modifier = Modifier
                .fillMaxSize()
                .pullRefresh(refreshState)
                .padding(padding)
        ) {
            LazyColumn(
                modifier = Modifier.fillMaxSize(),
                contentPadding = PaddingValues(16.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                item {
                    Text(
                        text = stringResource(R.string.bookshelf_libraries_header),
                        style = MaterialTheme.typography.titleSmall,
                        color = MaterialTheme.colorScheme.primary,
                        modifier = Modifier.padding(bottom = 4.dp)
                    )
                }

                items(state.libraries.size) { index ->
                    val library = state.libraries[index]
                    val selected = state.selectedLibrary?.id == library.id
                    Card(
                        onClick = { model.selectLibrary(library) },
                        modifier = Modifier.fillMaxWidth(),
                    ) {
                        ListItem(
                            headlineContent = {
                                Text(
                                    text = library.name,
                                    style = MaterialTheme.typography.titleMedium
                                )
                            },
                            supportingContent = {
                                Text(
                                    text = stringResource(
                                        R.string.bookshelf_library_subtitle,
                                        library.mediaType,
                                        library.itemCount
                                    )
                                )
                            },
                            trailingContent = {
                                if (selected) {
                                    Text(
                                        text = stringResource(R.string.bookshelf_selected_label),
                                        style = MaterialTheme.typography.labelMedium,
                                        color = MaterialTheme.colorScheme.primary
                                    )
                                }
                            }
                        )
                    }
                }

                state.selectedLibrary?.let { library ->
                    item {
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = stringResource(R.string.bookshelf_books_header, library.name),
                            style = MaterialTheme.typography.titleSmall,
                            color = MaterialTheme.colorScheme.primary,
                            modifier = Modifier.padding(bottom = 4.dp)
                        )
                    }

                    item {
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.SpaceBetween
                        ) {
                            Text(
                                text = stringResource(R.string.bookshelf_offline_only_label),
                                style = MaterialTheme.typography.bodyMedium
                            )
                            Switch(
                                checked = state.offlineOnly,
                                onCheckedChange = { model.toggleOfflineOnly() }
                            )
                        }
                    }
                }

                items(state.books.size) { index ->
                    val book = state.books[index]
                    val cacheStatus = state.cacheStatuses[book.remoteId]
                    BookListItem(
                        book = book,
                        cacheStatus = cacheStatus,
                        onDownload = { model.cacheBook(book) },
                        onDelete = { model.deleteCache(book) }
                    )
                }

                if (state.isLoading && state.libraries.isEmpty() && state.books.isEmpty()) {
                    item {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(vertical = 32.dp),
                            contentAlignment = Alignment.Center
                        ) {
                            CircularProgressIndicator()
                        }
                    }
                }
            }

            if (!state.isLoading && state.libraries.isEmpty() && state.error == null) {
                EmptyPlaceholder(
                    modifier = Modifier.align(Alignment.Center),
                    message = stringResource(R.string.bookshelf_empty_message),
                    icon = rememberVectorPainter(Icons.Default.CloudQueue),
                    actionTitle = stringResource(R.string.bookshelf_refresh_action),
                    action = model::loadLibraries
                )
            }

            state.error?.let { error ->
                ErrorPlaceholder(
                    modifier = Modifier.align(Alignment.Center),
                    errorMessage = error,
                    icon = rememberVectorPainter(Icons.Default.CloudOff),
                    actionTitle = stringResource(R.string.bookshelf_retry_action),
                    action = model::loadLibraries
                )
            }

            PullRefreshIndicator(
                refreshing = state.isLoading,
                state = refreshState,
                modifier = Modifier.align(Alignment.TopCenter),
                backgroundColor = MaterialTheme.colorScheme.inverseSurface,
                contentColor = MaterialTheme.colorScheme.inverseOnSurface
            )
        }
    }
}

@Composable
private fun BookListItem(
    book: Book,
    cacheStatus: CacheStatus?,
    onDownload: () -> Unit,
    onDelete: () -> Unit,
) {
    val cacheState = cacheStatus?.state ?: CacheState.NONE
    val showDownload = cacheState == CacheState.NONE || cacheState == CacheState.FAILED
    val showDelete = cacheState != CacheState.NONE

    Card(
        modifier = Modifier.fillMaxWidth(),
    ) {
        Column {
            ListItem(
                headlineContent = {
                    Text(
                        text = book.title,
                        style = MaterialTheme.typography.titleMedium
                    )
                },
                supportingContent = {
                    Column {
                        Text(
                            text = book.author.getAsString()
                                ?: stringResource(R.string.unknown_author)
                        )
                        if (cacheState != CacheState.NONE) {
                            Text(
                                text = cacheStatusLabel(cacheState),
                                style = MaterialTheme.typography.labelMedium,
                                color = MaterialTheme.colorScheme.primary
                            )
                        }
                    }
                },
                trailingContent = {
                    Row {
                        if (showDownload) {
                            IconButton(onClick = onDownload) {
                                Icon(
                                    imageVector = Icons.Default.Download,
                                    contentDescription = stringResource(R.string.bookshelf_download_action)
                                )
                            }
                        }
                        if (showDelete) {
                            IconButton(onClick = onDelete) {
                                Icon(
                                    imageVector = Icons.Default.Delete,
                                    contentDescription = stringResource(R.string.bookshelf_delete_cache_action)
                                )
                            }
                        }
                    }
                }
            )

            if (cacheState == CacheState.DOWNLOADING) {
                LinearProgressIndicator(
                    progress = { cacheStatus?.progress?.coerceIn(0f, 1f) ?: 0f },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp, vertical = 8.dp),
                )
            }
        }
    }
}

@Composable
private fun cacheStatusLabel(state: CacheState): String =
    when (state) {
        CacheState.NONE -> ""
        CacheState.PENDING -> stringResource(R.string.bookshelf_cache_pending)
        CacheState.DOWNLOADING -> stringResource(R.string.bookshelf_cache_downloading)
        CacheState.COMPLETED -> stringResource(R.string.bookshelf_cache_completed)
        CacheState.FAILED -> stringResource(R.string.bookshelf_cache_failed)
    }
