package meerkat.voting.gui.panels.straight_channel_section;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import meerkat.voting.gui.managment.TwoWayNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Vladimir Eliezer Tokarev on 8/27/2016.
 * ChannelChoiceController handle the behavior of select channel section screen
 */
public class StraightChannelSectionController extends TwoWayNode {

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

    private List<Integer> chanelValue;
    private int pointer;
    private boolean lock;

    @FXML
    private void ProceedToNameSelection(MouseEvent boutonPressed) {
        this.currentStage.close();
        this.currentStage.setScene(this.next.GetCurrentScene());
        this.next.UpdateNode();
        this.currentStage.show();
        this.logger.debug("Created the proceess to name selection object loader.");
    }

    @Override
    /**
     * Creates the array of the canel value
     */
    public void UpdateNode() {
        this.pointer = 0;
        this.lock = false;

        // created the channel value list
        // every value in the list represents one of the cells values
        this.chanelValue = new ArrayList<>();
        this.chanelValue.add(0);
        this.chanelValue.add(0);
        this.chanelValue.add(0);
        this.chanelValue.add(0);
        this.logger.debug("Created and filled with zeros the channel value.");

        this.updateVisualChanel();
        this.UpdateVotersChoise();
    }

    /**
     * Updates the channel in the voters choise
     */
    private void UpdateVotersChoise(){
        this.votersBallot.VoterChannel = this.chanelValue;
    }

    /**
     * Updates the visual channel value
     */
    private void updateVisualChanel(){
        for (int i = 0 ; i < 4 ; i ++ ){
            String id = "#textarea_"+i;
            TextField textField = ((TextField)this.currentStage.getScene().lookup(id));
            textField.setText(String.valueOf(this.chanelValue.get(i)));
        }
        this.logger.debug("Updated the visual representation of the channel (its visual value).");
    }

    @FXML
    private void numberPressed(MouseEvent mousePressed){
        if (!this.lock) {
            String value = ((Button) mousePressed.getSource()).getId().split("_")[1];
            this.chanelValue.set(this.pointer, Integer.parseInt(value));
            this.pointer++;
            this.updateVisualChanel();
            this.UpdateVotersChoise();
            if (this.pointer == 4) {
                this.lock = true;
            }
            this.logger.debug("The " + value +" button have been pressed, then this value was pushed to channel value.");
        }
    }

    @FXML
    private void clearLastNumber(MouseEvent mousePressed){
        if (this.pointer >= 1) {
            int lastValueLocation = this.pointer - 1;
            int lastValue = this.chanelValue.get(lastValueLocation);
            this.chanelValue.set(lastValueLocation, 0);
            this.currentStage.show();
            this.pointer--;
            this.updateVisualChanel();
            this.UpdateVotersChoise();
            this.lock = false;
            this.logger.debug("The last value was removed from channel value (the value was " + lastValue + " ).");
        }
    }


}
