import * as config from './config.js';

export function createDistanceMatrix(links, size) {
    let M = Array(size).fill(null).map(() => Array(size).fill(0));
    for (const link of links) {
        let source = typeof link.source.id === 'number' ? link.source.id : link.source;
        let target = typeof link.target.id === 'number' ? link.target.id : link.target;
        let distance = link.distance*200;
        M[source][target] = distance;
        M[target][source] = distance;
    }
    return M;
}

export function getBoundingBox(textElement) {
    const {x, y, width, height} = textElement.getBBox();
    return {x: x-2.5, y: y-2.5, width: width+5, height: height+5, arr: [width+5, height+5]};
}

export function filterLinksByWord(link, word) {
    return link.similarity != 0 && (link.source.id == word.id || link.target.id == word.id);
}

export function getNeighborsSortedBySimilarity(links, d) {
    return links.filter(l => (l.source.id == d.id || l.target.id == d.id) && l.similarity > 0).sort((a,b) => b.similarity - a.similarity).map(
        l => l.source.id == d.id ? l.target : l.source
    );
}

let color = d3.scaleOrdinal(d3.schemeDark2);

export function getColor(index) {
    if (config.COLOR) {
        return color(index);
    } else {
        return "black";
    }
}

export function lowlight(element) {
    element.style("fill", "black").style("opacity", 0.5);
}

export function dehighlight(element) {
    element.style("fill", d => { return getColor(d.id); }).style("opacity", 1.0);
}

export function highlight(element) {
    element.style("fill", "red").style("opacity", 1.0);
}

export function highlightBoundingBox(element) {
    element.style("stroke", "red");
    element.style("opacity", 1);
}

export function dehighlightBoundingBox(element) {
    element.style("stroke", "black");
    element.style("opacity", 0);
}

export function show(element) {
    element.style("visibility", "visible");
}

export function highlight_edge(element) {
    element.style("stroke", "red");
}

export function highlightEdgeColor(element, color) {
    element.style("stroke", color);
}

export function dehighlight_edge(element) {
    element.style("stroke", "#999");
    element.style("stroke-opacity", 0.6);
}

export function hide(element) {
    element.style("visibility", "hidden");
}

export const emptyRectangle = {x: 0, y: 0, width: 0, height: 0, arr: [0, 0]};

export function generateGridPoints(left, top, cellWidth, cellHeight, columns, rows) {
    let points = [];
    for (let i = 0; i < columns; i++) {
        for (let j = 0; j < rows; j++) {
            points.push({x: left + i * cellWidth, y: top + j * cellHeight});
        }
    }
    return points;
}

export function generateGridCenterPoints(left, top, cellWidth, cellHeight, columns, rows) {
    let points = [];
    for (let i = 0; i < columns; i++) {
        for (let j = 0; j < rows; j++) {
            points.push({x: left + i * cellWidth + cellWidth/2, y: top + j * cellHeight + cellHeight/2});
        }
    }
    return points;
}

export function getAverage(array) {
    return array.reduce((a, b) => a + b) / array.length || 0;
}

export function getNode(id) {
    return d3.select("#word"+id).node();
}
