package com.pixelized.desktop.lwa.ui.theme.shapes

import androidx.compose.runtime.Stable
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection

@Stable
class ArrowShape(
    private val core: Dp,
    private val head: Dp,
) : Shape {

    override fun createOutline(
        size: Size,
        layoutDirection: LayoutDirection,
        density: Density,
    ): Outline {
        return Outline.Generic(
            path = drawArrowPath(
                size = size,
                core = with(density) { core.toPx() },
                head = with(density) { head.toPx() },
            )
        )
    }

    private fun drawArrowPath(
        size: Size,
        core: Float,
        head: Float,
    ): Path {
        return Path().apply {
            reset()

            moveTo(
                x = size.width / 2f,
                y = 0f,
            )

            lineTo(
                x = size.width / 2f,
                y = 0f,
            )

            lineTo(
                x = size.width / 2f + core + head,
                y = core + head,
            )

            lineTo(
                x = size.width / 2f + core,
                y = core + head,
            )

            lineTo(
                x = size.width / 2f + core,
                y = size.height,
            )

            lineTo(
                x = size.width / 2f - core,
                y = size.height,
            )

            lineTo(
                x = size.width / 2f - core,
                y = core + head,
            )

            lineTo(
                x = size.width / 2f - core - head,
                y = core + head,
            )

            close()
        }
    }
}