package com.aiosman.riderpro

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
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.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.draw.shadow
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


@Composable
fun ProfilePage(){
    Column (
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            .padding(bottom = 16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Top,
    ){
        CarGroup()
        UserInformation()
        RidingStyle()
        UserMoment()
    }
}

@Composable
fun CarGroup(){
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 54.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        CarTopInformation()
        CarTopPicture()
    }
}

@Composable
fun CarTopInformation(){
    Row{
        Text(text = "BMW", color = Color.Black, fontSize = 12.sp, style = TextStyle(fontWeight = FontWeight.Bold))
        Text(modifier = Modifier.padding(start = 4.dp), text = "/", color = Color.Gray, fontSize = 12.sp)
        Text(modifier = Modifier.padding(start = 4.dp), text = "M1000RR", color = Color.Gray, fontSize = 12.sp)
    }
}

@Composable
fun CarTopPicture(){
    Image(
        modifier = Modifier
            .size(width = 336.dp, height = 224.dp)
            .padding(top = 42.dp),
        painter = painterResource(id = R.drawable.default_profile_moto), contentDescription = "")
}

@Composable
fun UserInformation(){
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 8.dp, start = 33.dp, end = 33.dp),
        horizontalAlignment = Alignment.CenterHorizontally){
        Row (modifier = Modifier.fillMaxWidth()){
            val userInfoModifier = Modifier.weight(1f)
            UserInformationFollowers(userInfoModifier)
            UserInformationBasic(userInfoModifier)
            UserInformationFollowing(userInfoModifier)
        }
        UserInformationSlogan()
        CommunicationOperatorGroup()
    }
}

