package interactivefeaturelocator.views;

import interactivefeaturelocator.model.DerivationSession;
import interactivefeaturelocator.model.MethodInfo;
import interactivefeaturelocator.model.MethodProperty;
import interactivefeaturelocator.model.CallTreeGraphNode;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.ntlab.trace.MethodExecution;

public class SelectionComposite extends Composite {
	private MethodExecution me;
	private boolean dataflow;
	private Composite dataflowComposite;
	private Combo combo;
	private Label discription;
	private Button decideButton;

	public SelectionComposite(Composite parent, int style, MethodExecution me, boolean dataflow) {
		super(parent, style);
		// TODO Auto-generated constructor stub
		this.me = me;
		this.dataflow = dataflow;
		
		setLayout(new FormLayout());
		
		combo = new Combo(this, SWT.READ_ONLY);
		FormData comboData = new FormData();
		comboData.top = new FormAttachment(0, 10);
		comboData.right = new FormAttachment(100, -10);
		combo.setLayoutData(comboData);
		updateCombo(me);
		combo.setVisible(!dataflow);
				
		decideButton = new Button(this, SWT.NONE);
		FormData decideButtonData = new FormData();
		decideButtonData.right = new FormAttachment(100, -10);
		decideButtonData.bottom = new FormAttachment(100, -10);
		decideButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
//				session.decideMethodProperty(property)
//				int index = combo.getSelectionIndex();
//				DerivationSession session = this;
//				session.decideMethodProperty(((ArrayList<MethodProperty>)combo.getData()).get(index));
				MessageDialog.openInformation(parent.getShell(), "決定", 
						me.getSignature() + "は" + combo.getText() + "に決定。");
			}
		});
		decideButton.setText("決定");
		decideButton.setLayoutData(decideButtonData);

		discription = new Label(this, SWT.NONE);
		updateDiscription(me);
		FormData textData = new FormData();
		textData.top = new FormAttachment(0, 0);
		textData.left = new FormAttachment(0, 0);
		if (dataflow) {
			textData.right = new FormAttachment(100, -10);
		} else {
			textData.right = new FormAttachment(combo, -10);
		}
		discription.setLayoutData(textData);

		dataflowComposite = new DataflowComposite(this, SWT.BORDER, me);
		FormData dataflowCompData = new FormData();
		dataflowCompData.top = new FormAttachment(discription, 10);
		dataflowCompData.bottom = new FormAttachment(decideButton, -10);
		dataflowCompData.right = new FormAttachment(100, -10);
		dataflowCompData.left = new FormAttachment(0, 10);
		dataflowComposite.setLayoutData(dataflowCompData);
		dataflowComposite.setVisible(dataflow);
		
		
	}
	
	public void update(MethodExecution me) {
		updateCombo(me);
		updateDiscription(me);
	}

	public void updateCombo(MethodExecution me) {
		ArrayList<MethodProperty> propertyList = new ArrayList<>(((MethodInfo)me.getAugmentation()).getProperties());
		setData(propertyList);
		for (MethodProperty property : (ArrayList<MethodProperty>)this.getData()) {
			switch (property) {
			case created:
				combo.add("新規作成");
				combo.setText("新規作成");
				break;
			case modified:
				combo.add("修正あり");
				combo.setText("修正あり");
				break;
			case unmodified:
				combo.add("修正なし");
				combo.setText("修正なし");
				break;

			default:
				break;
			}
		}
	}
	
	public void updateDiscription(MethodExecution me) {
		String text = "";
		text += "Current Method : " + me.getSignature() + "\n";
		discription.setText(text);
	}
	

}
