package meerkat.voting.gui.panels.ballot_summary;

import com.google.protobuf.ByteString;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import meerkat.voting.gui.managment.VotersBallot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by Vladimir Eliezer Tokarev on 10/5/2016.
 * This object add all the information inputed by the voter and displays it in the right section
 */
class VotersChoicesAdder {

    private final Logger logger = LoggerFactory.getLogger(VotersChoicesAdder.class);
    private Stage currentStage;
    private int rowIndex;
    private VotersBallot votersBallot;

    VotersChoicesAdder(Stage primaryStage, VotersBallot votersBallot) {
        this.currentStage = primaryStage;
        this.rowIndex = 0;
        this.votersBallot = votersBallot;

        // The lookup works only after the css have been randered
        this.currentStage.getScene().getRoot().applyCss();
        this.logger.debug("Created voters choises adder.");
    }



    /**
     * Adds all of the voters choises to the right panel
     */
    void ShowVotersChoices() throws IOException {
        this.RemoveAllAnswers();
        if (this.votersBallot != null) {
            this.addAnswer(this.getChannelChoice(this.votersBallot.VoterChannel));
            this.addAnswer(this.getNameChoice(this.votersBallot.GetCandidateName()));

            if (this.votersBallot.VotersImageSelection != null) {
                this.addAnswer(this.getImageChoice(this.votersBallot.VotersImageSelection.getAnswer()));
                this.addAnswer(this.getImageDescription(this.votersBallot.VotersImageSelection.getDescription()));
            }
            Label error = new Label();
            error.setPrefSize(250, 30);
            this.addAnswer(error);
        }
        this.logger.debug("Add all the choises of the voter to the represent grid pane.");
    }

    /**
     * Creates label and puts it into borderpane (the label contains desctiption of the cadidate image)
     * @param description the descriptiom of candidate image
     * @return borderPane which contains the label
     */
    private BorderPane getImageDescription(String description) {
        BorderPane borderPane = new BorderPane();
        Label descriptionLabel = new Label(description);
        borderPane.setCenter(descriptionLabel);
        return borderPane;
    }

    /**
     * Remove all previous binaryDatas from the container
     */
    private void RemoveAllAnswers() {
        this.GetAnswersContainer().getChildren().remove(0, this.GetAnswersContainer().getChildren().size());
        this.logger.debug("Removed all the previous voters choises.");
    }

    /**
     * Gets the container of all the answers
     *
     * @return GridPane object
     */
    private GridPane GetAnswersContainer() {
        return (GridPane) this.currentStage.getScene().lookup("#VotersChoices");
    }

   /**
     * Adds the GridPane binaryData to the panel
     *
     */
    private void addAnswer(Node node) throws IOException {
        GridPane container = this.GetAnswersContainer();
        //container.addRow(this.rowIndex);
        this.rowIndex++;
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(node);

        container.add(borderPane, 0, this.rowIndex);
        this.currentStage.show();
        this.logger.debug("Added other answer to answers pane.");
    }

    /**
     * Creates binaryData element which is grid pane with the binaryData and check box
     *
     * @param binaryData string binaryData to show to the voter
     * @return ImageView which contains the answer image
     */
    private ImageView getImageChoice(ByteString binaryData) throws IOException {
        ImageView imageView = new ImageView();
        if (binaryData != null) {
            BufferedImage bufferedImage = ImageIO.read(binaryData.newInput());
            Image image = SwingFXUtils.toFXImage(bufferedImage, null);

            imageView = new ImageView();
            imageView.setImage(image);

        }
        this.logger.debug("Wraps given image into image answer and returns it.");
        return imageView;
    }

    /**
     * Create label with the chanel inside
     * @param chanel the chanel the coter selected
     * @return BorderPane
     */
    private BorderPane getChannelChoice(List<Integer> chanel){
        BorderPane borderPane = new BorderPane();
        if (chanel != null) {
            Label label = new Label();
            label.setText("Your chanel: " + chanel.get(0) + "" + chanel.get(1) + "" + chanel.get(2) + "" + chanel.get(3));
            label.setPrefSize(250, 30);
            borderPane.setCenter(label);
            borderPane.setPrefSize(250, 30);
        }
        this.logger.debug("Adds the voters channel selection into grid pane and returns it.");
        return borderPane;
    }

    /**
     * Create grid pane element that contains the name of the answer
     * @param name the name of the candidate the voter selected
     * @return GridPane object
     */
    private BorderPane getNameChoice(String name){
        BorderPane borderPane = new BorderPane();
        if (name != null ) {
            Label label = new Label();
            label.setText("Your candidate name: " + name);
            label.setPrefSize(250, 30);
            borderPane.setCenter(label);
            borderPane.setPrefSize(250,30);
        }
        this.logger.debug("Wraps the given answer name by candidate in grid pane and returns it.");
        return borderPane;
    }
}
