package FLSETAlgorithm;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

public class Node extends Thread {
    private int id, rounds, decision, f;
    private volatile HashSet<Integer> window;
    private ArrayList<Node> network;
    private ReentrantLock lock;

    public Node(int id, int initial_window, int f) {
        this.id = id;
        this.rounds = 0;
        this.decision = -1; // It represents that the value is not known
        this.window = new HashSet<>();
        window.add(initial_window);
        this.f = f;
        this.lock = new ReentrantLock();
    }

    public ReentrantLock getLock() {
        return this.lock;
    }

    public void setNetwork(ArrayList<Node> network) {
        this.network = network;
    }

    public synchronized void sendWindow() {
        for (int i = 0; i < network.size(); i++) {
            if (i != id) {
                network.get(i).getLock().lock();
                for (Integer value : window) {
                    network.get(i).window.add(value);
                }
                network.get(i).getLock().unlock();
            }
        }
    }

    public int minValueWindow() {
        int min_value = 10000000;

        for (Integer value : window) {
            if (value < min_value) {
                min_value = value;
            }
        }
        return min_value;
    }

    public void run() {
        Random rand = new Random();
        while (rounds != f + 1 && this.window.size() != this.network.size()) {
            // the size approach only works as i know theyre only sending one message
            int failing_chance = rand.nextInt(5);
            if (failing_chance < 3) {
                System.out.println("Thread with id " + id + " crashed on round " + rounds +
                        "\nHe will be back in the next one.");
            } else {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sendWindow();
            }
            rounds++;
        }
        this.decision = minValueWindow();
        System.out.println("The value decision of " + id + " is: " + decision);

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (id == 1) {
            this.lock.lock();
            System.out.println("The window values are: ");
            for (Integer value : window) {
                System.out.print(value + " ");
            }
            this.lock.unlock();

        }
    }
}
