package app.dudu.design

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * 线条图标（对齐 Lucide 风格：2px 圆头描边、24 网格比例），用 Canvas 手绘，
 * 避免引入图标库依赖；颜色跟随传入 color（等价 currentColor）。
 * 设计规则：不用 emoji、不用 unicode 符号作图标。
 */

private fun DrawScope.lineStroke(w: Float): Stroke =
    Stroke(width = w, cap = StrokeCap.Round, join = StrokeJoin.Round)

private fun DrawScope.strokeW(side: Float): Float = side * 2f / 24f

/** dudu logo mark：圆环（嘟起的嘴）+ 三条声波竖线。 */
@Composable
fun DuduLogoMark(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = s * 3.5f / 24f // logo 用 3.5px/24 笔画
        val stroke = lineStroke(w)
        drawCircle(color = color, radius = s * 0.42f, center = center, style = stroke)
        val bar = { x: Float, h: Float ->
            drawLine(color, Offset(s * x, center.y - h / 2), Offset(s * x, center.y + h / 2), w, StrokeCap.Round)
        }
        bar(0.36f, s * 0.18f)
        bar(0.50f, s * 0.34f)
        bar(0.64f, s * 0.18f)
    }
}

/** 麦克风。 */
@Composable
fun IconMic(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val stroke = lineStroke(w)
        // 话筒身：圆角胶囊
        val bodyW = s * 0.25f
        drawRoundRect(
            color = color,
            topLeft = Offset(center.x - bodyW / 2, s * 0.08f),
            size = Size(bodyW, s * 0.5f),
            cornerRadius = androidx.compose.ui.geometry.CornerRadius(bodyW / 2),
            style = stroke,
        )
        // 拾音弧
        val arcR = s * 0.29f
        drawArc(
            color = color,
            startAngle = 0f,
            sweepAngle = 180f,
            useCenter = false,
            topLeft = Offset(center.x - arcR, s * 0.42f - arcR),
            size = Size(arcR * 2, arcR * 2),
            style = stroke,
        )
        // 支杆
        drawLine(color, Offset(center.x, s * 0.71f), Offset(center.x, s * 0.92f), w, StrokeCap.Round)
    }
}

/** 对勾。 */
@Composable
fun IconCheck(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val p = Path().apply {
            moveTo(s * 0.17f, s * 0.55f)
            lineTo(s * 0.40f, s * 0.78f)
            lineTo(s * 0.83f, s * 0.25f)
        }
        drawPath(p, color, style = lineStroke(w))
    }
}

/** 上箭头。 */
@Composable
fun IconArrowUp(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        drawLine(color, Offset(center.x, s * 0.17f), Offset(center.x, s * 0.83f), w, StrokeCap.Round)
        val p = Path().apply {
            moveTo(s * 0.25f, s * 0.42f)
            lineTo(center.x, s * 0.17f)
            lineTo(s * 0.75f, s * 0.42f)
        }
        drawPath(p, color, style = lineStroke(w))
    }
}

/** 地球（切换输入法）。 */
@Composable
fun IconGlobe(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val stroke = lineStroke(w)
        drawCircle(color, radius = s * 0.4f, center = center, style = stroke)
        drawLine(color, Offset(s * 0.1f, center.y), Offset(s * 0.9f, center.y), w, StrokeCap.Round)
        drawOval(
            color = color,
            topLeft = Offset(center.x - s * 0.18f, s * 0.1f),
            size = Size(s * 0.36f, s * 0.8f),
            style = stroke,
        )
    }
}

/** 退格。 */
@Composable
fun IconBackspace(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val stroke = lineStroke(w)
        val body = Path().apply {
            moveTo(s * 0.36f, s * 0.2f)
            lineTo(s * 0.88f, s * 0.2f)
            lineTo(s * 0.88f, s * 0.8f)
            lineTo(s * 0.36f, s * 0.8f)
            lineTo(s * 0.08f, s * 0.5f)
            close()
        }
        drawPath(body, color, style = stroke)
        drawLine(color, Offset(s * 0.5f, s * 0.38f), Offset(s * 0.72f, s * 0.62f), w, StrokeCap.Round)
        drawLine(color, Offset(s * 0.72f, s * 0.38f), Offset(s * 0.5f, s * 0.62f), w, StrokeCap.Round)
    }
}

