package nc.pub.filesystem.newui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.pubitf.rbac.IUserPubService;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIButton;
import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.beans.UIPanel;
import nc.vo.ml.MultiLangUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.FileTypeConst;
import nc.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.filesystem.NCFileVO;
import nc.vo.sm.UserVO;
import net.miginfocom.swing.MigLayout;

public class TestFilePatchAttrDialog extends UIDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7993565512938510101L;
	private TestFileContentPanel parentpane;
	private JLabel lblfilename;
	private JTextField txtfilename;
	private JLabel lblfilesize;
	private JTextField txtfilesize;
	private JLabel lblfiletype;
	private JTextField txtfiletype;
	private JLabel lblfilescantimes;
	private JTextField txtfilescantimes;
	private JLabel lblfilecreator;
	private JTextField textfilecreator;
	private JLabel lblcreatetime;
	private JTextField txtcreatetime;
	private JLabel lblfilemodifier;
	private JTextField txtfilemodifier;
	private JLabel lblfilemodifytime;
	private JTextField txtfilemodifytime;
	private JLabel lblfiledesc;
	private JTextArea txtareafiledesc;
	private JLabel lblinfo;

	public JLabel getLblinfo() {
		if (lblinfo == null) {
			lblinfo = new JLabel(NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0011")/*附件说明不超过200字*/);
			lblinfo.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return lblinfo;
	}

	private JPanel btnpanel;

	public JPanel getBtnpanel() {
		if (btnpanel == null) {
			btnpanel = new JPanel();
			btnpanel.setOpaque(false);
			UIButton btnsave = new UIButton(NCLangRes.getInstance().getStrByID(
					"sftemp", "UploadURLAction-0003")/* 确定 */);
			UIButton btncancel = new UIButton(NCLangRes.getInstance().getStrByID(
					"sftemp", "UploadURLAction-0004")/* 取消 */);
			btnsave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
				
					//检查输入合法性
					if(!checkValidate()){
						return;
					}
					
					NCFileNode selectednode = (NCFileNode) parentpane
							.getPatchTree().getSelectionPath()
							.getLastPathComponent();
					String selectedtreepath = selectednode.getFullPath();
					String sepath = "";

					int row = parentpane.getPatchInfoTable().getSelectedRow();
					NCFileVO oldvo = (NCFileVO) parentpane.getfiletablemodel()
							.getVO(row);
					NCFileVO newvo = oldvo.clone();
					newvo.setFiledesc(TestFilePatchAttrDialog.this
							.getTxtareafiledesc().getText());

					if (selectedtreepath.equals(parentpane.getRootDirStr())) {
						sepath = "root";
					} else {
						sepath = oldvo.getParentpath();
					}

					String path = "";
					int point = getTxtfilename().getText().lastIndexOf(".");
					// 如果没有输入扩展名，加上扩展名，输入了取值即可
					if ((point == -1 || !getTxtfiletype().getText()
							.equalsIgnoreCase(
									getFileType(getTxtfilename().getText())))
							&& !getTxtfiletype().getText().equals(
									FileTypeConst.URL)) {
						path = oldvo.getParentpath() + "/"
								+ getTxtfilename().getText() + "."
								+ getTxtfiletype().getText();
					} else {
						path = oldvo.getParentpath() + "/"
								+ getTxtfilename().getText();
					}

					newvo.setPath(path);
					// 修改人
					newvo.setModifier(InvocationInfoProxy.getInstance()
							.getUserId());
					// 修改的时间
					Date date = new Date();
					newvo.setModifytime(date.getTime());
					// 保存
					IFileSystemService service = NCLocator.getInstance()
							.lookup(IFileSystemService.class);
					try {
						boolean b = service.updateFileAttribute(newvo, oldvo
								.getFullPath());
						if (b) {
							// 更新table
							oldvo.setFiledesc(TestFilePatchAttrDialog.this
									.getTxtareafiledesc().getText());
							oldvo.setPath(path);
							oldvo.setModifier(newvo.getModifier());
							oldvo.setModifytime(newvo.getModifytime());
							parentpane.getfiletablemodel().clearTable();
							parentpane.getfiletablemodel().addVO(
									parentpane.getTreenodefilecacheMap().get(
											sepath).toArray(new NCFileVO[0]));
							parentpane.getfiletablemodel().refreshTable();
						} else {
							MessageDialog.showErrorDlg(parentpane, NCLangRes
									.getInstance().getStrByID("sftemp",
											"PasteAttachAction-0001")/* 错误 */,
									NCLangRes.getInstance().getStrByID(
											"sftemp",
											"FilePatchAttrDialog-0000")/*
																		 * 文件名重复!
																		 */);
						}

					} catch (BusinessException e1) {
						Logger.error(e1);
					}
					TestFilePatchAttrDialog.this.closeOK();
				}

				private boolean checkValidate() {
					if (getTxtfilename().getText() == null
							|| getTxtfilename().getText().isEmpty()) {
						MessageDialog.showErrorDlg(TestFilePatchAttrDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0012")/*错误*/, NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0013")/*附件名不能为空！*/);
						return false;
					}
					if(getTxtfilename().getText().length()>30){
						MessageDialog.showErrorDlg(TestFilePatchAttrDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0012")/*错误*/, NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0014")/*附件名不能超过30字！*/);
						return false;
					}
					if(getTxtareafiledesc().getText()!=null&&getTxtareafiledesc().getText().length()>200){
						MessageDialog.showErrorDlg(TestFilePatchAttrDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0012")/*错误*/, NCLangRes.getInstance().getStrByID("sftemp", "FilePatchAttrDialog-0015")/*附件描述长度不能超过200字！*/);
						return false;
					}
					return true;
				}
			});
			btncancel.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// 
					TestFilePatchAttrDialog.this.closeCancel();

				}
			});
			btnpanel.add(btnsave);
			btnpanel.add(btncancel);
		}
		return btnpanel;
	}

	public JLabel getLblfiledesc() {
		if (lblfiledesc == null) {
			
			lblfiledesc = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0001")/* 附件说明 */);
		}
		return lblfiledesc;
	}
