package nc.pub.filesystem.newui;

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import nc.bs.logging.Logger;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.IImageFetch;
import nc.ui.pub.beans.ImageBrowser;
import nc.ui.pub.beans.ImageObject;
import nc.ui.pub.beans.ImagePreviewPanel;
import nc.ui.pub.beans.ImageSelectionEvent;
import nc.ui.pub.beans.ImageSelectionListener;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.ScaleSlider;
import nc.ui.pub.beans.UIPanel;
import nc.ui.pub.filesystem.FileManageServletClient;
import nc.vo.pub.filesystem.NCFileVO;
import nc.vo.pub.lang.UFDouble;

public class TestPPTBrosePanel extends UIPanel {
	//需要传过来scaleSlider；
	private ScaleSlider scaleSlider;
	private ArrayList<NCFileVO> listfilevo;
	private TestFileContentPanel parentpanel = null;

	public TestPPTBrosePanel(ArrayList<NCFileVO> listvo, TestFileContentPanel pane,
			ScaleSlider parascaleSlider) {
		scaleSlider = parascaleSlider;
		listfilevo = listvo;
		parentpanel = pane;
		initUI();
		initListener();
		FileImageObject imageobj = (FileImageObject)getImagebrowser().getFirstIamgeObject();
		if(imageobj!=null){			
			fillImageToPrePane(imageobj);
		}
	}

	private void fillImageToPrePane(FileImageObject imageobj) {
		BufferedImage image = null;
		String fullpath = imageobj.getFullpath();
		String pk = imageobj.getImageID();
		File bigimgcache = new File(FileBroseConst
				.getBigImageCacheFileFullpath(pk, fullpath));
		String dsName = WorkbenchEnvironment.getInstance()
				.getLoginBusiCenter().getDataSourceName();
		FileOutputStream out = null;
		try {
			if (bigimgcache.exists()) {
				image = ImageIO.read(bigimgcache);
			} else {
				out = new FileOutputStream(bigimgcache);
				FileManageServletClient.downloadFile(dsName,
						fullpath, out);
				image = ImageIO.read(bigimgcache);
			}
		} catch (Exception ex) {
			Logger.error(ex);
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (Exception innerex) {
				Logger.error(innerex);
			}
		}
		// Image image=
		// Toolkit.getDefaultToolkit().getImage(e.getImageObject().getImageID());
		if (image == null) {
			if(bigimgcache.exists()){
				bigimgcache.delete();
			}
			MessageDialog.showErrorDlg(parentpanel, NCLangRes.getInstance().getStrByID("sftemp", "PPTBrosePanel-0000")/*下载图片出错*/,
					NCLangRes.getInstance().getStrByID("sftemp", "PPTBrosePanel-0001")/*下载图片出错,无法浏览！*/);
		} else {
			getpreviewPnl().setImage(image);
			double  scale =getpreviewPnl().getScale();
			int ivalue = (int)(scale*100);
			scaleSlider.setValue(ivalue);
			String filename = fullpath.substring( fullpath.lastIndexOf("/")+1);
			getLblfilename().setText(filename);
			String text ="" +(imageobj.getIndex()+1)+"/" + listfilevo.size() + "";
			getLblimageinfo().setText(text);
		}
	}

