const highlightLinks = new Set();
const highlightNodes = new Set();
let hoverNode = null;

const Graph = ForceGraph3D()
    (document.getElementById("three"))
    .jsonUrl('http://127.0.0.1:5000/static/output2.json')
    .nodeColor('#ffffff')
    .nodeLabel('id')
    .nodeRelSize(20)
    .nodeThreeObject(node => {
        const sprite = new SpriteText(node.id);
        sprite.material.depthWrite = false; // make sprite background transparent
        sprite.color = node.color;
        sprite.textHeight = 7.5;
        return sprite;
    })
    
    .linkThreeObject(link => {
        // extend link with text sprite
        const sprite = new SpriteText(`${link.value}`);
        sprite.color = 'blue';
        sprite.textHeight = 1.5;
        return sprite;
      })
      .linkPositionUpdate((sprite, { start, end }) => {
        const middlePos = Object.assign(...['x', 'y', 'z'].map(c => ({
          [c]: start[c] + (end[c] - start[c]) / 2 // calc middle point
        })));

        // Position sprite
        Object.assign(sprite.position, middlePos);
      })    
    

    .onNodeClick(node => {

        var baseUrl = "page/" + node.id;
        window.location.href = baseUrl;  
    })

    .onNodeHover(node => {
        // ノードにホバーした時の処理
        hoverNode = node ? node.id : null;
        updateHoverText(); // テキストを更新する関数を呼び出す
    })
    .linkOpacity(0.25)
    .linkDirectionalArrowLength(3)
    .linkDirectionalArrowRelPos(1)
    .linkCurvature(0.01)
    .linkDirectionalParticleWidth(2)
    .linkDirectionalParticles("value")
    .linkDirectionalParticleSpeed(d => d.value * 0.01)
    .linkThreeObjectExtend(true)
    .linkColor(link=>{
        if (link.value > 0){
            return 'red'
        } else {
            return 'blue'
        }
    })
    .linkWidth(1)
    .backgroundColor("#f8f8f8");

function updateHoverText() {
    // ホバー時のノードのテキストサイズを変更
    Graph.nodeThreeObject(node => {
        const sprite = new SpriteText(node.id);
        sprite.material.depthWrite = false;
        sprite.color = node.color;
        sprite.textHeight = hoverNode === node.id ? 15 : 7.5; // ホバー時にテキストサイズを変更
        return sprite;
    });
}