package com.zaojiao.app.feat.design

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
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 AppBar(
    title: String,
    backColor: Color = Colors.FF333333,
    primary: Boolean = true,
) {
    var modifier: Modifier = Modifier
    modifier = modifier.background(color = Color.White)
    if (primary) {
        modifier = modifier.statusBarsPadding()
    }
    modifier = modifier.fillMaxWidth()
    modifier = modifier.height(44.dp)


    Box(
        modifier = modifier,
    ) {
        NavBack(color = backColor)
        Text(
            text = title,
            modifier = Modifier.align(Alignment.Center),
            style = TextStyle(
                color = Colors.FF333333,
                fontSize = 18.sp,
                lineHeight = 25.sp,
                fontWeight = FontWeight.SemiBold,
            )
        )
    }
}

@Composable
private fun BoxScope.NavBack(color: Color) {
    Canvas(
        modifier = Modifier
            .align(Alignment.CenterStart)
            .size(44.dp, 44.dp),
    ) {
        drawIntoCanvas { canvas ->
            val paint = Paint()
            paint.color = color
            paint.strokeWidth = 2.5.dp.toPx()
            paint.style = PaintingStyle.Stroke
            paint.strokeCap = StrokeCap.Square

            val path = Path()
            path.moveTo(25f.dp.toPx(), 13f.dp.toPx())
            path.lineTo(16f.dp.toPx(), 22f.dp.toPx())
            path.lineTo(25f.dp.toPx(), 31f.dp.toPx())

            canvas.drawPath(path, paint)
        }
    }
}