package com.qst.dms.ui;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;

public class UpdateTimeSetFXML
{

    public static int updateTime;
    public int tempUpdateTime;
    private InfoInputFXML infoInputFXML;
    private TableFXML tableFXML;
    private UpdateTimeSetFXML updateTimeSetFrame;
    @FXML
    private TextField timeField;

    @FXML
    private Slider timeSlider;


    @FXML
    void mouseDragging(MouseEvent event) {
        tempUpdateTime =  (int)timeSlider.getValue();
        timeField.setText(""+tempUpdateTime);
    }


    public void initialize()
    {
        updateTime = 1;
        timeSlider.setMin(1);
        timeSlider.setMax(60);
        timeSlider.setValue(1);
        timeField.setText("1");
    }

    public void getDelegation(InfoInputFXML _infoInputFXMLController, TableFXML _tableFXMLController)
    {
        infoInputFXML=_infoInputFXMLController;
        tableFXML=_tableFXMLController;
    }

    public void beChanged(KeyEvent keyEvent)
    {
        String value = timeField.getText();
        int caretPosition = timeField.getCaretPosition();
        if (!value.matches("\\d*")) {
            timeField.setText(value.replaceAll("[^\\d]", ""));
            timeField.positionCaret(Math.max(0,caretPosition-1));
        }
        else {
            if(value.isEmpty())
                return;
            tempUpdateTime = Math.max(1,Integer.parseInt(value));
            timeSlider.setValue(tempUpdateTime);
        }
    }

    public void closeRequest()
    {
        updateTime=tempUpdateTime;
        infoInputFXML.updateSQLCheckerTime();
        tableFXML.updateFlusherTime();
    }

}
