package cz.req.ax;

import com.vaadin.server.VaadinService;
import com.vaadin.ui.*;

import javax.servlet.http.Cookie;
import java.util.*;

public class AxUtils {

    public static String readCookie(String name) {
        for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
            if (cookie.getName().equals(name)) {
                return cookie.getValue();
            }
        }
        return null;
    }

    public static void writeCookie(String name, String value) {
        try {
            Cookie cookie = new Cookie(name, value);
            cookie.setPath(VaadinService.getCurrentRequest().getContextPath());
            cookie.setMaxAge(30 * 24 * 60 * 60);
            VaadinService.getCurrentResponse().addCookie(cookie);
        } catch (Exception e) {
            System.err.println("Cannot write cookie " + name + ": " + e.getMessage());
        }
    }

    public static Integer getTabPosition(TabSheet tabSheet) {
        return tabSheet.getTabPosition(tabSheet.getTab(tabSheet.getSelectedTab()));
    }

    public static <T> T getDataValue(Button.ClickEvent event) {
        Object source = event.getSource();
        if (source instanceof AbstractComponent) {
            Object data = ((AbstractComponent) source).getData();
            return (T) data;
        }
        throw new IllegalArgumentException();
    }

    public static void closeWindows() {
        for (Window window : new ArrayList<>(UI.getCurrent().getWindows())) {
            UI.getCurrent().removeWindow(window);
        }
    }

    public static boolean focusOnFirstField(Component component) {
        if (component == null) return false;
        if (component instanceof HasComponents) {
            Iterator<Component> iterator = ((HasComponents) component).iterator();
            while (iterator.hasNext()) {
                if (focusOnFirstField(iterator.next())) {
                    return true;
                }
            }
        } else if (component instanceof Field) {
            ((Field) component).focus();
            return true;
        }
        return false;
    }

    public static String makeURL(String base, Object... parameters) {
        StringBuilder sb = new StringBuilder(base);
        if (parameters != null && parameters.length > 0) {
            for (Object parameter : parameters) {
                if (parameter != null) {
                    sb.append("/").append(parameter.toString());
                }
            }
        }
        return sb.toString();
    }

    public static <T> T newInstance(Class<T> beanType) {
        try {
            return beanType.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void appendCaptionSuffix(Component component, String suffix) {
        String caption = component.getCaption();
        if (caption != null && suffix != null && !caption.endsWith(suffix)) {
            component.setCaption(caption + suffix);
        }
    }

    public static void removeCaptionSuffix(Component component, String suffix) {
        String caption = component.getCaption();
        if (caption != null && suffix != null && caption.endsWith(suffix)) {
            component.setCaption(caption.substring(0, caption.length() - suffix.length()));
        }
    }

}

@@ -1639,24 +1639,181 @@
 ent 
-== null)
+instanceof Field) %7B%0A            // Field mus%C3%ADme testovat prvn%C3%AD, proto%C5%BEe m%C5%AF%C5%BEe z%C3%A1rove%C5%88 implementovat HasComponents%0A            ((Field) component).focus();%0A           
  return 
 fals
@@ -1804,28 +1804,27 @@
      return 
-fals
+tru
 e;%0A        i
@@ -1814,32 +1814,39 @@
 n true;%0A        
+%7D else 
 if (component in
@@ -2122,123 +2122,8 @@
   %7D%0A
-        %7D else if (component instanceof Field) %7B%0A            ((Field) component).focus();%0A            return true;%0A
     

Fix AxUtils.focusOnFirstField