
import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.stream.file.FileSourceEdge;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SimulationPropagation2 {

    public static void main(String ... args) {
        List<Node> lstNodeInfect = new ArrayList<>();

        String filePath = "src/data.txt";
        Graph g = new DefaultGraph("g");
        FileSourceEdge fs = new FileSourceEdge(true);

        fs.addSink(g);

        try {
            fs.begin(filePath);

            while (fs.nextEvents()) {
                // Optionally some code here ...
            }
        } catch( IOException e) {
            e.printStackTrace();
        }

        try {
            fs.end();
        } catch( IOException e) {
            e.printStackTrace();
        } finally {
            fs.removeSink(g);
        }

        for(Node n : g){
            if(Math.random() < 1/2.0){
                n.addAttribute("etat","Immuniser");
            }else{
                n.addAttribute("etat","susceptible");
            }

        }

        int nbInfect = SimulationPropagation2.nbSusceptible(g);
        Node nTmp = g.getNode((int)(Math.random()*g.getNodeCount()));
        //Node nTmp = g.getNode(1);
        lstNodeInfect.add(nTmp);
        nTmp.setAttribute("etat","infecter");

        // 84 est le nombre total de jours en 3 mois ( 28*3 )
        for(int i = 0; i<84; i++){
            List<Node> lstTmpInfect = new ArrayList<>(lstNodeInfect);
            for(Node n : lstTmpInfect){
                if(Math.random() < 1/7.0 ){
                    for(Edge e : n.getLeavingEdgeSet()){
                        Node voisin = e.getOpposite(n);
                        if (voisin.getAttribute("etat").equals("susceptible")) {
                            lstNodeInfect.add(voisin);
                            voisin.setAttribute("etat","infecter");
                        }
                    }
                }
                if(Math.random() < 1/14.0){
                    n.setAttribute("etat","susceptible");
                    lstNodeInfect.remove(n);
                }
            }
            System.out.println((lstNodeInfect.size()*100.0)/nbInfect);
        }

    }

    public static int nbSusceptible(Graph g){

        int nbSusceptible = 0;

        for(Node n : g){
            if(n.getAttribute("etat").equals("susceptible")){
                nbSusceptible++;
            }
        }
        return nbSusceptible;
    }

}
