package com.example.lexilearn.ui.views.pHome

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
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.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.lexilearn.R
import com.example.lexilearn.ui.components.AutoSizeText
import com.example.lexilearn.ui.components.ButtonHome
import com.example.lexilearn.ui.theme.cGray
import com.example.lexilearn.ui.theme.cTextPrimary
import com.example.lexilearn.ui.theme.cprimary
import com.example.lexilearn.ui.theme.csecondary
import com.example.lexilearn.ui.theme.ctextBlack
import com.example.lexilearn.ui.theme.ctextWhite
import com.example.lexilearn.ui.theme.cwhite


@Composable
fun HomeScreen(navController: NavController) {
    val viewModel: HomeViewModel = viewModel()
    val scrollState = rememberScrollState()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(
                brush = Brush.linearGradient(
                    colors = listOf(cprimary, csecondary),
                    start = Offset(0f, 0f), // Kiri atas
                    end = Offset(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) // Kanan bawah
                )
            )
    ) {
        ConstraintLayout(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            val (titleRef, dateRef) = createRefs()
            Text(
                text = "Name",
                fontSize = 20.sp,
                fontWeight = FontWeight.SemiBold,
                color = ctextBlack,
                modifier = Modifier.constrainAs(titleRef) {
                    top.linkTo(parent.top, margin = 24.dp)
                    start.linkTo(parent.start, margin = 16.dp)
                    bottom.linkTo(parent.bottom, margin = 24.dp)
                }
            )
            Text(
                text = "Selamat Datang",
                fontSize = 14.sp,
                color = ctextWhite,
                modifier = Modifier.constrainAs(dateRef) {
                    top.linkTo(titleRef.bottom, margin = 4.dp)
                    start.linkTo(titleRef.start)
                }
            )
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(1.dp)
                .background(color = cGray)
        )
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp)
                .border(
                    width = 2.dp,
                    color = cwhite,
                    shape = RoundedCornerShape(12.dp)
                )
                .clip(RoundedCornerShape(12.dp))
                .background(
                    brush = Brush.linearGradient(
                        colors = listOf(
                            cprimary,
                            csecondary
                        ), // Gradient dari cprimary ke csecondary
                        start = Offset(0f, 0f), // Mulai dari kiri atas
                        end = Offset(
                            Float.POSITIVE_INFINITY,
                            Float.POSITIVE_INFINITY
                        ) // Berakhir di kanan bawah
                    )
                )
        ) {
            Column(Modifier.padding(14.dp)) {
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.Bottom,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(
                        text = "Materi Terselesaikan",
                        color = ctextWhite,
                        fontWeight = FontWeight.Bold,
                        fontSize = 16.sp
                    )
                    Image(
                        painter = painterResource(id = R.drawable.bg_children),
                        contentDescription = "image",
                        modifier = Modifier.size(80.dp)
                    )
                }
            }
        }
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .verticalScroll(scrollState)
        ) {
            Text(
                text = "Belajar Mengenal Huruf & Angka",
                color = cTextPrimary,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(10.dp)
            )
            ConstraintLayout(
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                val (quizRef, alphabetRef) = createRefs()
                ButtonHome(
                    onClick = { navController.navigate("learnNumber") }, modifier = Modifier
                        .constrainAs(quizRef) {
                            start.linkTo(parent.start)
                            top.linkTo(parent.top)
                            width = Dimension.percent(0.5f)
                        }, color = Color(0xfff99155)
                ) {
                    Column(modifier = Modifier.padding(vertical = 10.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.bg_number),
                            contentDescription = null,
                            alignment = Alignment.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 12.dp)
                        )
                        Text(
                            text = "Pengenalan Angka",
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth(),
                            color = ctextWhite
                        )
                    }
                }
                ButtonHome(
                    onClick = { navController.navigate("learnAlphabet") }, modifier = Modifier
                        .constrainAs(alphabetRef) {
                            top.linkTo(parent.top)
                            end.linkTo(parent.end)
                            bottom.linkTo(quizRef.bottom)
                            width = Dimension.percent(0.5f)
                        }, color = Color(0xff3dd470)
                ) {
                    Column(modifier = Modifier.padding(vertical = 10.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.bg_alphabet),
                            contentDescription = null,
                            alignment = Alignment.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 12.dp)
                        )
                        Text(
                            text = "Pengenalan Alfabet",
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth(),
                            color = ctextWhite
                        )
                    }
                }
            }
            Text(
                text = "Latihan Bahasa Inggris Interaktif",
                color = cTextPrimary,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(10.dp)
            )

            ConstraintLayout(
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                val (quizRef, alphabetRef, screeningRef, newsRef) = createRefs()
                ButtonHome(onClick = { navController.navigate("read") },
                    color = Color(0xff47bff0),
                    modifier = Modifier
                        .constrainAs(quizRef) {
                            start.linkTo(parent.start)
                            top.linkTo(parent.top)
                            width = Dimension.percent(0.5f)
                        }) {
                    Column(modifier = Modifier.padding(vertical = 10.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.bg_limb),
                            contentDescription = null,
                            alignment = Alignment.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 12.dp)
                        )
                        Text(
                            text = "Anggota Tubuh",
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth(),
                            color = ctextWhite
                        )
                    }
                }
                ButtonHome(onClick = { },
                    color = Color(0xffea2b72),

                    modifier = Modifier
                        .constrainAs(alphabetRef) {
                            top.linkTo(parent.top)
                            end.linkTo(parent.end)
                            bottom.linkTo(quizRef.bottom)
                            width = Dimension.percent(0.5f)
                        }) {
                    Column(modifier = Modifier.padding(vertical = 10.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.bg_house),
                            contentDescription = null,
                            alignment = Alignment.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 12.dp)
                        )
                        Text(
                            text = "Bagian Rumah",
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth(),
                            color = ctextWhite
                        )
                    }
                }
                ButtonHome(onClick = { navController.navigate("navMaterial/animal") },
                    color = Color(0xff4264e7), modifier = Modifier
                        .constrainAs(screeningRef) {
                            top.linkTo(quizRef.bottom)
                            start.linkTo(quizRef.start)
                            width = Dimension.percent(0.5f)
                        }) {
                    Column(modifier = Modifier.padding(vertical = 10.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.bg_animal),
                            contentDescription = null,
                            alignment = Alignment.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 12.dp)
                        )
                        AutoSizeText(
                            text = "Pengenalan Hewan",
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth(),
                            color = ctextWhite,
                            maxLines = 1
                        )
                    }
                }
                ButtonHome(onClick = { },
                    color = Color(0xffedb92f),
                    modifier = Modifier
                        .constrainAs(newsRef) {
                            top.linkTo(quizRef.bottom)
                            end.linkTo(parent.end)
                            width = Dimension.percent(0.5f)
                        }) {
                    Column(modifier = Modifier.padding(vertical = 10.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.bg_family),
                            contentDescription = null,
                            alignment = Alignment.Center,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 12.dp)
                        )
                        AutoSizeText(
                            text = "Anggota Keluarga",
                            fontWeight = FontWeight.Bold,
                            textAlign = TextAlign.Center,
                            modifier = Modifier.fillMaxWidth(),
                            color = ctextWhite,
                            maxLines = 1
                        )
                    }
                }
            }
        }
    }
}
