import colors from "../colors"

/**
 * <p>결과적용</p>
 * @param 
 * @param 
 * @return
 */
export function fn_getRegions(result, regionClsList){
    let regions = [];
    if(result){
        result = JSON.parse(result);
        const shapes   = result.shapes;
        const scaleX   = result.imageWidth; 
        const scaleY   = result.imageHeight;
        let   clsIndex = -1;

        for(var idx_shapes = 0; idx_shapes < shapes.length; idx_shapes++){
            for(var idx = 0; idx < regionClsList.length; idx++){
                if(regionClsList[idx] === shapes[idx_shapes].label){
                    clsIndex = idx;
                    break;
                }
            }

            if(shapes[idx_shapes].shape_type === "rectangle"){
                regions[idx_shapes] = drawBoxRegion(shapes[idx_shapes], clsIndex, scaleX, scaleY); 
            }
            else if(shapes[idx_shapes].shape_type === "point"){
                regions[idx_shapes] = drawPointRegion(shapes[idx_shapes], clsIndex, scaleX, scaleY); 
            }
            else if(shapes[idx_shapes].shape_type === "polygon"){
                regions[idx_shapes] = drawPolygonRegion(shapes[idx_shapes], clsIndex, scaleX, scaleY); 
            }
        }
    }

    return regions;
}

/**
 * <p>결과적용</p>
 * @param 
 * @param 
 * @return
 */
export function fn_getImages(images, results, regionClsList){
    let rtn_images    = [];
    let named_results = {};

    for(let idx_results = 0; idx_results < results.length; idx_results++){
        if(results[idx_results]){
            const result = JSON.parse(results[idx_results]);
            const file_nm = result.imagePath.substring(0, result.imagePath.lastIndexOf('.'));
            named_results[file_nm] = result;
        }
    }

    for(let idx_images = 0; idx_images < images.length; idx_images++){
        let image = images[idx_images];
        if(Object.keys(named_results).length > 0){
            const result = named_results[images[idx_images].name.substring(0, images[idx_images].name.lastIndexOf('.'))];
            if(result){
                const shapes   = result.shapes;
                const scaleX   = result.imageWidth; 
                const scaleY   = result.imageHeight;
                let   clsIndex = -1;
                let   regions  = [];

                for(var idx_shapes = 0; idx_shapes < shapes.length; idx_shapes++){
                    for(var idx = 0; idx < regionClsList.length; idx++){
                        if(regionClsList[idx] === shapes[idx_shapes].label){
                            clsIndex = idx;
                            break;
                        }
                    }
    
                    if(shapes[idx_shapes].shape_type === "rectangle"){
                        regions[idx_shapes] = drawBoxRegion(shapes[idx_shapes], clsIndex, scaleX, scaleY); 
                    }
                    else if(shapes[idx_shapes].shape_type === "point"){
                        regions[idx_shapes] = drawPointRegion(shapes[idx_shapes], clsIndex, scaleX, scaleY); 
                    }
                    else if(shapes[idx_shapes].shape_type === "polygon"){
                        regions[idx_shapes] = drawPolygonRegion(shapes[idx_shapes], clsIndex, scaleX, scaleY); 
                    }
                }
                image.regions = regions;
            }
        }

        rtn_images[idx_images] = image;
    }

    return rtn_images;
}

const drawBoxRegion = (shape, clsIndex, scaleX, scaleY) => {
    const xmin   = shape.points[0][0];
    const ymin   = shape.points[0][1];
    const xmax   = shape.points[1][0];
    const ymax   = shape.points[1][1];

    const x_min = xmin / scaleX;
    const y_min = ymin / scaleY;
    const x_max = xmax / scaleX;
    const y_max = ymax / scaleY;

    const _region = {
          cls  : shape.label
        , tags : shape.tags
        , type : 'box'
        , color: clsIndex !== -1 ? colors[clsIndex % colors.length] : colors[0 % colors.length]
        , id   : Math.random().toString().split(".")[1]
        , w    : x_max - x_min
        , h    : y_max - y_min
        , x    : x_min
        , y    : y_min
    };

    return _region;
}

const drawPointRegion = (shape, clsIndex, scaleX, scaleY) => {
    const x = shape.points[0];
    const y = shape.points[1];

    const _region = {
          cls  : shape.label
        , tags : shape.tags
        , type : 'point'
        , color: clsIndex !== -1 ? colors[clsIndex % colors.length] : colors[0 % colors.length]
        , id   : Math.random().toString().split(".")[1]
        , x    : x / scaleX
        , y    : y / scaleY
    };

    return _region;
}

const drawPolygonRegion = (shape, clsIndex, scaleX, scaleY) => {
    const node_arr_point = shape.points;
    let   points = [];

    for(var i = 0; i < node_arr_point.length; i++){
        const x = node_arr_point[i][0];
        const y = node_arr_point[i][1];

        let point = [];
        point.push(x / scaleX);
        point.push(y / scaleY);
        points.push(point);
    }

    const _region = {
          cls    : shape.label
        , tags : shape.tags
        , type   : 'polygon'
        , color  : clsIndex !== -1 ? colors[clsIndex % colors.length] : colors[0 % colors.length]
        , id     : Math.random().toString().split(".")[1]
        , points : points
    };

    return _region;
}