package com.zaojiao.app.feat.home.index

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
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.requiredWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
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.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zaojiao.app.feat.home.R
import com.zaojiao.component.common.Spacer

@Composable
fun HomeIndexTopBar() {
    Row(
        modifier = Modifier
            .background(color = Color(0xFFFBFCFD))
            .padding(top = 24.dp, bottom = 10.dp)
            .statusBarsPadding()
            .fillMaxWidth()
            .height(44.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        HomeIndexTopBarUserInfo()
        HomeIndexTopBarSearchBar()
        HomeIndexTopBarSignupButton()
    }
}

@Composable
fun HomeIndexTopBarUserInfo() {
    Row(
        modifier = Modifier
            .padding(start = 16.dp)
            .background(
                color = Color(0x260B57C7),
                shape = RoundedCornerShape(100.dp)
            )
            .height(44.dp)
            .wrapContentWidth(),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Image(
            painter = painterResource(id = R.mipmap.default_avatar),
            contentDescription = "这是用户的头像",
            modifier = Modifier
                .clip(
                    shape = RoundedCornerShape(50),
                )
                .size(44.dp),
            contentScale = ContentScale.Crop,
        )
        Box(modifier = Modifier.width(4.dp))
        Column {
            Text(
                text = "逻辑狗",
                modifier = Modifier.requiredWidth(45.dp),
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                style = TextStyle(
                    color = Color(0xFF0B57C7),
                    fontSize = 13.sp,
                    lineHeight = 18.sp,
                    fontWeight = FontWeight.Medium,
                ),
            )
            Text(
                text = "3岁8个月",
                maxLines = 1,
                style = TextStyle(
                    color = Color(0x990B57C7),
                    fontSize = 11.sp,
                    lineHeight = 16.sp,
                    fontWeight = FontWeight.Medium,
                ),
            )
        }
        Box(modifier = Modifier.width(12.dp))
    }
}

@Composable
fun RowScope.HomeIndexTopBarSearchBar() {
    Row(
        modifier = Modifier
            .weight(1f)
            .padding(start = 12.dp, end = 12.dp)
            .shadow(
                elevation = 2.dp,
                shape = RoundedCornerShape(100.dp),
            )
            .background(
                color = Color.White,
                shape = RoundedCornerShape(100.dp),
            )
            .padding(start = 10.dp, end = 10.dp, top = 11.dp, bottom = 11.dp)
            .fillMaxHeight()
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Image(
            painter = painterResource(id = R.mipmap.index_search),
            contentDescription = "",
            contentScale = ContentScale.Fit,
        )
        Spacer(width = 6.dp)
        Text(
            text = "逻辑狗",
            style = TextStyle(
                color = Color(0xFFCECECE),
                fontSize = 14.sp,
                lineHeight = 14.sp,
            ),
            modifier = Modifier.weight(1f),
        )
        Image(
            painter = painterResource(id = R.mipmap.index_qrcode),
            contentDescription = "",
            contentScale = ContentScale.Fit,
        )
    }
}

@Composable
fun HomeIndexTopBarSignupButton() {
    Image(
        painter = painterResource(id = R.mipmap.index_signup),
        contentDescription = "签到",
        modifier = Modifier
            .padding(end = 16.dp)
            .size(44.dp),
        contentScale = ContentScale.Crop,
    )
}