// src/helpers/helper.js

// ===============================
// 坐标转换工具函数集 (coordConverter.js)
// ===============================


// ======== 坐标转换 ========
/**
 * 检测区域、子区域 矩形转换 雷达坐标系 --> 画布坐标系
 * @param {Object} src_rect - 雷达坐标系矩形
 * @param {Object} p_radar - 画布中雷达的位置
 * @returns {Object} CSS 可用矩形
 */
export function convert_region_r2c(src_rect, p_radar) {
    //   console.log('src_rect: ', src_rect)
    const left = p_radar.x + src_rect.x_cm_start;
    const top = p_radar.y - src_rect.y_cm_stop;
    const width = src_rect.x_cm_stop - src_rect.x_cm_start;
    const height = src_rect.y_cm_stop - src_rect.y_cm_start;

    return { left, top, width, height };
}

/**
 * 检测区域、子区域 矩形转换 画布坐标系 --> 雷达坐标系
 * @param {Object} dst_rect - 画布矩形
 * @param {Object} p_radar - 雷达在画布坐标系中的位置
 * @returns {Object} 雷达坐标矩形
 */
export function convert_region_c2r(dst_rect, p_radar) {
    const x_cm_start = dst_rect.left - p_radar.x;
    const x_cm_stop = x_cm_start + dst_rect.width;
    const y_cm_stop = p_radar.y - dst_rect.top;
    const y_cm_start = y_cm_stop - dst_rect.height;

    return {
        x_cm_start,
        x_cm_stop,
        y_cm_start,
        y_cm_stop
    };
}

/**
 * 家具 矩形转换 雷达坐标系 --> 画布坐标系
 * @param {Object} furniture - 雷达坐标家具对象
 * @param {Object} p_radar - 雷达在画布上的坐标
 * @returns {Object} CSS 可用家具矩形
 */
export function convert_furniture_r2c(furniture, p_radar) {
    const left = p_radar.x + furniture.x;
    const top = p_radar.y - furniture.y;
    const width = furniture.width;
    const height = furniture.length;
    //   console.log('convert_furniture_r2c', p_radar, furniture, {left, top, width, height})

    return {
        left,
        top,
        width,
        height
    };
}

/**
 * 家具 矩形转换 画布坐标系 --> 雷达坐标系
 * @param {Object} furniture_item - 画布矩形（CSS）
 * @param {Object} p_radar - 雷达在画布上的坐标
 * @returns {Object} 雷达坐标家具矩形
 */
export function convert_furniture_c2r(furniture_item, p_radar) {
    const x = furniture_item.left - p_radar.x;
    const y = p_radar.y - furniture_item.top;
    const width = furniture_item.width;
    const height = furniture_item.height;

    return {
        x,
        y,
        width,
        height
    };
}


/**
 * 将雷达坐标系中的点转换到画布坐标系（旋转 + 平移）
 * @param {{x: number, y: number}} src_point - 雷达坐标系中的点
 * @param {{x: number, y: number}} pRadar - 雷达在画布坐标系中的坐标
 * @param {number} angle - 雷达顺时针旋转角度（度）
 * @returns {{x: number, y: number}} - 画布坐标系中的点
 */
export function convert_point_r2c(src_point, pRadar, angle) {
    const rad = (angle * Math.PI) / 180; // 角度转弧度（顺时针为正）
    const cosA = Math.cos(rad);
    const sinA = Math.sin(rad);

    // 顺时针旋转
    const xRot = src_point.x * cosA + src_point.y * sinA;
    const yRot = -src_point.x * sinA + src_point.y * cosA;

    // 平移到画布坐标系
    return {
        x: pRadar.x + xRot,
        y: pRadar.y - yRot,
    };
}




// ======== 旋转 ========

