// 定义 Rect 类型
interface Rect {
    x: number;
    y: number;
    width: number;
    height: number;
}

// 定义 Thickness 类型
interface Thickness {
    left: number;
    top: number;
    right: number;
    bottom: number;
}

// 定义 CornerRadius 类型
interface CornerRadius {
    topLeft: number;
    topRight: number;
    bottomLeft: number;
    bottomRight: number;
}

// 定义 GeometryHelper 类
class GeometryHelper {
    /**
     * 获取带有圆角的矩形 SVG 路径
     * @param baseRect 基础矩形
     * @param thickness 边框厚度
     * @param cornerRadius 圆角半径
     * @returns SVG 路径字符串
     */
    static getRoundRectangle(baseRect: Rect, thickness: Thickness, cornerRadius: CornerRadius): string {
        // 归一化圆角半径
        if (cornerRadius.topLeft < Number.EPSILON)
            cornerRadius.topLeft = 0.0;
        if (cornerRadius.topRight < Number.EPSILON)
            cornerRadius.topRight = 0.0;
        if (cornerRadius.bottomLeft < Number.EPSILON)
            cornerRadius.bottomLeft = 0.0;
        if (cornerRadius.bottomRight < Number.EPSILON)
            cornerRadius.bottomRight = 0.0;

        // 考虑边框厚度
        const leftHalf = thickness.left * 0.5;
        const topHalf = thickness.top * 0.5;
        const rightHalf = thickness.right * 0.5;
        const bottomHalf = thickness.bottom * 0.5;

        // 计算公差
        const tolerance = baseRect.height < baseRect.width ? baseRect.height / baseRect.width : baseRect.width / baseRect.height;

        // 创建四个角的矩形
        const topLeftRect: Rect = {
            x: baseRect.x - tolerance,
            y: baseRect.y - tolerance,
            width: Math.max(0.0, cornerRadius.topLeft - leftHalf),
            height: Math.max(0.0, cornerRadius.topLeft - topHalf)
        };
        const topRightRect: Rect = {
            x: baseRect.x + baseRect.width - cornerRadius.topRight + rightHalf + tolerance,
            y: baseRect.y - tolerance,
            width: Math.max(0.0, cornerRadius.topRight - rightHalf),
            height: Math.max(0.0, cornerRadius.topRight - topHalf)
        };
        const bottomRightRect: Rect = {
            x: baseRect.x + baseRect.width - cornerRadius.bottomRight + rightHalf + tolerance,
            y: baseRect.y + baseRect.height - cornerRadius.bottomRight + bottomHalf + tolerance,
            width: Math.max(0.0, cornerRadius.bottomRight - rightHalf),
            height: Math.max(0.0, cornerRadius.bottomRight - bottomHalf)
        };
        const bottomLeftRect: Rect = {
            x: baseRect.x - tolerance,
            y: baseRect.y + baseRect.height - cornerRadius.bottomLeft + bottomHalf + tolerance,
            width: Math.max(0.0, cornerRadius.bottomLeft - leftHalf),
            height: Math.max(0.0, cornerRadius.bottomLeft - bottomHalf)
        };

        // 调整角矩形的宽高比例
        if (topLeftRect.x + topLeftRect.width > topRightRect.x) {
            const newWidth = (topLeftRect.width / (topLeftRect.width + topRightRect.width)) * baseRect.width;
            topLeftRect.width = newWidth;
            topRightRect.x = baseRect.x + newWidth;
            topRightRect.width = Math.max(0.0, baseRect.width - newWidth);
        }

        if (topRightRect.y + topRightRect.height > bottomRightRect.y) {
            const newHeight = (topRightRect.height / (topRightRect.height + bottomRightRect.height)) * baseRect.height;
            topRightRect.height = newHeight;
            bottomRightRect.y = baseRect.y + newHeight;
            bottomRightRect.height = Math.max(0.0, baseRect.height - newHeight);
        }

        if (bottomRightRect.x < bottomLeftRect.x + bottomLeftRect.width) {
            const newWidth = (bottomLeftRect.width / (bottomLeftRect.width + bottomRightRect.width)) * baseRect.width;
            bottomLeftRect.width = newWidth;
            bottomRightRect.x = baseRect.x + newWidth;
            bottomRightRect.width = Math.max(0.0, baseRect.width - newWidth);
        }

        if (bottomLeftRect.y < topLeftRect.y + topLeftRect.height) {
            const newHeight = (topLeftRect.height / (topLeftRect.height + bottomLeftRect.height)) * baseRect.height;
            topLeftRect.height = newHeight;
            bottomLeftRect.y = baseRect.y + newHeight;
            bottomLeftRect.height = Math.max(0.0, baseRect.height - newHeight);
        }

        // 创建 SVG 路径
        let path = `M ${topLeftRect.x + topLeftRect.width},${topLeftRect.y + topLeftRect.height}`;
        path += ` A ${topLeftRect.width},${topLeftRect.height} 0 0 1 ${topLeftRect.x},${topLeftRect.y + topLeftRect.height}`;
        path += ` L ${topRightRect.x},${topRightRect.y}`;
        path += ` A ${topRightRect.width},${topRightRect.height} 0 0 1 ${topRightRect.x + topRightRect.width},${topRightRect.y}`;
        path += ` L ${bottomRightRect.x + bottomRightRect.width},${bottomRightRect.y}`;
        path += ` A ${bottomRightRect.width},${bottomRightRect.height} 0 0 1 ${bottomRightRect.x + bottomRightRect.width},${bottomRightRect.y + bottomRightRect.height}`;
        path += ` L ${bottomLeftRect.x + bottomLeftRect.width},${bottomLeftRect.y + bottomLeftRect.height}`;
        path += ` A ${bottomLeftRect.width},${bottomLeftRect.height} 0 0 1 ${bottomLeftRect.x},${bottomLeftRect.y + bottomLeftRect.height}`;
        path += ' Z';

        return path;
    }
}

export default GeometryHelper;