package nc.pub.filesystem.newui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.pub.billcode.ui.ButtonPanel;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIDialog;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.NCFileNode;
import nc.vo.pub.filesystem.NCFileVO;

public class ThFileMoveDialog extends UIDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3151163400123581252L;
	ThFileContentPanel fielcontentpanel ;
	public ThFileMoveDialog(ThFileContentPanel para){	
		fielcontentpanel = para ;
		int row = ThFileMoveDialog.this.fielcontentpanel.getPatchInfoTable().getSelectedRow();
		if(row==-1){
			return ;
		}
		initUI();		
		this.setTitle(NCLangRes.getInstance().getStrByID("sftemp", "FileMoveDialog-0000")/*移动*/);
	}
	
	 private JPanel mainpanel;
	public JPanel getMainpanel(){
		if(mainpanel== null){
			mainpanel = new JPanel();
			SpringLayout layout = new SpringLayout();
			mainpanel.add(getLblmovefile());
			mainpanel.add(getComboxfolder());
//			mainpanel.add(getLinepanel());
			mainpanel.add(getBottombtnPane());
			mainpanel.setLayout(layout);
			
			layout.putConstraint(SpringLayout.NORTH, getLblmovefile(), 35,SpringLayout.NORTH , mainpanel);
			layout.putConstraint(SpringLayout.WEST, getLblmovefile(), 35,SpringLayout.WEST , mainpanel);
//			layout.putConstraint(SpringLayout.EAST, getLblmovefile(), 0,SpringLayout.EAST , mainpanel);
			
			layout.putConstraint(SpringLayout.WEST, getComboxfolder(), 10,SpringLayout.EAST , getLblmovefile());
			layout.putConstraint(SpringLayout.EAST, getComboxfolder(), -35,SpringLayout.EAST , mainpanel);
			layout.putConstraint(SpringLayout.NORTH, getComboxfolder(), 35,SpringLayout.NORTH , mainpanel);			
			
//			layout.putConstraint(SpringLayout.SOUTH, getLinepanel(), -75,SpringLayout.SOUTH , mainpanel);
//			layout.putConstraint(SpringLayout.WEST, getLinepanel(), 0,SpringLayout.WEST , mainpanel);
//			layout.putConstraint(SpringLayout.EAST, getLinepanel(), 0,SpringLayout.EAST , mainpanel);
			
			layout.putConstraint(SpringLayout.WEST ,getBottombtnPane(), 0,SpringLayout.WEST , mainpanel);
			layout.putConstraint(SpringLayout.EAST ,getBottombtnPane(), 0,SpringLayout.EAST , mainpanel);
//			layout.putConstraint(SpringLayout.SOUTH, getBottombtnPane(), 0,SpringLayout.SOUTH , mainpanel);
			layout.putConstraint(SpringLayout.SOUTH, getBottombtnPane(), -0,SpringLayout.SOUTH , mainpanel);
			
		}
		return mainpanel ;
	}
	private JPanel bottombtnPane;
	public JPanel getBottombtnPane(){
		if(bottombtnPane==null){
			bottombtnPane = new JPanel();
//			bottombtnPane.setPreferredSize(new Dimension(290,40));
			FlowLayout  fl = new FlowLayout();
			fl.setAlignment(FlowLayout.RIGHT);
			bottombtnPane.setLayout(fl);
			bottombtnPane.add(getBtnpanel());
			
			bottombtnPane.setBackground(FileColorConst.BOTTOMCOLOR);
//			bottombtnPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			bottombtnPane.setBorder(new MyBorder(0,0,0,0));
		}
		
		return bottombtnPane;
	}
	
