/*
 *
 *          Copyright (c) 2014,2019  AT&T Knowledge Ventures
 *                     SPDX-License-Identifier: MIT
 */
package com.att.research.xacml.admin.view.windows;

import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Buffered.SourceException;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class VariableDefinitionEditorWindow extends Window {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private Button buttonSave;
	@AutoGenerated
	private TextField textFieldID;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final VariableDefinitionEditorWindow self = this;
	private final VariableDefinitionType variable;
	private boolean isSaved = false;
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 * @param variable 
	 */
	public VariableDefinitionEditorWindow(VariableDefinitionType variable) {
		buildMainLayout();
		//setCompositionRoot(mainLayout);
		setContent(mainLayout);
		//
		// Save
		//
		this.variable = variable;
		//
		// Set our shortcuts
		//
		this.setCloseShortcut(KeyCode.ESCAPE);
		//
		// Initialize
		//
		this.initializeText();
		this.initializeButton();
		//
		// Initial focus
		//
		this.textFieldID.focus();
	}
	
	protected void initializeText() {
		//
		// Initialize GUI properties
		//
		this.textFieldID.setImmediate(true);
		//
		// Listen to changes
		//
		this.textFieldID.addTextChangeListener(new TextChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				if (event.getText() != null && event.getText().isEmpty() == false) {
					self.buttonSave.setEnabled(true);
				} else {
					self.buttonSave.setEnabled(false);
				}
			}
			
		});
		this.textFieldID.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				if (self.textFieldID.getValue() != null && self.textFieldID.getValue().isEmpty() == false) {
					self.buttonSave.setEnabled(true);
				} else {
					self.buttonSave.setEnabled(false);
				}
			}			
		});
		//
		// Set the value
		//
		this.textFieldID.setValue(variable.getVariableId());
	}
	
	protected void initializeButton() {
		this.buttonSave.setClickShortcut(KeyCode.ENTER);
		this.buttonSave.addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					//
					// Commit it
					//
					self.textFieldID.commit();
					//
					// Save it
					//
					self.variable.setVariableId(self.textFieldID.getValue());
					self.isSaved = true;
					//
					// Close window
					//
					self.close();
				} catch (SourceException | InvalidValueException e) {
				}
			}			
		});
	}
	
	public boolean isSaved() {
		return this.isSaved;
	}
	
	public VariableDefinitionType getVariable() {
		return this.variable;
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("-1px");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);
		
		// top-level component properties
		setWidth("-1px");
		setHeight("-1px");
		
		// textFieldID
		textFieldID = new TextField();
		textFieldID.setCaption("Variable ID");
		textFieldID.setImmediate(false);
		textFieldID.setWidth("-1px");
		textFieldID.setHeight("-1px");
		textFieldID.setInvalidAllowed(false);
		textFieldID.setRequired(true);
		textFieldID.setNullRepresentation("");
		mainLayout.addComponent(textFieldID);
		
		// buttonSave
		buttonSave = new Button();
		buttonSave.setCaption("Save and Continue");
		buttonSave.setImmediate(false);
		buttonSave.setWidth("-1px");
		buttonSave.setHeight("-1px");
		mainLayout.addComponent(buttonSave);
		mainLayout.setComponentAlignment(buttonSave, new Alignment(48));
		
		return mainLayout;
	}

}