package nc.pub.filesystem.newui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.border.LineBorder;

import nc.bs.logging.Logger;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.ui.pub.beans.UIPanel;
import nc.ui.pub.beans.UIScrollPane;
import nc.ui.pub.filesystem.FileManageServletClient;
import nc.vo.pub.filesystem.NCFileVO;

public class ThSmallImageBrowsePanel extends UIPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<NCFileVO> listfilevo;
	private ThFileContentPanel parentpanel = null;
	private HashMap<String, String> pkandnameMap = new HashMap<String, String>();
	public ThSmallImageBrowsePanel(ArrayList<NCFileVO> listvo,
			ThFileContentPanel pane) {
		listfilevo = listvo;
		if(listfilevo!=null){
			for(NCFileVO filevo :listfilevo){
				if(!pkandnameMap.containsKey(filevo.getPk())){
					pkandnameMap.put(filevo.getPk(), filevo.getName());				
				}
			}
		}
		parentpanel = pane;
		initUI();
		SmallImageSwingWorker work = new SmallImageSwingWorker(getImagespane());
		work.execute();
		this.updateUI();
	}

	private UIScrollPane imagescorpane = null;

	public UIScrollPane getImagescorpane() {
		if (imagescorpane == null) {
			imagescorpane = new UIScrollPane(){
				@Override
				public void setBounds(int x, int y, int width, int height) {
					// TODO Auto-generated method stub
					super.setBounds(x, y, width, height);
				}
			};
			imagescorpane.setViewportView(getImagespane());
				imagescorpane.setPreferredSize(new Dimension(200,200));
			imagescorpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//					imagescorpane.setViewportView(getImagespane());
		}
		return imagescorpane;

	}

	public UIPanel getImagespane() {
		if (imagespane == null) {			
			imagespane = new UIPanel()
			{
				@Override
				public void setBounds(int x, int y, int width, int height) {
					int rowheight = 96+10+20 ;
					int colwidth =106 ;
					int colnum = (width)/colwidth;
					int rownum = 0;
					if(listfilevo.size()%colnum ==0){
						rownum =listfilevo.size()/colnum ;
					}else{
						rownum =(listfilevo.size()/colnum+1);
					}
					int newheight =rownum *rowheight+10;
					super.setBounds(x, y, width, newheight);
				}
			};
			
			imagespane.setPreferredSize(new Dimension(240,100));
			
			FlowLayout f =new FlowLayout();
			
			f.setVgap(10);
			f.setHgap(10);
			f.setAlignment(FlowLayout.LEFT);
			imagespane.setLayout(f);
		}
		return imagespane;
	}

	private UIPanel imagespane = null;

	private void initUI() {
		this.setLayout(new BorderLayout());
		this.add(getImagescorpane(),BorderLayout.CENTER);

	}
	
//	class SmallImageLabel extends JLabel{
//		String imagepath = "";
//		public SmallImageLabel(String path){
//			imagepath =path ;
//		}
//		public void paint(Graphics g) {
//			ImageIcon ico =new ImageIcon(imagepath);
//			
//			Image image = ico.getImage();
//			g.drawImage(image, (96-ico.getIconWidth())/2,(96-ico.getIconHeight())/2 ,SmallImageLabel.this);
//			image.flush();
//		}
//	}
	class SmallImageSwingWorker extends SwingWorker<Object, ArrayList<File>> {
		private final UIPanel imagepane;

		public SmallImageSwingWorker(UIPanel myimagepane) {
			imagepane = myimagepane;
		}

		@Override
		protected Object doInBackground() throws Exception {			
			if(listfilevo==null) return null ;
//			Thread.sleep(1000);
			for (int i = 0; i < listfilevo.size(); i++) {				
				if ((i + 1) % 5 == 0 || (i + 1) == listfilevo.size()) {
					int startindext = 0;
					if((i + 1) % 5 == 0 ){
						startindext= i-4;// (i+1)-5;
						ArrayList<File> listfile = loadData(startindext, i);
						publish(listfile );
					}else{
						startindext= (i + 1)-((i + 1) % 5);// (i+1)-5;
						ArrayList<File> listfile = loadData(startindext, i);						
						publish(listfile );
					}
				}
//				Thread.sleep(1000);
			}
			//				

			return null;
		}

		private ArrayList<File> loadData(int startindext, int end) {
			String dsname = WorkbenchEnvironment.getInstance()
					.getLoginBusiCenter().getDataSourceName();
			String strpkcom = "";
			ArrayList<File>  listfile = new ArrayList<File>();

			for (int i = startindext; i <= end; i++) {
				String pk = listfilevo.get(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 = listfilevo.get(i).getPk();
				String filename = FileBroseConst
						.getSmallImageCacheFileFullpath(pk);
				File imagefile = new File(filename);
				listfile.add(imagefile);
			}
			return listfile;
		}

		@Override
		protected void process(List<ArrayList<File>> chunks) {
			for (ArrayList<File> list : chunks) {
				for(File file :list){
//					SmallImageLabel label = new SmallImageLabel(file.getAbsolutePath());
					//缩率图panel，上图 下名
					JPanel pan = new JPanel();
//					pan.setPreferredSize(new Dimension(96,116));
//					pan.setSize(new Dimension(96,0));
					pan.setLayout(new BorderLayout());
					//缩略图					
					JLabel label = new JLabel();
					label.setPreferredSize(new Dimension(96,96));
					label.setBorder(new LineBorder(Color.gray));
					ImageIcon icon = new ImageIcon(file.getAbsolutePath());
					label.setIcon(icon);
					label.updateUI();
					//文件名
					JLabel lblfilename = new JLabel();
					
					lblfilename.setHorizontalAlignment(SwingConstants.CENTER);
					String filename = file.getName();
					String pk = filename.substring(0,filename.lastIndexOf("."));
					String turename = pkandnameMap.get(pk);
					lblfilename.setText(turename);
					lblfilename.setPreferredSize(new Dimension(96,20));
//					lblfilename.setSize(new Dimension(96,20));
//					lblfilename.setMaximumSize(new Dimension(96,20));
					//
					pan.add(label,BorderLayout.CENTER);
					pan.add(lblfilename,BorderLayout.SOUTH);
					pan.updateUI();
					imagepane.add(pan);					
				}
			
				imagepane.revalidate();
				
//				getImagescorpane().revalidate();
			}
		
		}
	}
}
