package nc.pub.filesystem.newui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;

import nc.bs.logging.Logger;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.sfbase.client.ClientToolKit;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
 
public class TestGaopaiyiAction extends AbstractAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4811264645182420117L;
	private TestFileContentPanel parentpanel ;
	public TestGaopaiyiAction(TestFileContentPanel para) {
		parentpanel=para;
		this.putValue(AbstractAction.NAME,NCLangRes.getInstance().getStrByID("sftemp", "GaopaiyiAction-0000")/*高拍仪*/);
		this.putValue(Action.SHORT_DESCRIPTION, NCLangRes.getInstance().getStrByID("sftemp", "GaopaiyiAction-0000")/*高拍仪*/);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		GaopaiyiDialog dlg =  new GaopaiyiDialog();
		dlg.setSize(new Dimension(850,670));
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
//		Insets insets =Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration());
		dlg.setLocation(new Point(((int)(dim.width-850)/2),(int)(dim.height-670)/2));
		
		dlg.setResizable(true);
		
		dlg.setModal(true);
		dlg.setVisible(true);
		
	}
	
	protected class GaopaiyiDialog extends JDialog{
		public String tempfile ="";
		boolean firstloadflag = true ;
		TestTakePotoPanel panel =null ;
		public GaopaiyiDialog() {
			//用户ID+时间time值 作为临时文件夹名
			tempfile = WorkbenchEnvironment.getInstance().getLoginUser().getCuserid()+new Date().getTime();
			String catchpath =ClientToolKit.getNCCodeBase();
			//临时文件夹绝对位置
			tempfile=catchpath+File.separatorChar+"attach"+File.separatorChar+tempfile;
			File file = new File(tempfile);
			if(!file.exists()){
				file.mkdirs();				
			}
			panel= new TestTakePotoPanel(parentpanel,tempfile,GaopaiyiDialog.this);
			this.add(panel);
			//
			
			
			//窗口事件
			this.addWindowListener(new WindowAdapter(){
				//高拍仪。。只能在 加载界面时做初始化，所以在加载完dialog后 延时1s开始操作
				@Override
				public void windowActivated(WindowEvent e) {
					try{
						if(firstloadflag){
							firstloadflag = false;
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e1) {
								// 
								Logger.error(e1);
							}
	//						panel.getGpypanel().OpenScanner();
							if(!panel.getGpypanel().IsConnect()){
								MessageDialog.showErrorDlg(GaopaiyiDialog.this, NCLangRes.getInstance().getStrByID("sftemp", "GaopaiyiAction-0003")/*未连接高拍仪!*/, NCLangRes.getInstance().getStrByID("sftemp", "GaopaiyiAction-0003")/*未连接高拍仪!*/);
							}
							panel.getGpypanel().StartPreview();
							panel.getGpypanel().SetScanSize(0);
							panel.getGpypanel().SetVideoRotate(0);
						}
					}catch (Exception e1) {
						// 
						MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("sftemp", "GaopaiyiAction-0001")/*高拍仪错误*/, NCLangRes.getInstance().getStrByID("sftemp", "GaopaiyiAction-0002")/*高拍仪初始化失败，请检查设备重新打开本窗口！*/);
					}
				}
				@Override
				public void windowClosing(WindowEvent e) {
					try{
						
						panel.getGpypanel().StopPreview();
						if(panel.getGpypanel().IsConnect()){		
							panel.getGpypanel().CloseScanner();
						}
						panel.getGpypanel().dispose();
						GaopaiyiDialog.this.removeAll();
						
						//删除临时文件,
						try {
							FileBroseConst.deleteFile(tempfile);
						} catch (Exception e1) {
//							MessageDialog.showErrorDlg(parentpanel, "错误", "删除临时文件出错");
							Logger.error(e1);
						}

					}catch (Exception e1) {
						//
						Logger.error(e1);
					}
					
				}
			});
		}
		
		
	}
}
