package meerkat.voting.gui.panels.vote_have_been_cast;

import javafx.fxml.FXML;
import javafx.scene.input.MouseEvent;
import meerkat.voting.gui.managment.TwoWayNode;
import meerkat.voting.gui.managment.Printer;
import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;
import net.sourceforge.barbecue.output.OutputException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


/**
 * Created by Vladimir Eliezer Tokarev on 8/27/2016.
 * VoteHaveBeenCastController is in charge of handle the vote that have been casted
 */
public class VoteHaveBeenCastController extends TwoWayNode {

    private final Logger logger = LoggerFactory.getLogger(VoteHaveBeenCastController.class);

    /**
     * This method prints the whole VotersBalliot into barcode
     */
    private void createBarCode() throws BarcodeException, OutputException, IOException {
        Barcode barcode = BarcodeFactory.createCode128(this.votersBallot.toString());
        barcode.setFont(null);
        BufferedImage image = BarcodeImageHandler.getImage(barcode);
        File outputfile = new File("VotersBallot.png");
        ImageIO.write(image, "png", outputfile);
        Printer.printBallot("VotersBallot.png");
        this.logger.debug("Created the bar code with voters choises at VotersBallot.png.");
    }

    @FXML
    private void backToWelcomeSplash(MouseEvent mousePress) throws BarcodeException, OutputException, IOException {
        this.createBarCode();
        this.currentStage.close();
        this.currentStage.setScene(this.previous.GetCurrentScene());
        this.previous.UpdateNode();
        this.currentStage.show();
        this.logger.debug("Jumping back to the welcome splash panel.");
    }

    @Override
    public void UpdateNode() {
        // This panel doesn't have any questions representations that why there is nothing ti show here
    }


}