/**
 * 顺时针旋转矩形（家具）
 * @param {Object} src_rect - 输入矩形 { left, top, width, height }
 * @param {Object} pRadar - 雷达中心坐标 { x, y }
 * @param {number} angle - 顺时针旋转角度 0, 90, 180, 270
 * @returns {Object} 旋转后的矩形 { left, top, width, height }
 */
export function rotateRect_cw(src_rect, pRadar, angle) {
    if (![0, 90, 180, 270].includes(angle)) angle = 0

    const { left, top, width, height } = src_rect
    const cx = left + width / 2
    const cy = top + height / 2

    let dx = cx - pRadar.x
    let dy = cy - pRadar.y

    let new_dx, new_dy

    switch (angle) {
        case 0:
            new_dx = dx
            new_dy = dy
            break
        case 90: // 顺时针 90°
            new_dx = -dy
            new_dy = dx
            break
        case 180:
            new_dx = -dx
            new_dy = -dy
            break
        case 270: // 顺时针 270° = 逆时针 90°
            new_dx = dy
            new_dy = -dx
            break
    }

    const new_cx = pRadar.x + new_dx
    const new_cy = pRadar.y + new_dy

    // 对于 90/270 旋转，宽高交换
    let new_width = width
    let new_height = height
    if (angle === 90 || angle === 270) {
        new_width = height
        new_height = width
    }

    return {
        left: new_cx - new_width / 2,
        top: new_cy - new_height / 2,
        width: new_width,
        height: new_height
    }
}

/**
 * 逆时针旋转矩形（家具/检测区域）
 * @param {Object} src_rect - 输入矩形 { left, top, width, height }
 * @param {Object} pRadar - 雷达中心坐标 { x, y }
 * @param {number} angle - 逆时针旋转角度 0, 90, 180, 270
 * @returns {Object} 旋转后的矩形 { left, top, width, height }
 */
export function rotateRect_ccw(src_rect, pRadar, angle) {
    if (![0, 90, 180, 270].includes(angle)) angle = 0;
    const { left, top, width, height } = src_rect;
    const cx = left + width / 2;
    const cy = top + height / 2;

    const dx = cx - pRadar.x;
    const dy = cy - pRadar.y;

    let new_dx, new_dy;

    switch (angle) {
        case 0:
            new_dx = dx;
            new_dy = dy;
            break;
        case 90: // 逆时针 90°
            new_dx = dy;
            new_dy = -dx;
            break;
        case 180:
            new_dx = -dx;
            new_dy = -dy;
            break;
        case 270: // 逆时针 270° = 顺时针 90°
            new_dx = -dy;
            new_dy = dx;
            break;
    }

    const new_cx = pRadar.x + new_dx;
    const new_cy = pRadar.y + new_dy;

    let new_width = width;
    let new_height = height;
    if (angle === 90 || angle === 270) {
        new_width = height;
        new_height = width;
    }

    return {
        left: new_cx - new_width / 2,
        top: new_cy - new_height / 2,
        width: new_width,
        height: new_height
    };
}



/**
 * 坐标系：x正轴朝右→， y正轴朝上↑
 * 将 src_point 绕 point 旋转 angle 度
 * @param {{x: number, y: number}} src_point - 原始点
 * @param {{x: number, y: number}} point - 旋转中心点
 * @param {number} angle - 旋转角度（度数）
 * @returns {{x: number, y: number}} - 旋转后的点
 */
export function rotatePoint(src_point, point, angle) {
    // console.log('rotatePoint 输入:', { src_point, point, angle });
    const rad = (-angle * Math.PI) / 180; // 顺时针旋转
    const cosA = Math.cos(rad);
    const sinA = Math.sin(rad);

    const x = src_point.x - point.x;
    const y = src_point.y - point.y;

    const xRot = x * cosA - y * sinA;
    const yRot = x * sinA + y * cosA;

    const result = {
        x: parseFloat((xRot + point.x).toFixed(6)),
        y: parseFloat((yRot + point.y).toFixed(6)),
    };

    // console.log('rotatePoint 返回:', result);
    return result;
}

