/*
 * Copyright 2025 Pierre-Yves Nicolas
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */
package org.mydomain.myscan.view

import android.graphics.Bitmap
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

const val PAGE_LIST_ELEMENT_SIZE_DP = 120

@Composable
fun CommonPageList(
    pageIds: List<String>,
    imageLoader: (String) -> Bitmap?,
    onPageClick: (Int) -> Unit,
    listState: LazyListState = rememberLazyListState(),
    currentPageIndex: Int? = null,
    onLastItemPosition: ((Offset) -> Unit)? = null,
) {
    LazyRow (
        state = listState,
        contentPadding = PaddingValues(4.dp),
        modifier = Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceContainer),
        horizontalArrangement = Arrangement.spacedBy(8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        itemsIndexed(pageIds) { index, id ->
            // TODO Use small images rather than big ones
            val image = imageLoader(id)
            if (image != null) {
                PageThumbnail(
                    image,
                    index,
                    currentPageIndex,
                    pageIds.lastIndex,
                    onLastItemPosition,
                    onPageClick
                )
            }
        }
    }
    if (pageIds.isEmpty()) {
        Box(
            modifier = Modifier
                .height(120.dp)
                .addPositionCallback(onLastItemPosition, LocalDensity.current, 0.5f)
        ) {}
    }
}

@Composable
private fun PageThumbnail(
    image: Bitmap,
    index: Int,
    currentPageIndex: Int?,
    lastIndex: Int,
    onLastItemPosition: ((Offset) -> Unit)?,
    onPageClick: (Int) -> Unit,
) {
    val bitmap = image.asImageBitmap()
    val isSelected = index == currentPageIndex
    val borderColor =
        if (isSelected) MaterialTheme.colorScheme.secondary else Color.Transparent
    val maxImageSize = PAGE_LIST_ELEMENT_SIZE_DP.dp
    var modifier =
        if (bitmap.height > bitmap.width)
            Modifier.height(maxImageSize)
        else
            Modifier.width(maxImageSize)
    if (index == lastIndex) {
        val density = LocalDensity.current
        modifier = modifier.addPositionCallback(onLastItemPosition, density, 1.0f)
    }
    Box (modifier = Modifier.height(PAGE_LIST_ELEMENT_SIZE_DP.dp)) {
        Image(
            bitmap = bitmap,
            contentDescription = null,
            modifier = modifier
                .align(Alignment.Center)
                .padding(4.dp)
                .border(2.dp, borderColor)
                .clickable { onPageClick(index) }
        )
        Box(
            modifier = Modifier
                .padding(8.dp)
                .fillMaxWidth()
                .align(Alignment.BottomCenter)
                .background(Color.Black.copy(alpha = 0.5f), shape = RoundedCornerShape(4.dp))
                .padding(vertical = 0.dp, horizontal = 8.dp)
        ) {
            Text(
                text = "${index + 1}",
                color = Color.White,
                fontSize = 10.sp,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}

fun Modifier.addPositionCallback(callback: ((Offset) -> Unit)?, density: Density, xFactor: Float): Modifier {
    if (callback == null) {
        return this
    }
    return this.onGloballyPositioned { coordinates ->
        with(density) {
            callback(coordinates.localToWindow(
                Offset(
                    x = PAGE_LIST_ELEMENT_SIZE_DP.dp.toPx() * xFactor,
                    y = PAGE_LIST_ELEMENT_SIZE_DP.dp.toPx() / 2)))
        }
    }
}
