package kay;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;
import org.apache.commons.validator.routines.InetAddressValidator;

import static java.lang.Thread.sleep;


@Title("FDB viewer") //заголовок в окне браузера
@Theme("valo") //Тема
@SpringUI
public class AppUI extends UI {
    private Grid grid = new Grid();
    private TextField filterText = new TextField();
    private Button clearFilterTextBtn = new Button(FontAwesome.TIMES);
    private TextField ipAddress = new TextField();
    private Button getFdbBtn = new Button(FontAwesome.ARROW_DOWN);
    private InetAddressValidator iav = InetAddressValidator.getInstance();

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout root = new VerticalLayout();
        //root.setSizeFull();
        root.setMargin(true);
        root.setSpacing(true);
        setContent(root);

        final CssLayout getFdbLayout = new CssLayout();
        getFdbLayout.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
        getFdbLayout.addComponents(ipAddress,getFdbBtn);
        ipAddress.setValue("192.168.1.");
        ipAddress.addTextChangeListener(e-> getFdbBtn.setEnabled(iav.isValidInet4Address(e.getText())));
        
        getFdbBtn.setEnabled(false);

        filterText.setInputPrompt("row filter...");
        clearFilterTextBtn.addClickListener(clickEvent -> filterText.clear());

        final CssLayout filtering = new CssLayout();
        filtering.addComponents(filterText,clearFilterTextBtn);
        filtering.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);

        HorizontalLayout topBar = new HorizontalLayout();
        topBar.setSpacing(true);
        topBar.addComponents(getFdbLayout,filtering);
        root.addComponents(topBar,grid);

//for debug
new Thread(()-> {
    while (true) {
    try {
        System.out.println(ipAddress.getValue());
        sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }}
}).start();


    }
}
