import UIKit

extension BubbleGeometryEngine {
    static func addFigmaTail(
        to path: UIBezierPath,
        bodyRect: CGRect,
        outgoing: Bool,
        tailProtrusion: CGFloat
    ) {
        let svgStraightX: CGFloat = 5.59961
        let svgMaxY: CGFloat = 33.2305
        let scale = tailProtrusion / svgStraightX
        let tailHeight = svgMaxY * scale

        let bodyEdge = outgoing ? bodyRect.maxX : bodyRect.minX
        let bottom = bodyRect.maxY
        let top = bottom - tailHeight
        let direction: CGFloat = outgoing ? 1 : -1

        func point(_ svgX: CGFloat, _ svgY: CGFloat) -> CGPoint {
            let dx = (svgStraightX - svgX) * scale * direction
            return CGPoint(x: bodyEdge + dx, y: top + svgY * scale)
        }

        if outgoing {
            path.move(to: point(5.59961, 24.2305))
            path.addCurve(
                to: point(0, 33.0244),
                controlPoint1: point(5.42042, 28.0524),
                controlPoint2: point(3.19779, 31.339)
            )
            path.addCurve(
                to: point(2.6123, 33.2305),
                controlPoint1: point(0.851596, 33.1596),
                controlPoint2: point(1.72394, 33.2305)
            )
            path.addCurve(
                to: point(13.0293, 29.5596),
                controlPoint1: point(6.53776, 33.2305),
                controlPoint2: point(10.1517, 31.8599)
            )
            path.addCurve(
                to: point(7.57422, 23.1719),
                controlPoint1: point(10.7434, 27.898),
                controlPoint2: point(8.86922, 25.7134)
            )
            path.addCurve(
                to: point(5.6123, 4.2002),
                controlPoint1: point(5.61235, 19.3215),
                controlPoint2: point(5.6123, 14.281)
            )
            path.addLine(to: point(5.6123, 0))
            path.addLine(to: point(5.59961, 0))
            path.addLine(to: point(5.59961, 24.2305))
            path.close()
        } else {
            path.move(to: point(5.59961, 24.2305))
            path.addLine(to: point(5.59961, 0))
            path.addLine(to: point(5.6123, 0))
            path.addLine(to: point(5.6123, 4.2002))
            path.addCurve(
                to: point(7.57422, 23.1719),
                controlPoint1: point(5.6123, 14.281),
                controlPoint2: point(5.61235, 19.3215)
            )
            path.addCurve(
                to: point(13.0293, 29.5596),
                controlPoint1: point(8.86922, 25.7134),
                controlPoint2: point(10.7434, 27.898)
            )
            path.addCurve(
                to: point(2.6123, 33.2305),
                controlPoint1: point(10.1517, 31.8599),
                controlPoint2: point(6.53776, 33.2305)
            )
            path.addCurve(
                to: point(0, 33.0244),
                controlPoint1: point(1.72394, 33.2305),
                controlPoint2: point(0.851596, 33.1596)
            )
            path.addCurve(
                to: point(5.59961, 24.2305),
                controlPoint1: point(3.19779, 31.339),
                controlPoint2: point(5.42042, 28.0524)
            )
            path.close()
        }
    }
}
