package com.connor.dongmu.plm.handlers.tmom008;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;

import com.connor.dongmu.plm.handlers.tmom005.TCGroupDialogTree;
import com.connor.dongmu.plm.handlers.tmom005.TMomUserBean;
import com.connor.dongmu.plm.util.TMomMethodUtil;
import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCComponentUserType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.DateButton;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;

public class TM2StuffCheckDialog extends AbstractAIFDialog implements
		ActionListener {

	private AbstractAIFApplication app;
	private TCSession session;
	private JPanel rootPanel;
	private JPanel panel1;
	private JPanel panel2;
	private JButton okButton;
	private JButton celButton;
	private JButton fileButton;
	private JTextField pathText;
	private JTextField fileName;
	private static JTextField personName;
	private JFileChooser jfc;
	private DateButton startDate;
	private DateButton finishDate;
	private static JButton personButton;
	private static TCComponentUser user;
	private static AddUserDialog userDialog;

	public TM2StuffCheckDialog(AbstractAIFApplication app, TCSession session) {
		super(false);
		this.app = app;
		this.session = session;
	}

	public void init() {
		this.setTitle("Ա˱");
		FileSystemView fsv = FileSystemView.getFileSystemView();
		String deskTop = fsv.getHomeDirectory().getPath();
		this.setPreferredSize(new Dimension(520, 250));
		jfc = new JFileChooser();
		jfc.setCurrentDirectory(new File(deskTop));// ļѡĳʼĿ¼Ϊc
		okButton = new JButton("ȷ");
		celButton = new JButton("ȡ");
		fileButton = new JButton("...");

		fileButton.addActionListener(this);

		okButton.addActionListener(this);

		celButton.addActionListener(this);

		pathText = new JTextField(25);
		fileName = new JTextField(25);
		this.startDate = new DateButton(new SimpleDateFormat("yyyy-M-dd"));
		this.finishDate = new DateButton(new SimpleDateFormat("yyyy-M-dd"));

		pathText.setText(deskTop);
		fileName.setText("Ա˱");
		personName = new JTextField(10);
		// personName.enable(false);
		personButton = new JButton("ѡԱ.....");
		personButton.addActionListener(this);
		panel1 = new JPanel(new PropertyLayout());
		panel1.add("1.1.right.top.preferred.preferred", new JLabel("         "));
		panel1.add("2.1.right.top.preferred.preferred", new JLabel("ʼʱ:"));
		panel1.add("2.2.left.top.preferred.preferred", startDate);//
		panel1.add("3.1.right.top.preferred.preferred", new JLabel("ʱ:"));
		panel1.add("3.2.left.top.preferred.preferred", finishDate);
		//
		panel1.add("4.1.right.top.preferred.preferred", new JLabel("Ա:"));
		panel1.add("4.2.left.top.preferred.preferred", personName);
		panel1.add("4.3.left.top.preferred.preferred", personButton);
		// fileName
		panel1.add("5.1.right.top.preferred.preferred", new JLabel("ļ:"));
		panel1.add("5.2.left.top.preferred.preferred", fileName);
		panel1.add("6.1.right.top.preferred.preferred", new JLabel("ѡļ:"));
		panel1.add("6.2.left.top.preferred.preferred", pathText);
		panel1.add("6.3.left.top.preferred.preferred", fileButton);

		panel2 = new JPanel(new FlowLayout());
		panel2.add(okButton);
		panel2.add(new JLabel("     "));
		panel2.add(celButton);

		rootPanel = new JPanel(new BorderLayout());
		rootPanel.add(panel1, BorderLayout.CENTER);
		rootPanel.add(panel2, BorderLayout.SOUTH);

		this.add(rootPanel, "Center");
		this.pack();
		this.centerToScreen();
		this.showDialog();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object sourceObj = e.getSource();
		if (sourceObj.equals(this.okButton)) {
			try {
				String personNameStr = this.personName.getText();
				if (personNameStr.trim().equals("")) {
					this.dispose();
					MessageBox.post("ѡԱ", "Error", MessageBox.ERROR);
					this.show();
					this.setVisible(true);
					return;
				} else {
					TCComponentUserType type = null;
					InterfaceAIFComponent[] userComps = TMomMethodUtil
							.searchComponentsCollection(this.session,
									"cust_query_user",
									new String[] { "username" },
									new String[] { personNameStr });
					if (userComps != null && userComps.length > 0) {
						this.user = (TCComponentUser) userComps[0];
					} else {
						this.dispose();
						MessageBox.post("ѡԱ", "Error", MessageBox.ERROR);
						this.show();
						this.setVisible(true);
						return;
					}
				}
				if (this.user == null) {
					this.dispose();
					MessageBox.post("ѡԱ", "Error", MessageBox.ERROR);
					this.show();
					this.setVisible(true);
					return;
				}
				File tempFile = new File(pathText.getText());
				if (tempFile.exists() && tempFile.isDirectory()) {
					this.dispose();
					// ҵ߼
					try {

						TM2StuffCheckOperation operation = new TM2StuffCheckOperation(
								user, this.personName.getText(), app, session,
								this.startDate.getDate(),
								this.finishDate.getDate(),
								this.fileName.getText(),
								this.pathText.getText());
						// operation.executeOperation();
						new Thread(operation).start();
						this.dispose();
					} catch (Exception e1) {
						TMomMethodUtil.setByPass(this.session, false);
						// TODO Auto-generated catch block
						e1.printStackTrace();
						this.dispose();
						MessageBox.post("ʧ", "Error", MessageBox.ERROR);
						this.show();
						this.setVisible(true);
					}

				} else {
					this.dispose();
					MessageBox.post("ѡ񵼳Ŀ¼", "Error", MessageBox.ERROR);
					this.show();
					this.setVisible(true);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
				this.dispose();
				MessageBox.post("Ա˱ʧ", "Error", MessageBox.ERROR);
				this.show();
				this.setVisible(true);
			}
		} else if (sourceObj.equals(this.celButton)) {
			this.dispose();
		} else if (sourceObj.equals(this.fileButton)) {
			selectFileButtonEvent();
		} else if (sourceObj.equals(this.personButton)) {
			if (userDialog == null) {
				userDialog = new AddUserDialog();
			} else {
				userDialog.showDialog();
			}
		}
	}

	/**
	 * ѡļеѡ
	 */
	public void selectFileButtonEvent() {
		jfc.setFileSelectionMode(1);// 趨ֻѡļ
		int state = jfc.showOpenDialog(null);// ˾ǴļѡĴ
		if (state == 1) {
			return;
		} else {
			File f = jfc.getSelectedFile();// fΪѡ񵽵Ŀ¼
			pathText.setText(f.getAbsolutePath());
		}
	}

	/**
	 * 
	 * 
	 * @author hub
	 * 
	 */
	private class AddUserDialog extends AbstractAIFDialog {

		private JPanel panel;
		private JPanel panel3;
		private JLabel lable;
		// private JTextField textField;
		private JButton okButton;
		private JButton celButton;
		// private MyTable table;
		private JTree userTree;
		TCGroupDialogTree treeDialog;

		public AddUserDialog() {
			super(true);

			init();
		}

		public void init() {
			System.out.println("ʼ");
			this.setTitle("֯ṹ");
			this.setPreferredSize(new Dimension(400, 300));

			lable = new JLabel("ѡֵ");

			panel = new JPanel(new BorderLayout());
			panel3 = new JPanel(new FlowLayout());

			okButton = new JButton("ȷ");
			okButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					okAction(e);
				}
			});
			celButton = new JButton("ȡ");
			celButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					celAction(e);
				}
			});
			// JPanel orgPanel = new JPanel(new BorderLayout());

			if (treeDialog == null) {
				treeDialog = new TCGroupDialogTree();
			}
			if (this.userTree == null) {

				TCSession session = (TCSession) AIFUtility
						.getCurrentApplication().getSession();
				this.userTree = treeDialog.getTcOrgTree(session, "NBTM");
			}
			// orgPanel.add(BorderLayout.CENTER, this.userTree);
			// panel2.add(lable);
			// panel2.add(BorderLayout.CENTER, );
			panel3.add(okButton);
			panel3.add(celButton);
			panel.add(panel3, BorderLayout.SOUTH);
			panel.add(new JScrollPane(this.userTree), BorderLayout.CENTER);
			this.add(panel, "Center");
			// this.setAlwaysOnTop(true);
			this.pack();
			this.centerToScreen();
			this.showDialog();
		}

		private void okAction(ActionEvent e) {

			Object selectObj = userTree.getLastSelectedPathComponent();
			if (selectObj instanceof DefaultMutableTreeNode) {
				Object userObj = ((DefaultMutableTreeNode) selectObj)
						.getUserObject();
				if (userObj instanceof TMomUserBean) {
					if (((TMomUserBean) userObj).getUser() instanceof TCComponentUser) {
						try {

							user = (TCComponentUser) ((TMomUserBean) userObj)
									.getUser();
							personName.setText(user
									.getStringProperty("user_name"));
							this.disposeDialog();
							System.out.println("get user = " + user);
						} catch (TCException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							// user = null;
							// personName.setText("");
						}
					} else {
						MessageBox.post("ѡԱ", "", MessageBox.ERROR);
						// user = null;
						// personName.setText("");
					}

				} else {
					MessageBox.post("ѡԱ", "", MessageBox.ERROR);
					// user = null;
					// personName.setText("");
				}
			} else {
				MessageBox.post("ѡԱ", "", MessageBox.ERROR);
				// user = null;
				// personName.setText("");
			}
		}

		private void celAction(ActionEvent e) {
			this.dispose();
		}

	}

}