//	private JPanel dialogContenPane;
//	public JPanel getDialogpane(){
//		if(dialogContenPane == null){
//			dialogContenPane = new JPanel();
//			dialogContenPane.add(getMainpanel());
//		}
//		return dialogContenPane;
//	}
	private void initUI(){
		setSize(290, 145);
		this.getContentPane().add(getMainpanel());
	}
	private JLabel lblmovefile;
	public JLabel getLblmovefile(){
		if(lblmovefile == null){
			lblmovefile = new JLabel(NCLangRes.getInstance().getStrByID("sftemp", "FileMoveDialog-0001")/*移动到*/);
		}
		return lblmovefile ;
	}
	private JComboBox Comboxfile;
	public JComboBox getComboxfolder(){
		if(Comboxfile == null){
			Comboxfile = new JComboBox();
			
			NCFileNode node =(NCFileNode)fielcontentpanel.getTreemodel().getRoot();
			String rootpath = node.getFullPath();
			//隐藏根目录前字符串
//			String unvisstr = "";
//			if(rootpath.contains("/")){
//				unvisstr=rootpath.substring(0,rootpath.lastIndexOf("/")+1);
//			}
			
			Enumeration<NCFileNode>  e =node.breadthFirstEnumeration();
			while(e.hasMoreElements()){
				NCFileNode enode =(NCFileNode)e.nextElement();
				if(!enode.isRoot()){
					MoveFolder mf = new MoveFolder();
					if(rootpath!=""){
						mf.dispname=NCLangRes.getInstance().getStrByID("sftemp", "FileBrowseGPYPotoDialog-0010", null, new String[]{enode.getFullPath().substring(rootpath.length())})/*全部{0}*/;							
					}else{
						mf.dispname=enode.getFullPath();	
					}
					mf.value=enode.getFullPath();
					Comboxfile.addItem( mf);					
				}
			}
			
		}
		return Comboxfile;
	}
	private ButtonPanel btnpanel ;
	public ButtonPanel getBtnpanel(){
		if(btnpanel == null){
			btnpanel = new ButtonPanel();
			btnpanel.setOpaque(false);
			btnpanel.getBtnOK().addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					int row = ThFileMoveDialog.this.fielcontentpanel.getPatchInfoTable().getSelectedRow();
					if(row==-1){
						return ;
					}
					NCFileVO filevo = (NCFileVO)ThFileMoveDialog.this.fielcontentpanel.getfiletablemodel().getVO(row);
					String oldpath = filevo.getFullPath();
					String filenodepath =filevo.getParentpath();
					String oldtreepath = "";
						
					if(filevo.getParentpath().equalsIgnoreCase(ThFileMoveDialog.this.fielcontentpanel.getRootDirStr())){
						oldtreepath="root";
					}else{
						oldtreepath=filevo.getParentpath();
					}
					
					String newfullpath ="";
					String filenewpath = ((MoveFolder)getComboxfolder().getSelectedItem()).getValue();
					if(filenewpath.endsWith("/")){
						newfullpath=filenewpath+filevo.getName();
					}else{
						newfullpath=filenewpath+"/"+filevo.getName();
					}
					//假如根本没有移动
					if(oldpath.equalsIgnoreCase(newfullpath)){
						MessageDialog.showErrorDlg(ThFileMoveDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0005")/*提示*/, NCLangRes.getInstance().getStrByID("sftemp", "FileMoveDialog-0002")/*文件在本文件夹下，不用移动！*/);
						return;
					}
					filevo.setPath(newfullpath);
					
					NCFileNode node = (NCFileNode)fielcontentpanel.getPatchTree().getSelectionPath().getLastPathComponent();
					IFileSystemService service = NCLocator.getInstance().lookup(IFileSystemService.class);					
					try {
						//更新树
						boolean b =service.updateFileAttribute(filevo, oldpath);
						if(b){
							//更新界面
							//树上的缓存
							ThFileMoveDialog.this.fielcontentpanel.getTreenodefilecacheMap().get(filenodepath).remove(filevo);
							ArrayList<NCFileVO> arr = new ArrayList<NCFileVO>();
							arr.addAll(ThFileMoveDialog.this.fielcontentpanel.getTreenodefilecacheMap().get(filevo.getParentpath()));
							arr.add(filevo);
							//更新table
							ThFileMoveDialog.this.fielcontentpanel.getTreenodefilecacheMap().put(filevo.getParentpath(), arr);
							NCFileVO [] arrtablevo =ThFileMoveDialog.this.fielcontentpanel.getTreenodefilecacheMap().get(oldtreepath).toArray(new NCFileVO[0]);
							ThFileMoveDialog.this.fielcontentpanel.getfiletablemodel().clearTable();
							if(node.getFullPath().equals(fielcontentpanel.rootDirStr)){
								ThFileMoveDialog.this.fielcontentpanel.getfiletablemodel().addVO(fielcontentpanel.getTreenodefilecacheMap().get("root").toArray(new NCFileVO[0]));
							}else{
								ThFileMoveDialog.this.fielcontentpanel.getfiletablemodel().addVO(fielcontentpanel.getTreenodefilecacheMap().get(node.getFullPath()).toArray(new NCFileVO[0]));
							}
							//状态栏
							ThFileMoveDialog.this.fielcontentpanel.refreshStatuBar();
						}else{
							//有同名 
							MessageDialog.showErrorDlg(ThFileMoveDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "UploadURLAction-0005")/*提示*/, NCLangRes.getInstance().getStrByID("sftemp", "FileMoveDialog-0003")/*不能移动，该文件下有同名文件！*/);
						}
						ThFileMoveDialog.this.closeOK();
					} catch (BusinessException e1) {
						Logger.error(e1);
					}
				}	
			});
			btnpanel.getBtnCancel().addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					ThFileMoveDialog.this.closeCancel();
					
				}
			});
		}
		return btnpanel ;
	}
	private class MoveFolder{
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
		public String getDispname() {
			return dispname;
		}
		public void setDispname(String dispname) {
			this.dispname = dispname;
		}
		private String value ;
		private String dispname ;
		@Override
		public String toString() {
			
			return this.dispname;
		}
	}
	
}
