package interactivefeaturelocator.views;

import interactivefeaturelocator.model.MethodInfo;
import interactivefeaturelocator.model.MethodProperty;
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.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
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.Group;
import org.ntlab.trace.MethodExecution;

public class DataflowComposite extends Composite {
	MethodExecution me;
//	Group radioGroup;
	Combo propertyCombo;
	Button nextButton;
	Button currentRadioButton = null;
	

	public DataflowComposite(Composite parent, int style, MethodExecution me) {
		super(parent, style);
		// TODO Auto-generated constructor stub
		this.me = me;
		setLayout(new FormLayout());
		
		propertyCombo = new Combo(this, SWT.READ_ONLY);
		FormData comboData = new FormData();
		comboData.top = new FormAttachment(0, 10);
		comboData.right = new FormAttachment(100, -10);
		propertyCombo.setLayoutData(comboData);
		updateCombo(me);
		
//		radioGroup = new Group(this, SWT.NONE);
//		FormData groupData = new FormData();
//		groupData.left = new FormAttachment(0,  0);
//		groupData.right = new FormAttachment(propertyCombo, -10);
//		groupData.bottom = new FormAttachment(100, 0);
//		radioGroup.setLayoutData(groupData);
		updateExSelectionButton(me);
		
		nextButton = new Button(this, SWT.NONE);
		FormData nextButtonData = new FormData();
		nextButtonData.right = new FormAttachment(100, -10);
		nextButtonData.bottom = new FormAttachment(100, -10);
		nextButton.setLayoutData(nextButtonData);
		nextButton.setText("次へ");
		nextButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (currentRadioButton == null) {
					MessageDialog.openInformation(parent.getShell(), "エラー", "式を選択してください");
				} else {
					MessageDialog.openInformation(parent.getShell(), "次へ", 
						me.getSignature() + "の" + currentRadioButton.getText() + "は" + propertyCombo.getText() + "に決定。");
				}
			}
		});


	}
	
	public void update(MethodExecution me) {
		updateCombo(me);
		updateExSelectionButton(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:
				propertyCombo.add("新規作成");
				propertyCombo.setText("新規作成");
				break;
			case modified:
				propertyCombo.add("修正あり");
				propertyCombo.setText("修正あり");
				break;
			case unmodified:
				propertyCombo.add("修正なし");
				propertyCombo.setText("修正なし");
				break;

			default:
				break;
			}
		}
	}
	
	public void updateExSelectionButton(MethodExecution me) {
		ArrayList<Button> explessionSelectionButtons = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			Button exSelectionButton = new Button(this, SWT.RADIO);
			FormData exSelectionData = new FormData();
			if (i == 0) {
				exSelectionData.top = new FormAttachment(0, 0);
				
			} else {
				exSelectionData.top = new FormAttachment(explessionSelectionButtons.get(i-1), 0);
			}
			exSelectionData.left = new FormAttachment(0,  0);
			exSelectionData.right = new FormAttachment(propertyCombo, 0);
			exSelectionButton.setLayoutData(exSelectionData);
			exSelectionButton.setText(i + "式の値に直接的に影響を与えたメソッドの実行");
			exSelectionButton.setBackground(this.getBackground());
			exSelectionButton.addSelectionListener(new SelectionListener() {
				
				@Override
				public void widgetSelected(SelectionEvent e) {
					// TODO Auto-generated method stub
					currentRadioButton = (Button)e.widget;
				}
				
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					// TODO Auto-generated method stub
					
				}
			});
			explessionSelectionButtons.add(exSelectionButton);
		}
//		radioGroup.setData(explessionSelectionButtons);
	}

}
