package com.pixelized.biblib.utils.extention.modifier

import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.withTransform
import androidx.compose.ui.text.*
import kotlin.math.sqrt

@OptIn(ExperimentalTextApi::class)
fun Modifier.drawDiagonalLabel(
    text: String,
    color: Color,
    style: TextStyle,
    labelTextRatio: Float,
) = clipToBounds().composed(
    factory = {
        val textMeasurer = rememberTextMeasurer()
        val textLayoutResult: TextLayoutResult = remember {
            textMeasurer.measure(text = AnnotatedString(text), style = style)
        }
        drawWithContent {
            val canvasWidth = size.width

            val textSize = textLayoutResult.size
            val textWidth = textSize.width
            val textHeight = textSize.height

            val rectWidth = textWidth * labelTextRatio * 1.5f

            val rect = Rect(
                offset = Offset(canvasWidth - rectWidth, 0f),
                size = Size(rectWidth, textHeight.toFloat())
            )

            val sqrt = sqrt(rectWidth / 2f)
            val translatePos = sqrt * sqrt

            drawContent()
            withTransform(
                transformBlock = {
                    rotate(
                        degrees = 45f,
                        pivot = Offset(
                            canvasWidth - rectWidth / 2,
                            translatePos
                        )
                    )
                },
                drawBlock = {
                    drawRect(
                        color = color,
                        topLeft = rect.topLeft,
                        size = rect.size
                    )
                    drawText(
                        textMeasurer = textMeasurer,
                        text = text,
                        style = style,
                        topLeft = Offset(
                            rect.left + (rectWidth - textWidth) / 2f,
                            rect.top + (rect.bottom - textHeight) / 2f
                        )
                    )
                }
            )
        }
    }
)