/** 回车（corner-down-left）。 */
@Composable
fun IconEnter(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val stroke = lineStroke(w)
        val p = Path().apply {
            moveTo(s * 0.83f, s * 0.21f)
            lineTo(s * 0.83f, s * 0.46f)
            quadraticTo(s * 0.83f, s * 0.62f, s * 0.67f, s * 0.62f)
            lineTo(s * 0.17f, s * 0.62f)
        }
        drawPath(p, color, style = stroke)
        val head = Path().apply {
            moveTo(s * 0.38f, s * 0.42f)
            lineTo(s * 0.17f, s * 0.62f)
            lineTo(s * 0.38f, s * 0.83f)
        }
        drawPath(head, color, style = stroke)
    }
}

/** 设置（sliders-horizontal）。 */
@Composable
fun IconSliders(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val stroke = lineStroke(w)
        drawLine(color, Offset(s * 0.1f, s * 0.33f), Offset(s * 0.9f, s * 0.33f), w, StrokeCap.Round)
        drawLine(color, Offset(s * 0.1f, s * 0.67f), Offset(s * 0.9f, s * 0.67f), w, StrokeCap.Round)
        drawCircle(color, radius = s * 0.1f, center = Offset(s * 0.62f, s * 0.33f), style = stroke)
        drawCircle(color, radius = s * 0.1f, center = Offset(s * 0.38f, s * 0.67f), style = stroke)
    }
}

/** 右箭头（列表行入口）。 */
@Composable
fun IconChevronRight(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val p = Path().apply {
            moveTo(s * 0.38f, s * 0.2f)
            lineTo(s * 0.66f, s * 0.5f)
            lineTo(s * 0.38f, s * 0.8f)
        }
        drawPath(p, color, style = lineStroke(w))
    }
}

/** 左箭头（返回）。 */
@Composable
fun IconChevronLeft(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val p = Path().apply {
            moveTo(s * 0.62f, s * 0.2f)
            lineTo(s * 0.34f, s * 0.5f)
            lineTo(s * 0.62f, s * 0.8f)
        }
        drawPath(p, color, style = lineStroke(w))
    }
}

/** 加号（image-plus 的简化形：添加图片框内用）。 */
@Composable
fun IconPlus(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        drawLine(color, Offset(center.x, s * 0.2f), Offset(center.x, s * 0.8f), w, StrokeCap.Round)
        drawLine(color, Offset(s * 0.2f, center.y), Offset(s * 0.8f, center.y), w, StrokeCap.Round)
    }
}

/** 叉（删除图片）。 */
@Composable
fun IconX(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        drawLine(color, Offset(s * 0.25f, s * 0.25f), Offset(s * 0.75f, s * 0.75f), w, StrokeCap.Round)
        drawLine(color, Offset(s * 0.75f, s * 0.25f), Offset(s * 0.25f, s * 0.75f), w, StrokeCap.Round)
    }
}

/** 用户（账号卡头像占位）。 */
@Composable
fun IconUser(size: Dp, color: Color, modifier: Modifier = Modifier) {
    Canvas(modifier.size(size)) {
        val s = this.size.minDimension
        val w = strokeW(s)
        val stroke = lineStroke(w)
        drawCircle(color, radius = s * 0.18f, center = Offset(center.x, s * 0.32f), style = stroke)
        drawArc(
            color = color,
            startAngle = 180f,
            sweepAngle = 180f,
            useCenter = false,
            topLeft = Offset(s * 0.2f, s * 0.58f),
            size = Size(s * 0.6f, s * 0.62f),
            style = stroke,
        )
    }
}
