/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.opencit.ui.web.WizardSteps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.extensions.wizard.dynamic.DynamicWizardStep;
import org.apache.wicket.extensions.wizard.dynamic.IDynamicWizardStep;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.openengsb.core.common.Domain;
import org.openengsb.core.common.ServiceManager;
import org.openengsb.core.common.descriptor.AttributeDefinition;
import org.openengsb.core.common.descriptor.ServiceDescriptor;
import org.openengsb.core.common.validation.MultipleAttributeValidationResult;
import org.openengsb.opencit.core.config.OpenCitConfigurator;
import org.openengsb.opencit.core.projectmanager.model.Project;
import org.openengsb.opencit.ui.web.model.ServiceManagerModel;
import org.openengsb.ui.common.wicket.editor.ServiceEditorPanel;
import org.openengsb.ui.web.ServiceEditor;
import org.openengsb.ui.web.model.WicketStringLocalizer;

public class SetAttributesStep extends DynamicWizardStep {
	private Project project;
	private ServiceEditor editor;
	private FeedbackPanel feedbackPanel;
	private ServiceManagerModel serviceManager;
	private String serviceId;

	public SetAttributesStep(final Project project,
			final ServiceManagerModel serviceManager) {
		super(new CreateProjectStep(project), new ResourceModel(
				"setAttribute.title"),
				new ResourceModel("setAttribute.summary"), new Model<Project>(
						project));
		this.project = project;
		this.serviceManager = serviceManager;
		feedbackPanel = new FeedbackPanel("feedback");
		feedbackPanel.setOutputMarkupId(true);
		add(feedbackPanel);
		setComplete(false);

		IModel<List<AttributeDefinition>> attributes = new LoadableDetachableModel<List<AttributeDefinition>>() {
			@Override
			protected List<AttributeDefinition> load() {
				return buildAttributeList(serviceManager.getObject());
			}
		};
		Map<String, String> values = new HashMap<String, String>();

		editor = new ServiceEditor("editor", attributes.getObject(), values) {
			@Override
			public void onSubmit() {
				String domain = serviceManager.getObject().getDescriptor().getServiceType().getCanonicalName();
				String projName = SetAttributesStep.this.project.getId();
				serviceId = projName + "-" + domain;
				serviceManager.getObject().update(serviceId, getValues());
				setComplete(true);
			}
		};
		add(editor);
	}

	private List<AttributeDefinition> buildAttributeList(ServiceManager service) {
		AttributeDefinition.Builder builder = AttributeDefinition
				.builder(new WicketStringLocalizer(this));
		AttributeDefinition id = builder.id("id").name("attribute.id.name")
				.description("attribute.id.description").required().build();
		ServiceDescriptor descriptor = service.getDescriptor();
		List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>();
		attributes.add(id);
		attributes.addAll(descriptor.getAttributes());
		return attributes;
	}

	@Override
	public boolean isLastStep() {
		return false;
	}

	@Override
	public IDynamicWizardStep next() {
		project.addService(serviceManager.getObject().getDescriptor()
				.getServiceType(), serviceId);
		Map<Class<? extends Domain>, String> configured = project.getServices();
		for (Class<? extends Domain> c : OpenCitConfigurator
				.getRequiredServices()) {
			if (configured == null || !configured.containsKey(c)) {
				return new DomainSelectionStep(project);
			}
		}
		return new FinalStep(this, project);
	}

}

@@ -1119,60 +1119,8 @@
 ep;%0A
-import org.apache.wicket.markup.html.form.CheckBox;%0A
 impo
@@ -1169,24 +1169,24 @@
 dbackPanel;%0A
+
 import org.a
@@ -1356,60 +1356,8 @@
 el;%0A
-import org.apache.wicket.model.StringResourceModel;%0A
 impo
@@ -1574,87 +1574,8 @@
 or;%0A
-import org.openengsb.core.common.validation.MultipleAttributeValidationResult;%0A
 impo
@@ -1763,73 +1763,8 @@
 el;%0A
-import org.openengsb.ui.common.wicket.editor.ServiceEditorPanel;%0A
 impo
@@ -1860,16 +1860,44 @@
 lizer;%0A%0A
+@SuppressWarnings(%22serial%22)%0A
 public c
@@ -3200,16 +3200,95 @@
 domain;%0A
+%09%09%09%09Map%3CString, String%3E values = getValues();%0A%09%09%09%09values.put(%22id%22, serviceId);%0A
 %09%09%09%09serv
@@ -3328,27 +3328,22 @@
 viceId, 
-getV
+v
 alues
-()
 );%0A%09%09%09%09s

make sure service-id and the corresponding attribute-value are in sync