package com.rjconsultores.ventaboletos.web.utilerias;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.zkoss.zul.Button;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Window;

public class MyCustomButton {

	// disabled="${arg.isReadOnly}"
	private enum Buttons {
		btnApagar, btnSalvar
	}

	public MyCustomButton() {

	}

	public static void validateButtons(Window win1, Map args) {

		List list = win1.getChildren();
		for (Iterator winIt = list.iterator(); winIt.hasNext();) {

			Object itWin = winIt.next();
			if (itWin instanceof Toolbar) {

				Toolbar toolbar = (Toolbar) itWin;
				List listChildren = toolbar.getChildren();

				for (Iterator toolIt = listChildren.iterator(); toolIt.hasNext();) {
					Object itToolbar = toolIt.next();

					if (itToolbar instanceof Button) {
						Button button = (Button) itToolbar;

						if (contains(button.getId())) {
							button.setDisabled((Boolean) (args.containsKey("isReadOnly") ? args.get("isReadOnly") : Boolean.FALSE));
						}

					} else if (itToolbar instanceof Hbox) {

						Hbox hbox = (Hbox) itToolbar;

						List listButton = hbox.getChildren();

						for (Iterator buttonIt = listButton.iterator(); buttonIt.hasNext();) {

							Object button = buttonIt.next();

							if (button instanceof Button) {
								Button buttonIn = (Button) button;

								if (contains(buttonIn.getId())) {
									buttonIn.setDisabled((Boolean) (args.containsKey("isReadOnly") ? args.get("isReadOnly") : Boolean.FALSE));
								}
							}

						}

					}
				}

			}
		}
	}

	private static boolean contains(String btnName) {

		for (Buttons c : Buttons.values()) {
			if (c.name().equals(btnName)) {
				return true;
			}
		}

		return false;
	}

}
