/**
 * @author tyl
 * @Action  ini
 * 2011-3-21
 */
package cn.com.origin.autocodemanager.common.dialog;

import java.util.HashMap;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
import cn.com.origin.autocodemanager.common.AutoCodeManagerImageKey;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.AutoCodeManangerConstants;
import cn.com.origin.autocodemanager.common.CacheImage;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.TreeViewOperation;
import cn.com.origin.autocodemanager.views.AutoCodeListCodeView;
import cn.com.origin.autocodemanager.views.AutoCodeSegmentView;
import cn.com.origin.autocodemanager.views.AutoCodeTreeCodeView;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class AbstractCodeDialog extends Dialog {
	public AbstractCreateTreeView createView;
	public AbstractTreeData selectedTreeData;
	public TCComponent selectedComponent;
	public String title;
	public boolean isEdit;
	public TreeAndListCodeOperation treeAndListCodeOperation;
	public TreeViewOperation treeViewOperation;
	public Text nodeDescriptionText;
	public Text nodeNameText;
	public Text nodeValueText;
	public Text codeStyleText;
	public Text codeLengthText;
	public Combo typeCombo;
	public TCSession session;
	public AutoCodeListCodeView codeListView;
	public AutoCodeSegmentView codeSegmentView;
	public AutoCodeTreeCodeView codeTreeView;
	public Text seqBeginText;
	public Text seqMaxText;
	public Text seqStepText;
	public Text seqExcludeStrText;

	public AbstractCodeDialog(Shell parentShell, AbstractCreateTreeView createView, String title, boolean isEdit) {
		//super(parentShell);
		super(new Shell());
		this.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE 
				| SWT.APPLICATION_MODAL);
		this.createView = createView;
		this.title = title;
		this.isEdit = isEdit;
		
		
		initParams();
		this.selectedTreeData = treeViewOperation.getSelectedTreeNode(createView.treeViewer);
		if (selectedTreeData != null) {
			this.selectedComponent = selectedTreeData.getPackCodeNodeInfo().getNodeComponent();
		}
	}

	public void initParams() {
		this.treeAndListCodeOperation = new TreeAndListCodeOperation();
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		this.treeViewOperation = new TreeViewOperation();
		codeListView = (AutoCodeListCodeView) createView.viewPart.getSite().getPage().findView(AutoCodeManangerConstants.AutoCodeManagerListCodeView_ID);
		codeSegmentView = (AutoCodeSegmentView) createView.viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeSegmentView_ID);
		codeTreeView = (AutoCodeTreeCodeView) createView.viewPart.getSite().getPage().findView(AutoCodeManangerConstants.AutoCodeManagerTreeCodeView_ID);
	}

	public AbstractCodeDialog(Shell parentShell, AbstractCreateTreeView createView, String title) {
		super(parentShell);
		this.title = title;
		this.createView = createView;
		this.treeAndListCodeOperation = new TreeAndListCodeOperation();
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);

		return container;
	}

	public void initNewCodeComposite(Composite composite) {

	}

	public String getSeqLength() {
		String maxValue = getTextValue(seqMaxText);
		if ((!maxValue.equalsIgnoreCase("0")) && (maxValue.trim().length() > 0)) {
			return String.valueOf(maxValue.trim().length());
		} else {
			return "0";
		}
	}

	public void saveNewCodeNode(AbstractTreeData parentTreeData, TCComponent updateComponent, TreeViewer treeViewer, String type, boolean isRoot) {
		HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
		TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
		try {
			if (isEdit) {
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name, getTextValue(nodeNameText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_value, getTextValue(nodeValueText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_style, getTextValue(codeStyleText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, getTextValue(codeLengthText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_desc, getTextValue(nodeDescriptionText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, "");

				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, getTextValue(seqBeginText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, getSeqLength());
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value, getTextValue(seqMaxText));
				if (updateComponent != null) {
					treeAndListCodeOperation.updateTreeORListNode(updateComponent, hashMap);
					PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(updateComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
					selectedTreeData.setPackCodeNodeInfo(packCodeNodeInfo);
					treeViewer.refresh(selectedTreeData);
				}
			} else {
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_index, "");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name, getTextValue(nodeNameText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_value, getTextValue(nodeValueText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type, type);
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_style, getTextValue(codeStyleText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, getTextValue(codeLengthText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node, "");

				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_desc, getTextValue(nodeDescriptionText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root, String.valueOf(isRoot));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, "");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, "");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "false");
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, "false");

				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, getTextValue(seqBeginText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, getSeqLength());
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value, getTextValue(seqMaxText));
				hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes, "");

				TCComponent ruleComponent = null;
				if (!isRoot) {
					AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
					if (segmentTreeData != null) {
						ruleComponent = segmentTreeData.getPackCodeNodeInfo().getNodeComponent();
					}
				}
				TCComponent newComponent = new TreeAndListCodeOperation().createNewTreeORListNode(ruleComponent, parentTreeData, hashMap);
				if (newComponent == null) {
					return;
				}
				PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(newComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
				AbstractTreeData treeData = new AbstractTreeData(parentTreeData, packCodeNodeInfo);
				treeViewOperation.addExistCodeNode(treeViewer, parentTreeData, treeData);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public String getTextValue(Text text) {
		if (text != null) {
			return text.getText().trim();
		} else {
			return "";
		}
	}

	public void createSeqComposite(Composite parent) {
		Composite composite = new Composite(parent, 0);
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalSpan = 20;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;
		composite.setLayoutData(gridData1);
		composite.setLayout(gridLayout1);
		initSeqPropertyComposite(composite);
		try {
			if (selectedTreeData != null) {
				TCComponent selectedComp = selectedTreeData.getPackCodeNodeInfo().getNodeComponent();
				selectedComp.refresh();
				if (selectedComp != null) {
					selectedComp.refresh();
					String seqProperty[] = { AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value };
					String[] seqValues = selectedComp.getProperties(seqProperty);
					setTextValue(seqBeginText, seqValues[0]);
					setTextValue(seqMaxText, seqValues[1]);
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void initSeqPropertyComposite(Composite parent) {
		Composite seqComposite = new Composite(parent, SWT.NONE);
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalSpan = 20;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;

		seqComposite.setLayoutData(gridData1);
		seqComposite.setLayout(gridLayout1);
		createNewLabel(seqComposite, "初始值");
		seqBeginText = createNewText(seqComposite, "1");
		createNewLabel(seqComposite, "最大值");
		seqMaxText = createNewText(seqComposite, "9");
	}

	public void createNodeComposite(Composite parent) {
		Composite composite = new Composite(parent, 0);
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 2;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalSpan = 20;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;
		composite.setLayoutData(gridData1);
		composite.setLayout(gridLayout1);
		initNewCodeComposite(composite);
		try {
			if (selectedTreeData != null) {
				TCComponent selectedComp = selectedTreeData.getPackCodeNodeInfo().getNodeComponent();
				PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(selectedComp, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
				selectedTreeData.setPackCodeNodeInfo(packCodeNodeInfo);
				if (selectedComp != null) {
					selectedComp.refresh();
					String seqProperty[] = { AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value };

					String[] seqValues = selectedComp.getProperties(seqProperty);
					setTextValue(codeStyleText, packCodeNodeInfo.getCode_style());
					setTextValue(codeLengthText, seqValues[0]);

					setTextValue(nodeNameText, packCodeNodeInfo.getNode_name());
					setTextValue(nodeValueText, packCodeNodeInfo.getNode_value());
					setTextValue(nodeDescriptionText, packCodeNodeInfo.getNode_desc());

					setTextValue(seqBeginText, seqValues[1]);
					setTextValue(seqMaxText, seqValues[2]);
					if (typeCombo != null) {
						if (packCodeNodeInfo.getNode_type().equals(AutoCodeManagerPropertyName.ListName)) {
							typeCombo.setText("列表");
						} else {
							typeCombo.setText("编码树");
						}
						if (isEdit) {
							typeCombo.setEnabled(false);
						}
					}
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	public void setTextValue(Text text, String value) {
		if (text != null) {
			text.setText(value);
		}
	}

	public GridData getNewGridData() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = false;
		gridData.verticalAlignment = GridData.FILL;
		return gridData;
	}

	public Group createCompositeGroup(Composite parent, String title) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalSpan = 1;
		gridData.horizontalAlignment = GridData.FILL;
		Group codeTreeGroup = new Group(parent, SWT.NONE);
		codeTreeGroup.setText(title);
		codeTreeGroup.setLayout(gridLayout);
		codeTreeGroup.setLayoutData(gridData);
		return codeTreeGroup;
	}

	public Text createNewText(Composite parent, String initValue) {
		Text newText = new Text(parent, SWT.BORDER);
		newText.setLayoutData(getNewGridData());
		newText.setText(initValue);
		return newText;
	}

	public Button createNewCheckBox(Composite parent, String title) {
		Button newButton = new Button(parent, SWT.CHECK);
		newButton.setLayoutData(getNewGridData());
		newButton.setText(title);
		return newButton;
	}

	public Combo createNewCombo(Composite parent, String[] items) {
		Combo newCombo = new Combo(parent, SWT.BORDER);
		newCombo.setLayoutData(getNewGridData());
		newCombo.setItems(items);
		return newCombo;
	}

	public Label createNewLabel(Composite parent, String title) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(title + "：");
		return label;
	}

	public void postMesssageBox(String info) {
		MessageBox mb = new MessageBox(getShell(), SWT.ICON_ERROR | SWT.OK);
		mb.setText("INFO");
		mb.setMessage(info);
		mb.open();
	}

	// 创建对话框按钮
	@Override
	protected void createButtonsForButtonBar(final Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "确定", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "关闭", false);
		getButton(IDialogConstants.OK_ID).setImage(CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.OK_IMAGE));
		getButton(IDialogConstants.CANCEL_ID).setImage(CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CLOSE_IMAGE));
	}

	@Override
	public boolean close() {
		return super.close();
	}

	@Override
	protected Point getInitialSize() {
		return new Point(230, 150);
	}
}