@Composable
fun UserInformationFollowers(modifier: Modifier){
    Column(modifier = modifier.padding(top = 31.dp)) {
        Text(modifier = Modifier.padding(bottom = 5.dp),text = "183", fontSize = 24.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
        Spacer(
            modifier = Modifier
                .size(width = 88.83.dp, height = 1.dp)
                .border(width = 1.dp, color = Color.Gray)
                .padding(top = 5.dp, bottom = 5.dp)
        )
        Text(modifier = Modifier.padding(top = 5.dp),text = "FOLLOWERS", fontSize = 12.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
    }
}

@Composable
fun UserInformationBasic(modifier: Modifier){
    Column(
        horizontalAlignment = Alignment.CenterHorizontally) {
        Box(modifier = Modifier.size(width = 112.dp, height = 112.dp),
            contentAlignment = Alignment.Center
            ){
            Image(
                modifier = Modifier.fillMaxSize(),
                painter = painterResource(id = R.drawable.avatar_bold), contentDescription = "")
            Image(
                modifier = Modifier
                    .size(width = 88.dp, height = 88.dp)
                    .clip(
                        RoundedCornerShape(88.dp)
                    ),
                painter = painterResource(id = R.drawable.default_avatar), contentDescription = "")

        }
        Text(modifier = Modifier.padding(top = 8.dp), text = "Atom", fontSize = 32.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
        Text(modifier = Modifier.padding(top = 4.dp), text = "America", fontSize = 12.sp, color = Color.Gray)
        Text(modifier = Modifier.padding(top = 4.dp), text = "Member since Jun 4.2019", fontSize = 12.sp, color = Color.Gray)
    }
}

@Composable
fun UserInformationFollowing(modifier: Modifier){
    Column(modifier = modifier.padding(top = 6.dp),
        horizontalAlignment = Alignment.End) {
        Text(modifier = Modifier.padding(bottom = 5.dp), text = "306", fontSize = 24.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
        Box(
            modifier = Modifier
                .size(width = 88.83.dp, height = 1.dp)
                .border(width = 1.dp, color = Color.Gray)

        )
        Text(modifier = Modifier.padding(top = 5.dp),text = "FOLLOWING", fontSize = 12.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
    }
}

@Composable
fun UserInformationSlogan(){
    Text(modifier = Modifier.padding(top = 23.dp),text = "Ridering shooting fishing not much more", fontSize = 13.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
}

@Composable
fun CommunicationOperatorGroup(){
    val navController = LocalNavController.current
    Row (modifier = Modifier
        .fillMaxWidth()
        .padding(top = 16.dp), horizontalArrangement = Arrangement.Center){
        Box(modifier = Modifier.size(width = 142.dp, height = 40.dp),
            contentAlignment = Alignment.Center){
            Image(modifier = Modifier.fillMaxSize(), painter = painterResource(id = R.drawable.rider_pro_profile_follow), contentDescription = "")
            Text(text = "FOLLOW", fontSize = 16.sp, color = Color.White, style = TextStyle(fontWeight = FontWeight.Bold))
        }
        Box(modifier = Modifier
            .size(width = 142.dp, height = 40.dp)
            .padding(start = 6.dp).clickable {
                navController.navigate("MyMessage")
            },
            contentAlignment = Alignment.Center){
            Image(modifier = Modifier.fillMaxSize(),painter = painterResource(id = R.drawable.rider_pro_profile_message), contentDescription = "")
            Text(text = "MESSAGE", fontSize = 16.sp, color = Color.White, style = TextStyle(fontWeight = FontWeight.Bold))
        }
        Box(modifier = Modifier.size(width = 142.dp, height = 40.dp).clickable {
            navController.navigate("ProfileTimeline")
        },
            contentAlignment = Alignment.Center){
            Image(modifier = Modifier.fillMaxSize(), painter = painterResource(id = R.drawable.rider_pro_profile_follow), contentDescription = "")
            Text(text = "GALLERY", fontSize = 16.sp, color = Color.White, style = TextStyle(fontWeight = FontWeight.Bold))
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun RidingStyle(){
    Column(modifier = Modifier
        .fillMaxWidth()
        .padding(start = 24.dp, top = 40.dp, end = 24.dp),
        horizontalAlignment = Alignment.Start) {
        Text(text = "RIDING STYLES", fontSize = 18.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
        Image(modifier = Modifier
            .padding(top = 4.dp)
            .height(8.dp),painter = painterResource(id = R.drawable.rider_pro_profile_line), contentDescription = "")
        FlowRow (modifier = Modifier
            .fillMaxWidth()
            .padding(top = 24.dp)){
            RidingStyleItem(styleContent = "Cruiser")
            RidingStyleItem(styleContent = "Bobber")
            RidingStyleItem(styleContent = "Cafe")
            RidingStyleItem(styleContent = "Chopper")
            RidingStyleItem(styleContent = "Sport")
            RidingStyleItem(styleContent = "Vintage")
            RidingStyleItem(styleContent = "Trike")
            RidingStyleItem(styleContent = "Touring")
        }
    }
}

@Composable
fun RidingStyleItem(styleContent: String){
    Box(modifier = Modifier.padding(bottom = 8.dp, end = 8.dp),
        contentAlignment = Alignment.Center) {
        Image(modifier = Modifier.shadow(
            ambientColor = Color.Gray,
            spotColor = Color(0f,0f,0f,0.2f),
            elevation = 20.dp,
        ), painter = painterResource(id = R.drawable.rider_pro_style_wrapper), contentDescription = "")
        Text(modifier = Modifier.padding(start = 5.dp, end = 5.dp), text = styleContent, fontSize = 12.sp, color = Color.Gray, style = TextStyle(fontWeight = FontWeight.Bold))
    }
}

@Composable
fun UserMoment(){
    profileMomentItems.forEach(
        action = { MomentPostUnit(it) }
    )
}

@Composable
fun MomentPostUnit(momentItem: MomentItem){
    TimeGroup(momentItem.time)
    MomentCard(momentItem.momentTextContent,
        momentItem.momentPicture,
        momentItem.likeCount.toString(),
        momentItem.commentCount.toString())
}

@Composable
fun TimeGroup(time: String = "2024.06.08 12:23"){
    Row(modifier = Modifier
        .fillMaxWidth()
        .padding(start = 24.dp, top = 40.dp, end = 24.dp),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically){
        Image(
            modifier = Modifier.padding(end = 12.dp),
            painter = painterResource(id = R.drawable.rider_pro_moment_time_flag), contentDescription = "")
        Text(text = time,fontSize = 22.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold))
    }
}

@Composable
fun MomentCard(content: String,@DrawableRes picture: Int, like: String, comment: String){
    Column(modifier = Modifier
        .fillMaxWidth()
        .padding(start = 48.dp, top = 18.dp, end = 24.dp)
        .border(width = 1.dp, color = Color(0f,0f,0f,0.1f), shape = RoundedCornerShape(6.dp))
        ){
        MomentCardTopContent(content)
        MomentCardPicture(picture)
        MomentCardOperation(like,comment)
    }
}

@Composable
fun MomentCardTopContent(content: String){
    Row(modifier = Modifier
        .fillMaxWidth(),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically){
        Text(
            modifier = Modifier.padding(16.dp),
            text = content,fontSize = 16.sp, color = Color.Black)
    }
}

@Composable
fun MomentCardPicture(@DrawableRes drawable: Int){
    Image(modifier = Modifier
        .fillMaxSize()
        .padding(16.dp), painter = painterResource(id = drawable), contentDescription = "")

}

@Composable
fun MomentCardOperation(like: String, comment: String){
    Row(modifier = Modifier
        .fillMaxWidth()
        .height(56.dp),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically){
        Spacer(modifier = Modifier.weight(1f))
        MomentCardOperationItem(drawable = R.drawable.rider_pro_like, number = like, modifier = Modifier.weight(1f))
        MomentCardOperationItem(drawable = R.drawable.rider_pro_moment_comment, number = comment, modifier = Modifier.weight(1f))
    }
}

@Composable
fun MomentCardOperationItem(@DrawableRes drawable: Int, number: String, modifier: Modifier){
    Row(modifier = modifier,
        verticalAlignment = Alignment.CenterVertically){
        Image(modifier = Modifier.padding(start = 16.dp, end = 8.dp),
            painter = painterResource(id = drawable), contentDescription = "")
        Text(text = number)
    }
}