import Zookeeper.LeaderElection;
import Zookeeper.OnElectionAction;
import Zookeeper.ServiceRegistry;
import ch.qos.logback.classic.Logger;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Scanner;

public class Application implements Watcher {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(Application.class);
    private static final String address = "192.168.56.115:2181";
    private static final int SESSION_TIMEOUT = 3000; //dead client
    private static final int DEFAULT_PORT = 8080;
    private ZooKeeper zooKeeper;

    public static void main(String[] args) throws Exception {
       // if (args.length != 3) {
       //     logger.error("Expecting parameters <port number> <number of workers> <path to worker jar file>");
        //    System.exit(1);
       // }
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the port of this node");
        int currentServerPort = input.nextInt();
        Application application = new Application();
        ZooKeeper zooKeeper = application.connectToZookeeper();

       // int numberOfWorkers = Integer.parseInt(args[1]);
        //String pathToWorkerProgram = args[2];
        //Autohealer autohealer = new Autohealer(numberOfWorkers, pathToWorkerProgram,zooKeeper);
        //ServiceRegistry serviceRegistry = new ServiceRegistry(zooKeeper,autohealer);
        ServiceRegistry serviceRegistry = new ServiceRegistry(zooKeeper);

        OnElectionAction onElectionAction = new OnElectionAction(serviceRegistry, currentServerPort);

        LeaderElection leaderElection = new LeaderElection(zooKeeper, onElectionAction);
        leaderElection.volunteerForLeadership();
        leaderElection.reelectLeader();

        application.run();
        application.close();

    }

    public ZooKeeper connectToZookeeper() throws IOException {
        this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this);
        return zooKeeper;
    }

    public void run() throws InterruptedException {
        synchronized (zooKeeper) {
            zooKeeper.wait();
        }
    }

    void close() throws InterruptedException {
        this.zooKeeper.close();
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        switch (watchedEvent.getType()) {
            case None:
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    logger.info("Successfully connected to Zookeeper");
                } else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
                    synchronized (zooKeeper) {
                        logger.error("Disconnected from Zookeeper");
                        zooKeeper.notifyAll();
                    }
                } else if (watchedEvent.getState() == Event.KeeperState.Closed) {
                   logger.error ("Closed Successfully");
                }
                break;
        }
    }
}
