package Zookeeper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Worker {
    private String nodeName;
    private String ipAddress;
    private String userName;
    private int  JobsNum=0;

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Worker(String fullPathNode) {
        this.nodeName = fullPathNode;
    }

    public String getNodeName() {
        return nodeName;
    }

    public int getJobsNum() {
        return JobsNum;
    }
    public synchronized  void  increaseOne(){
        JobsNum++;
    }
    public static List<String> getIpAddress(List<Worker> workers){
        List<String> allAddress= new ArrayList<>();
        for (Worker worker:workers) {
            allAddress.add(worker.ipAddress);
        }
        return allAddress;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public String getUserName() {
        return userName;
    }
    public static void SortNodes(List<Worker> workers) {
        // Custom comparator to sort based on the size of the JobsNode list
        Comparator<Worker> byJobsListSize = Comparator.comparingInt(worker -> worker.getJobsNum());

        // Sorting the list using the custom comparator
        Collections.sort(workers, byJobsListSize);
    }
    public static Worker findByNodeName(String nodeName, List<Worker> workers){
        for (Worker worker:workers) {
            if(worker.getNodeName().equals(nodeName)) {
                return worker;
            }
        }
        return null;
    }

    public void setnumOfJobsZero() {
        JobsNum=0;
    }
    public static int getTotalNumOfJob(List<Worker> workers){
        int sum=0;
        for (Worker worker:workers) {
            sum+=worker.JobsNum;
        }
        return sum;
    }
}