private JScrollPane jscpanedesc;
private JScrollPane getJscpanedesc(){
	if(jscpanedesc==null){
		jscpanedesc = new JScrollPane(getTxtareafiledesc());
		
	}
	return jscpanedesc ;
}
	public JTextArea getTxtareafiledesc() {
		if (txtareafiledesc == null) {
			txtareafiledesc = new JTextArea();
			txtareafiledesc.setAutoscrolls(true);
			txtareafiledesc.setLineWrap(true);
			txtareafiledesc.setBorder(null);
		}
		return txtareafiledesc;
	}

	public JTextField getTextfilecreator() {
		if (textfilecreator == null) {
			textfilecreator = new JTextField();
		}
		return textfilecreator;
	}

	public JLabel getLblcreatetime() {
		if (lblcreatetime == null) {
			lblcreatetime = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0002")/* 创建时间 */);
		}
		return lblcreatetime;
	}

	public JTextField getTxtcreatetime() {
		if (txtcreatetime == null) {
			txtcreatetime = new JTextField();
			txtcreatetime.setEditable(false);
			txtcreatetime.setEnabled(false);
		}
		return txtcreatetime;
	}

	public JLabel getLblfilemodifytime() {
		if (lblfilemodifytime == null) {
			lblfilemodifytime = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0003")/* 修改时间 */);
		}
		return lblfilemodifytime;
	}

	public JTextField getTxtfilemodifytime() {
		if (txtfilemodifytime == null) {
			txtfilemodifytime = new JTextField();
			txtfilemodifytime.setEditable(false);
			txtfilemodifytime.setEnabled(false);
		}
		return txtfilemodifytime;
	}

	public JLabel getLblfilename() {
		if (lblfilename == null) {
			lblfilename = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0004")/* 附件名 */);
		}
		return lblfilename;
	}

	public JTextField getTxtfilename() {
		if (txtfilename == null) {
			txtfilename = new JTextField();
		}
		return txtfilename;
	}

	public JLabel getLblfilesize() {
		if (lblfilesize == null) {
			lblfilesize = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0005")/* 附件大小 */);
		}
		return lblfilesize;
	}

	public JTextField getTxtfilesize() {
		if (txtfilesize == null) {
			txtfilesize = new JTextField();
			// txtfilesize.setEditable(false);
			txtfilesize.setEnabled(false);

		}
		return txtfilesize;
	}

	public JLabel getLblfiletype() {
		if (lblfiletype == null) {
			lblfiletype = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0006")/* 附件类型 */);
		}
		return lblfiletype;
	}

	public JTextField getTxtfiletype() {
		if (txtfiletype == null) {
			txtfiletype = new JTextField();
			txtfiletype.setEditable(false);
			txtfiletype.setEnabled(false);
		}
		return txtfiletype;
	}

	public JLabel getLblfilescantimes() {
		if (lblfilescantimes == null) {
			lblfilescantimes = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0007")/* 浏览次数 */);
		}
		return lblfilescantimes;
	}

	public JTextField getTxtfilescantimes() {
		if (txtfilescantimes == null) {
			txtfilescantimes = new JTextField();
			// txtfilescantimes.setEditable(false);
			txtfilescantimes.setEnabled(false);
		}
		return txtfilescantimes;
	}

	public JLabel getLblfilecreator() {
		if (lblfilecreator == null) {
			lblfilecreator = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0008")/* 创建人 */);
		}
		return lblfilecreator;
	}

	public JTextField getTxtfilecreator() {
		if (textfilecreator == null) {
			textfilecreator = new JTextField();
			// textfilecreator.setEditable(false);
			textfilecreator.setEnabled(false);
		}
		return textfilecreator;
	}

	public JLabel getLblfilemodifier() {
		if (lblfilemodifier == null) {
			lblfilemodifier = new JLabel(NCLangRes.getInstance().getStrByID(
					"sftemp", "FilePatchAttrDialog-0009")/* 修改人 */);
		}
		return lblfilemodifier;
	}

	public JTextField getTxtfilemodifier() {
		if (txtfilemodifier == null) {
			txtfilemodifier = new JTextField();
			// txtfilemodifier.setEditable(false);
			txtfilemodifier.setEnabled(false);
		}
		return txtfilemodifier;
	}

	public TestFilePatchAttrDialog(TestFileContentPanel ppane) {
		this.parentpane = ppane;
		initUI();
		initData();
	}

	public TestFilePatchAttrDialog() {

		initUI();
		initData();
	}
