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.net.URL;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.TreePath;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.action.AbstractNCAction;
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.UITextField;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.FileTypeConst;
import nc.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.filesystem.NCFileVO;
import net.miginfocom.swing.MigLayout;

public class TestUploadURLAction   extends AbstractNCAction{
	private static final long serialVersionUID = 1L;
	private TestFileContentPanel parentpanel ;
	public TestUploadURLAction(TestFileContentPanel para){
		parentpanel = para;
		this.putValue(AbstractAction.NAME,NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0000")/*上传 URL*/);
		this.putValue(Action.SHORT_DESCRIPTION, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0000")/*上传 URL*/);
//		this.putValue(AbstractAction.SMALL_ICON, ClientToolKit.loadImageIcon("images/filesystem/newfolder.png"));
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		//TODO 弹出上传附件对话框，url，别名
		UploadURLDialog dlg = new UploadURLDialog();
		dlg.showModal();
		
	}
	private class UploadURLDialog extends UIDialog {
		public UploadURLDialog(){
			super(parentpanel, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0001")/*上传URL*/);
			
			
			this.setResizable(true);
			initUI();
			this.setSize((int)getMainPane().getPreferredSize().getWidth()+20, 180);
		}
		
		private void initUI(){
			UploadURLDialog.this.getContentPane().setLayout(new BorderLayout());
			UploadURLDialog.this.getContentPane().add(BorderLayout.CENTER,getMainPane());
			UploadURLDialog.this.getContentPane().add(BorderLayout.SOUTH,getBottombtnPane());

		}
//		private JScrollPane jscpane ;
//		private JScrollPane getJscpane(){
//			if(jscpane ==null){
//				jscpane = new JScrollPane();
//				jscpane.setViewportView(getMainPane());
//			}
//			return jscpane;
//		}
		private JPanel manPane ;
		public JPanel getMainPane(){
			if(manPane == null){
				manPane = new JPanel();
//				manPane.set
				manPane.setBorder(new EmptyBorder(20,20,20,20));
				manPane.setLayout(new MigLayout("","[]rel[]unrel[]rel[]","[]20[]"));
				manPane.add(getLblalias(),"");
				manPane.add(getTxtalias(),"w 300:300:300,wrap");
				manPane.add(getLblURL(),"");
				manPane.add(getTxtURL(),"w 300:300:300,wrap");
			}
			return manPane;
		}
		private JLabel lblalias;
		public JLabel getLblalias(){
			if(lblalias == null){
				lblalias = new JLabel(NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0002")/*附件名*/);
				lblalias.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			return lblalias ;
		}
		private UITextField txtalias;
		public UITextField getTxtalias(){
			if(txtalias == null){
				txtalias = new UITextField();
				txtalias.setShowMustInputHint(true);
			}
			return txtalias ;
		}
		
		private JLabel lblURL;
		public JLabel getLblURL(){
			if(lblURL == null){
				lblURL = new JLabel("URL");
				lblURL.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			return lblURL ;
		}
		private UITextField txtURL;
		public UITextField getTxtURL(){
			if(txtURL == null){
				txtURL = new UITextField();
				txtURL.setShowMustInputHint(true);
				//默认显示http://
				txtURL.setText("http://");
			}
			return txtURL ;
		}
		
		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 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(txtalias.getText()==null ||txtalias.getText().trim().isEmpty()){
							MessageDialog.showErrorDlg(UploadURLDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0005")/*提示*/, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0006")/*附件名不能为空！*/);
							return ;
						}
						if(txtURL.getText()==null ||txtURL.getText().trim().isEmpty()){
							MessageDialog.showErrorDlg(UploadURLDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0005")/*提示*/, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0007")/*URL不能为空！*/);
							return ;
						}

						try{
							//URL是否正确
							URL url = new URL(txtURL.getText());
						}catch (Exception e1) 
						{
							MessageDialog.showErrorDlg(UploadURLDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0005")/*提示*/, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0008")/*URL不合法！*/);
							return ;
						}
						TreePath path = parentpanel.getPatchTree().getSelectionPath();
						if (path == null) {
							path = new TreePath(parentpanel.rootNode.getPath());
						}
						NCFileNode node = (NCFileNode) path.getLastPathComponent();
						String filepath =node.getFullPath();
						filepath =filepath+"/"+txtalias.getText() ;
						//(creator,isfolder,filepath, lasttime, filetype,pk_doc)";
						NCFileVO newfilevo = new NCFileVO();  
						String creator =WorkbenchEnvironment.getInstance().getLoginUser().getCuserid();
						newfilevo.setCreator(creator);
						newfilevo.setPath(filepath);
						newfilevo.setFolder(false);
						newfilevo.setFiletype(FileTypeConst.URL);
						newfilevo.setTime(new Date().getTime());
						newfilevo.setPk_doc(txtURL.getText());
						IFileSystemService fileservice = NCLocator.getInstance().lookup(IFileSystemService.class);
						boolean flag = false ;
						try {
							flag =fileservice.insertURLFileVO(newfilevo);
						} catch (BusinessException e1) {
							Logger.error(e1);
						}
						if(flag ){
							//刷新界面 
							parentpanel.refreshUI(node, newfilevo);
						}
						UploadURLDialog.this.closeOK();
					}
				});
				btncancel.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						// 
						UploadURLDialog.this.closeCancel();
						
					}
				});
				btnpanel.add(btnsave);
				btnpanel.add(btncancel);
			}
			return btnpanel ;
		}
	}
}
