package com.zaojiao.component.common

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
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.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.key
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

fun LazyListScope.list(
    count: Int,
    itemContent: @Composable (index: Int) -> Unit,
    itemSeparation: @Composable (index: Int) -> Unit,
) {
    val total = count * 2 - 1
    items(total) { index ->
        if (index % 2 == 0) {
            itemContent(index / 2)
        } else {
            itemSeparation(index / 2)
        }
    }
}

fun LazyListScope.grid(
    count: Int,
    span: Int,
    hSpace: Dp = 0.dp,
    vSpace: Dp = 0.dp,
    isVertical: Boolean,
    padding: Dp = 0.dp,
    itemContent: @Composable (index: Int) -> Unit,
) {
    val gridCount = (count + span - 1) / span

    val mainTotal = gridCount * 2 - 1
    val crossTotal = span * 2 - 1

    for (i in 0 until mainTotal) {
        if (i % 2 == 0) {
            item {
                if (isVertical) {
                    Row(
                        modifier = Modifier
                            .padding(horizontal = padding)
                            .wrapContentHeight()
                            .fillMaxWidth(),
                    ) {
                        for (j in 0 until crossTotal) {
                            if (j % 2 == 0) {
                                val index = (i / 2) * span + j / 2
                                Box(
                                    modifier = Modifier.weight(1f, fill = true),
                                    propagateMinConstraints = true,
                                ) {
                                    itemContent(index)
                                }
                            } else {
                                Box(modifier = Modifier.width(hSpace))
                            }
                        }
                    }
                } else {
                    Column(
                        modifier = Modifier
                            .padding(vertical = padding)
                            .wrapContentWidth()
                            .fillMaxHeight(),
                    ) {
                        for (j in 0 until crossTotal) {
                            if (j % 2 == 0) {
                                val index = (i / 2) * span + j / 2
                                Box(
                                    modifier = Modifier.weight(1f, fill = true),
                                    propagateMinConstraints = true
                                ) {
                                    itemContent(index)
                                }
                            } else {
                                Box(modifier = Modifier.height(vSpace))
                            }
                        }
                    }

                }
            }
        } else {
            item {
                if (isVertical) {
                    Box(modifier = Modifier.height(vSpace))
                } else {
                    Box(modifier = Modifier.width(hSpace))
                }
            }
        }
    }
}