package com.zaojiao.component.common

import android.graphics.pdf.PdfDocument.Page
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import coil.compose.AsyncImage
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun <T> LJGBanner(
    data: List<T>,
    pagerState: PagerState? = null,
    onImagePath: (index: Int, data: T) -> String,
    modifier: Modifier = Modifier,
    contentScale: ContentScale = ContentScale.Crop,
    isLoopBanner: Boolean = true,
    loopDelay: Long = 3000,
    loopPeriod: Long = 3000,
    desc: @Composable ((index: Int, data: T) -> String)? = null,
    onBannerItemClick: ((index: Int, data: T) -> Unit)? = null,
) {
    val state: PagerState = pagerState ?: rememberPagerState(initialPage = 200 * data.size)

    if (isLoopBanner) {
        val coroutineScope = rememberCoroutineScope()
        DisposableEffect(Unit) {
            val timer = Timer()
            timer.schedule(object : TimerTask() {
                override fun run() {
                    coroutineScope.launch {
                        val next = state.currentPage + 1
                        state.animateScrollToPage(next)
                    }
                }
            }, loopDelay, loopPeriod)
            onDispose {
                timer.cancel()
            }
        }
    }
    val constraintSet = ConstraintSet {
        val image = createRefFor("image")
        val content = createRefFor("content")
        constrain(image) {
            start.linkTo(parent.start)
            end.linkTo(parent.end)
        }
        constrain(content) {
            bottom.linkTo(image.bottom)
            start.linkTo(image.start)
        }
    }
    HorizontalPager(
        pageCount = Int.MAX_VALUE,
        state = state,
        pageSpacing = 0.dp,
        modifier = modifier,
    ) { index ->
        val actualIndex = index % data.size
        val item = data[actualIndex]

        ConstraintLayout(constraintSet = constraintSet) {
            Images.Network(
                url = onImagePath(actualIndex, item),
                description = desc?.invoke(actualIndex, item),
                contentScale = contentScale,
                modifier = Modifier
                    .layoutId("image")
                    .fillMaxSize()
                    .clickable {
                        onBannerItemClick?.invoke(actualIndex, item)
                    },
            )
        }
    }
}