package meerkat.voting.gui;

import com.google.common.util.concurrent.FutureCallback;
import meerkat.crypto.DigitalSignature;
import meerkat.crypto.concrete.ECDSADeterministicSignature;
import meerkat.pollingstation.PollingStationScanner;
import meerkat.pollingstation.PollingStationWebScanner;
import meerkat.protobuf.PollingStation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Laura on 2/1/2017.
 */
public class PollingStationServerToyRun {
    final Logger logger = LoggerFactory.getLogger(getClass());
    private static final String SCAN_CONTEXT_PATH = "/scan";

    private PollingStationScanner.PollingStationServer scannerServer;
    PollingStation.ConnectionServerData serverData;
    DigitalSignature verifier;

    public void init() {

        verifier = new ECDSADeterministicSignature();

        logger.debug("Setting up Scanner WebApp!");

        scannerServer = new PollingStationWebScanner(0, SCAN_CONTEXT_PATH);

        scannerServer.subscribe(new FutureCallback<PollingStation.ScannedData>() {
            @Override
            public void onSuccess(PollingStation.ScannedData result) {
                logger.info("Received scan #{}", result.getSerial());
            }

            @Override
            public void onFailure(Throwable t) { }
        });

        try {
            serverData = scannerServer.start(false);
            logger.info("Scanner server started at {}", serverData.getServerUrl());
        } catch (Exception e) {
            logger.error("Could not start server: " + e.getMessage());
        }
    }

    public static void main(String [] args) {
        PollingStationServerToyRun run = new PollingStationServerToyRun();
        run.init();
    }
}