	private void initListener() {
		scaleSlider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				// 
				UFDouble d = new UFDouble(scaleSlider.getValue());
				UFDouble scale = d.div(100);
				getpreviewPnl().setImageScale(scale.doubleValue());
			}

		});
		
	}

	private void initUI() {
		this.setLayout(new BorderLayout());
		this.add(getImagepane(), BorderLayout.CENTER);
		this.add(getBroserpane(), BorderLayout.EAST);
	}

	private TestPatchFileImageFetch filefech;

	public TestPatchFileImageFetch getFilefech() {
		if (filefech == null) {
			filefech = new TestPatchFileImageFetch(listfilevo
					.toArray(new NCFileVO[0]));
		}
		return filefech;
	}

	private UIPanel broserpane;
	// 大图panel＋lblpanel
	private UIPanel imagepane;
	private JLabel lblimageinfo;
	// 第几张 共 几张
	private UIPanel infopane;
	// 大图控件 panel
	private UIPanel imgpreviewpane;

	UIPanel getinfopane() {
		if (infopane == null) {
			infopane = new UIPanel();
			infopane.add(getLblfilename());
			infopane.add(getLblimageinfo());
		}
		return infopane;
	}

	/** 大图 的 panel,里边一个 大图浏览控件 */
	UIPanel getImgpreviewpane() {
		if (imgpreviewpane == null) {
			imgpreviewpane = new UIPanel();
			imgpreviewpane.setBorder(new EmptyBorder(20, 10, 5, 10));
			imgpreviewpane.setLayout(new BorderLayout());
			imgpreviewpane.add(getpreviewPnl(), BorderLayout.CENTER);
		}
		return imgpreviewpane;
	}

	// 控件1 浏览控件
	private ImageBrowser imagebrowser;
	// 控件2 滑动 条
	// final ScaleSlider scaleSlider = new ScaleSlider();
	// //控件3 图片
	private ImagePreviewPanel previewPnl;

	private ImagePreviewPanel getpreviewPnl() {
		if (previewPnl == null) {
			previewPnl = new ImagePreviewPanel(scaleSlider);
		}
		return previewPnl;
	}

	public ImageBrowser getImagebrowser() {
		if (imagebrowser == null) {
			imagebrowser = new ImageBrowser(ImageBrowser.VERTICAL, listfilevo
					.size(), getFilefech());
			imagebrowser.addChangeListener(new ImageSelectionListener() {
				@Override
				public void valueChanged(ImageSelectionEvent e) {

					// //TODO 返回图片
					FileImageObject fimgobj = (FileImageObject) e
							.getImageObject();
					fillImageToPrePane(fimgobj);
				}
			});
		}
		return imagebrowser;
	}

	public UIPanel getBroserpane() {
		if (broserpane == null) {
			broserpane = new UIPanel();
			broserpane.add(getImagebrowser());

		}
		return broserpane;
	}

	public UIPanel getImagepane() {
		if (imagepane == null) {
			imagepane = new UIPanel();
			imagepane.setLayout(new BorderLayout());
			imagepane.add(getImgpreviewpane(), BorderLayout.CENTER);
			imagepane.add(getinfopane(), BorderLayout.SOUTH);
		}
		return imagepane;
	}
	private JLabel lblfilename = null ;
	private JLabel getLblfilename(){
		if(lblfilename== null ){
			lblfilename = new JLabel();
			
		}
		return lblfilename ;
	}
	public JLabel getLblimageinfo() {
		if (lblimageinfo == null) {
			lblimageinfo = new JLabel();
			lblimageinfo.setText("" + listfilevo.size() + "");
		}
		return lblimageinfo;
	}

}

// 取图片的类
class TestPatchFileImageFetch implements IImageFetch {
	private int pagenum = 5;
	NCFileVO[] arrimagefilevo = null;

	public TestPatchFileImageFetch(NCFileVO[] pimagefilevo) {
		arrimagefilevo = pimagefilevo;
	}

	@Override
	public ImageObject[] getImage(int page) {

		int startindext = (page - 1) * pagenum;
		int end = page * pagenum - 1;

		if (end > (arrimagefilevo.length - 1)) {
			end = arrimagefilevo.length - 1;
		}
		ImageObject[] rtnimgObj = new FileImageObject[end - startindext + 1];

		ArrayList<String> pklist = new ArrayList<String>();
		String dsName = WorkbenchEnvironment.getInstance().getLoginBusiCenter()
				.getDataSourceName();

		String strpkcom = "";
		// 如果文件不存在,得到要下载的pk
		for (int i = startindext; i <= end; i++) {
			String pk = arrimagefilevo[i].getPk();
			String filename = FileBroseConst.getSmallImageCacheFileFullpath(pk);
			File imagefile = new File(filename);
			if (!imagefile.exists()) {
				if (strpkcom.isEmpty()) {
					strpkcom = pk;
				} else {
					strpkcom = strpkcom + "," + pk;
				}

			}
		}
		//			
		if (!strpkcom.isEmpty()) {
			try {
				FileManageServletClient
						.downloadSmallImagebypk(dsName, strpkcom);
			} catch (Exception e) {
				Logger.error(e);
			} finally {

			}
		}
		for (int i = startindext; i <= end; i++) {
			String pk = arrimagefilevo[i].getPk();
			String filename = FileBroseConst.getSmallImageCacheFileFullpath(pk);
			File imagefile = new File(filename);
			BufferedImage image = null;
			try {
				image = ImageIO.read(imagefile);
			} catch (IOException e) {
				Logger.error(e);
			}
			rtnimgObj[i-startindext] = new FileImageObject(arrimagefilevo[i]
					.getFullPath(), pk, new ImageIcon(image), i);
		}
		return rtnimgObj;
	}

}