package com.zaojiao.app.feat.home.course

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.aspectRatio
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.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zaojiao.app.feat.home.R
import com.zaojiao.component.common.Colors
import com.zaojiao.component.common.Expanded
import com.zaojiao.component.common.Icons
import com.zaojiao.component.common.LJGBanner
import com.zaojiao.component.common.Spacer

@Composable
internal fun HomeCourseGroupBuy() {
    Row(
        modifier = Modifier
            .padding(horizontal = 16.dp)
            .fillMaxWidth()
            .wrapContentHeight()
    ) {
        HomeCourseGroupBuySwiper()
        Spacer(width = 10.dp)
        HomeCourseGroupBuyRecommend()
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun RowScope.HomeCourseGroupBuySwiper() {
    Box(
        modifier = Modifier
            .weight(1f)
            .aspectRatio(165f / 220f)
    ) {
        val pagerState = rememberPagerState(initialPage = 200 * 4)

        LJGBanner(
            data = listOf(
                "https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF",
                "https://t7.baidu.com/it/u=4198287529,2774471735&fm=193&f=GIF",
                "https://t7.baidu.com/it/u=1956604245,3662848045&fm=193&f=GIF",
                "https://t7.baidu.com/it/u=2529476510,3041785782&fm=193&f=GIF",
            ), pagerState = pagerState, onImagePath = { index, item ->
                item
            }, modifier = Modifier
                .clip(RoundedCornerShape(20.dp))
                .fillMaxSize()
        )

        Row(
            modifier = Modifier
                .padding(bottom = 5.dp)
                .align(Alignment.BottomCenter)
                .wrapContentHeight()
                .height(7.dp),
            horizontalArrangement = Arrangement.spacedBy(6.dp),
        ) {
            repeat(4) { index ->
                if (index == (pagerState.currentPage % 4)) {
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(7.dp))
                            .background(color = Color.White)
                            .size(7.dp, 7.dp)
                    )
                } else {
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(7.dp))
                            .background(color = Color.White.copy(alpha = 0.3f))
                            .size(7.dp, 7.dp)
                    )
                }
            }
        }
    }
}

@Composable
private fun RowScope.HomeCourseGroupBuyRecommend() {
    Box(
        modifier = Modifier
            .weight(1f)
            .aspectRatio(165f / 220f)
    ) {
        Image(
            painter = painterResource(id = R.mipmap.course_group_buy_bg),
            contentDescription = null,
        )
        Column(
            modifier = Modifier.fillMaxSize(),
        ) {
            HomeCourseGroupBuyRecommendTitle()
            Spacer(height = 10.dp)
            HomeCourseGroupBuyRecommendBody()
        }
    }
}

@Composable
private fun ColumnScope.HomeCourseGroupBuyRecommendTitle() {
    Row(
        modifier = Modifier
            .weight(1f)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Spacer(width = 11.dp)
        Image(
            painter = painterResource(id = R.mipmap.course_group_buy_text),
            contentDescription = null,
        )
        Expanded()
        Row(
            modifier = Modifier
                .background(color = Color.White, shape = RoundedCornerShape(100.dp))
                .padding(horizontal = 6.dp, vertical = 4.dp)
                .wrapContentWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Text(
                text = "22款上线",
                style = TextStyle(
                    fontSize = 10.sp,
                    lineHeight = 11.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = Colors.from("#FFFB6C07"),
                ),
            )
            Spacer(width = 2.dp)
            Icons.Forward(
                size = Size(4f, 6f),
                color = Colors.from("#FFFB6C07"),
                width = 0.5.dp,
            )
        }
        Spacer(width = 11.dp)
    }
}

@Composable
private fun HomeCourseGroupBuyRecommendBody() {
    Column(
        modifier = Modifier
            .padding(horizontal = 11.dp)
            .wrapContentHeight()
            .fillMaxWidth(),

        ) {
        Row(
            modifier = Modifier
                .wrapContentHeight()
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Box(
                modifier = Modifier
                    .background(color = Color.Blue)
                    .width(64.dp)
                    .height(64.dp),
            )
            Box(
                modifier = Modifier
                    .background(color = Color.Blue)
                    .width(64.dp)
                    .height(64.dp),
            )
        }
        Spacer(height = 15.dp)
        Row(
            modifier = Modifier
                .wrapContentHeight()
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Box(
                modifier = Modifier
                    .background(color = Color.Blue)
                    .width(64.dp)
                    .height(74.5.dp),
            )
            Box(
                modifier = Modifier
                    .background(color = Color.Blue)
                    .width(64.dp)
                    .height(74.5.dp),
            )
        }
    }
}