//	private JScrollPane jscpane ;
//	private JScrollPane getJscpane(){
//		if(jscpane ==null){
//			jscpane = new JScrollPane();
//			jscpane.setViewportView(getMainPane1());
//		}
//		return jscpane;
//	}
	private JPanel mainpane;

	public JPanel getMainPane1() {
		if (mainpane == null) {
			mainpane = new UIPanel();
			mainpane.setOpaque(true);

			// SpringLayout springlayout = new SpringLayout();
			// mainpane.setLayout(springlayout);
			// mainpane.add(getLblfilename());
			// mainpane.add(getTxtfilename());
			// mainpane.add(getLblfiletype());
			// mainpane.add(getTxtfiletype());
			// mainpane.add(getLblfilesize());
			// mainpane.add(getTxtfilesize());
			// mainpane.add(getLblfilescantimes());
			// mainpane.add(getTxtfilescantimes());
			// mainpane.add(getLblfilecreator());
			// mainpane.add(getTxtfilecreator());
			// mainpane.add(getLblcreatetime());
			// mainpane.add(getTxtcreatetime());
			// mainpane.add(getLblfilemodifier());
			// mainpane.add(getTxtfilemodifier());
			// mainpane.add(getLblfilemodifytime());
			// mainpane.add(getTxtfilemodifytime());
			// mainpane.add(getLblfiledesc());
			// mainpane.add(getTxtareafiledesc());
			// mainpane.add(getBtnpanel());
			//			
			// springlayout.putConstraint(SpringLayout.WEST, getLblfilename(),
			// 35, SpringLayout.WEST, mainpane);

			MigLayout miglayout = new MigLayout("insets 25 35 25 35",
					"[fill]rel[fill]30[fill]rel[fill]",
					"[]10[]10[]10[]10[]10[]");

			mainpane.setLayout(miglayout);
			mainpane.add(getLblfilename(), "");
			mainpane.add(getTxtfilename(), "w 150:150:150");
			mainpane.add(getLblfiletype(), "");
			mainpane.add(getTxtfiletype(), "w 150:150:150, wrap ");
			mainpane.add(getLblfilesize(), "");
			mainpane.add(getTxtfilesize(), "w 150:150:150");
			mainpane.add(getLblfilescantimes(), "");
			mainpane.add(getTxtfilescantimes(), "w 150:150:150 ,wrap");
			mainpane.add(getLblfilecreator(), "");
			mainpane.add(getTxtfilecreator(), "w 150:150:150");
			mainpane.add(getLblcreatetime(), "");
			mainpane.add(getTxtcreatetime(), "w 150:150:150 ,wrap");
			mainpane.add(getLblfilemodifier(), "");
			mainpane.add(getTxtfilemodifier(), "w 150:150:150");
			mainpane.add(getLblfilemodifytime(), "");
			mainpane.add(getTxtfilemodifytime(), "w 150:150:150,wrap");
			mainpane.add(getLblfiledesc(), "");
			mainpane.add(getJscpanedesc(),
					"span 3,wrap,w 380:380:, h 120:120:120, growy");
			mainpane.add(getLblinfo(), "span 4");
			// mainpane.add(getTxtareafiledesc(),"span 3,wrap,,w 380:380:380, h 120:120:120");
			// mainpane.add(getBottombtnPane(),"span 4,align right,wrap");
		}
		return mainpane;
	}

	private JPanel bottombtnPane;

	public JPanel getBottombtnPane() {
		if (bottombtnPane == null) {
			bottombtnPane = new JPanel();
			FlowLayout fl = new FlowLayout();
			fl.setAlignment(FlowLayout.RIGHT);
			bottombtnPane.setLayout(fl);
			bottombtnPane.setBackground(FileColorConst.BOTTOMCOLOR);

			bottombtnPane.add(getBtnpanel());
			bottombtnPane.setBorder(new MyBorder(0, 0, 0, 0));
		}

		return bottombtnPane;
	}

	private JPanel borderpanel;

	public JPanel getBorderpanel() {
		if (borderpanel == null) {
			borderpanel = new JPanel();
			BorderLayout fl = new BorderLayout();

			borderpanel.setLayout(fl);
			borderpanel.add(BorderLayout.CENTER, getMainPane1());
			borderpanel.add(BorderLayout.SOUTH, getBottombtnPane());
			borderpanel.setBorder(new MyBorder(0, 0, 0, 0));
		}

		return borderpanel;
	}

	private void initUI() {
			
		
		this.setResizable(true);
		this.add(getBorderpanel());
		this.setTitle(NCLangRes.getInstance().getStrByID("sftemp",
				"FilePatchAttrDialog-0010")/* 编辑属性 */);
		this.setSize((int)getMainPane1().getPreferredSize().getWidth()+40, 350);
		
	}

	private void initData() {
		if (this.parentpane == null)
			return;
		int row = this.parentpane.getPatchInfoTable().getSelectedRow();
		NCFileVO filevo = (NCFileVO) this.parentpane.getfiletablemodel().getVO(
				row);
		getTxtfilename().setText(getFileName(filevo.getName()));
		getTxtfiletype().setText(getFileType(filevo));
		getTxtfilesize().setText(
				FileManageStringFormat.formatFileSize(filevo.getFileLen()));
		getTxtfilescantimes().setText(filevo.getScantimes() + "");
		UserVO creator = null;
		UserVO modifier = null;
		String[] arruserpk = { filevo.getCreator(), filevo.getModifier() };
		IUserPubService ser = NCLocator.getInstance().lookup(
				IUserPubService.class);
		UserVO[] arruservo = null;
		try {
			arruservo = ser.getUsersByPKs(arruserpk);
		} catch (BusinessException e) {
			Logger.error(e);
		}
		if (arruservo != null) {
			for (UserVO uvo : arruservo) {
				if (uvo.getCuserid().equals(filevo.getCreator())) {
					creator = uvo;
				}
				if (uvo.getCuserid().equals(filevo.getModifier())) {
					modifier = uvo;
				}
			}
		}
//		if (creator == null || creator.equals("")) {
//			creator = filevo.getCreator();
//		}
//		if (modifier == null || modifier.equals("")) {
//			modifier = filevo.getModifier();
//		}
		getTxtfilecreator().setText(getUserVOName(creator));
		getTxtcreatetime().setText(
				FileManageStringFormat.formatDateToString(filevo.getTime()));
		getTxtfilemodifier().setText(getUserVOName(modifier));
		getTxtfilemodifytime().setText(
				FileManageStringFormat.formatDateToString(filevo
						.getModifytime()));
		getTxtareafiledesc().setText(filevo.getFiledesc());
	}
	private String getUserVOName(UserVO uvo){
		if(uvo==null){
			return "" ;			
		}
		String s = null;
		try{
			switch (MultiLangUtil.getCurrentLangSeq()) {
				case 1:
					s = uvo.getUser_name();
					break;
				case 2:
					s = uvo.getUser_name2();
					break;
				case 3:
					s = uvo.getUser_name3();
					break;
				case 4:
					s = uvo.getUser_name4();
					break;
				case 5:
					s = uvo.getUser_name5();
					break;
				case 6:
					s = uvo.getUser_name6();
					break;
				default:
					s = uvo.getUser_name();
					break;
			}
		}catch (Exception e) {
			Logger.error(e);
		}
		return s ;
	}
	/**
	 * @param name
	 * @return 返回文件后缀名，包含"."
	 */
	private String getFileType(NCFileVO filevo) {
		String type = null;
		if (filevo.getFiletype() != null
				&& filevo.getFiletype().equalsIgnoreCase(FileTypeConst.URL)) {
			return FileTypeConst.URL;
		}
		if (filevo != null && filevo.getName() != null
				&& filevo.getName().contains(".")) {
			type = filevo.getName().substring(
					filevo.getName().lastIndexOf(".") + 1,
					filevo.getName().length());
		}
		return type;
	}

	private String getFileType(String name) {
		String type = null;
		if (name != null && name.contains(".")) {
			type = name.substring(name.lastIndexOf(".") + 1, name.length());
		}
		return type;
	}

	private String getFileName(String name) {
		String filename = null;
		if (name != null && name.contains(".")) {
			filename = name.substring(0, name.lastIndexOf("."));
		} else {
			filename = name;
		}
		return filename;
	}

}
