package nc.pub.filesystem.newui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import nc.bs.logging.Logger;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.ui.pub.beans.ImagePreviewPanel;
import nc.ui.pub.beans.ScaleSlider;
import nc.ui.pub.beans.UIButton;
import nc.ui.pub.beans.UILabel;
import nc.ui.pub.beans.UIPanel;
import nc.ui.pub.filesystem.FileManageServletClient;
import nc.uitheme.ui.ThemeResourceCenter;
import nc.vo.pub.filesystem.NCFileVO;
import nc.vo.pub.lang.UFDouble;

import com.borland.jbcl.layout.VerticalFlowLayout;

public class ThSingleImageBrowsePanel extends UIPanel {
	ScaleSlider scaleSlider =null;
	private ArrayList<NCFileVO> listfilevo ;
	private String prefIconPath = "themeres/control/accessory/arrow_left_normal.png";
	private String nextfIconPath = "themeres/control/accessory/arrow_right_normal.png";
	
	private String prefIconPath_rollOver = "themeres/control/accessory/arrow_left_mouseover.png";
	private String nextfIconPath_rollOver = "themeres/control/accessory/arrow_right_mouseover.png";
	
	private ThFileContentPanel parentpanel = null;
	private int currentimageindex =0;
	public ThSingleImageBrowsePanel(ArrayList<NCFileVO> listvo,ThFileContentPanel pane,ScaleSlider pscaleSlider){
		scaleSlider =pscaleSlider;
		listfilevo = listvo ;
		parentpanel =pane ;
		initUI();
		initData();
		setBtnandLblstate();
		scaleSlider.addChangeListener(new ChangeListener() {

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

		});
	}
	
	private void setBtnandLblstate() {
		if(currentimageindex==0){
			getBtnleft().setEnabled(false);
			getBtnright().setEnabled(true);
		}else if(currentimageindex==listfilevo.size()-1){
			getBtnleft().setEnabled(true);
			getBtnright().setEnabled(false);
		}else if(currentimageindex==0&&currentimageindex==listfilevo.size()-1){
			getBtnleft().setEnabled(false);
			getBtnright().setEnabled(false);
		}
		else{
			getBtnleft().setEnabled(true);
			getBtnright().setEnabled(true);
		}
		//lbl
		String text =(currentimageindex+1)+"/"+listfilevo.size();		
		getLblinfo().setText(text);
		
	}

	private void initData() {
		//图片
		String pk =listfilevo.get(currentimageindex).getPk();
		String fullpath = listfilevo.get(currentimageindex).getFullPath();
		String filename = FileBroseConst.getBigImageCacheFileFullpath(pk, fullpath);
		String dsName = WorkbenchEnvironment.getInstance().getDSName();
		File file = new File(filename);
		FileOutputStream fileout = null ;
		try{
		if(!file.exists()){
			fileout = new FileOutputStream(file);
			FileManageServletClient.downloadFile(dsName, fullpath, fileout);
		}
		}catch (Exception e) {
			Logger.error(e);
		}finally{
			try {
				if(fileout!=null){
					fileout.close();
				}
			} catch (Exception e2) {
				Logger.error(e2);
			}
		}
		
		BufferedImage image=null;
		try {
			image = ImageIO.read(file);
		} catch (IOException e) {
			Logger.error(e);
		}finally{
			
		}
		getImagepane().setImage(image);
		double  scale =getImagepane().getScale();
		int ivalue = (int)(scale*100);
		scaleSlider.setValue(ivalue);
		
//		//信息
//		String text =(currentimageindex+1)+"/"+listfilevo.size();		
//		getLblinfo().setText(text);
		
	}

	public UIPanel getLeftbtnpane() {
		if(leftbtnpane== null){
			leftbtnpane = new UIPanel();
			VerticalFlowLayout  layout = new VerticalFlowLayout(VerticalFlowLayout.CENTER);
//			layout.setAlignment(VerticalFlowLayout.MIDDLE);
			leftbtnpane.setLayout(layout);
			leftbtnpane.add(getBtnleft());
		}
		return leftbtnpane;
	}

	public UIPanel getRigthbtnpane() {
		if(rigthbtnpane == null){
			rigthbtnpane = new UIPanel();
			VerticalFlowLayout  layout = new VerticalFlowLayout(VerticalFlowLayout.CENTER);
			rigthbtnpane.setLayout(layout);
			rigthbtnpane.add(getBtnright());
		}
		return rigthbtnpane;
	}

	public UIPanel getBottomlblpane() {
		if(bottomlblpane==null){
			bottomlblpane = new UIPanel();
			bottomlblpane.add(getLblinfo());
		}
		return bottomlblpane;
	}

	public UIPanel getCenterimagepane() {
		if(centerimagepane==null){
			centerimagepane = new UIPanel();
			centerimagepane.setLayout(new BorderLayout());
//			centerimagepane.add(new JLabel("testimage"));
			centerimagepane.add(getImagepane(),BorderLayout.CENTER);
		}
		return centerimagepane;
	}

	public UIButton getBtnleft() {
		if(btnleft==null){
			btnleft= new UIButton();
			ImageIcon icon = ThemeResourceCenter.getInstance().getImage(
					prefIconPath);
			ImageIcon icon_rollover = ThemeResourceCenter.getInstance().getImage(
					prefIconPath_rollOver);
			btnleft.setPreferredSize(new Dimension(30,30));

			btnleft.setOpaque(true);
			btnleft.setContentAreaFilled(false);
			btnleft.setBorder(null);
			btnleft.setFocusPainted(false);
			
			btnleft.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					//向前翻页
					if(currentimageindex==0){
						return ;
					}
					currentimageindex-=1;
					initData();
					setBtnandLblstate();
					
				}
			});
			
			btnleft.setIcon(icon);
			btnleft.setRolloverIcon(icon_rollover);
		}
		return btnleft;
	}

	public UIButton getBtnright() {
		if(btnright == null){
			btnright = new UIButton();
			ImageIcon icon = ThemeResourceCenter.getInstance().getImage(
					nextfIconPath);
			ImageIcon icon_rollover = ThemeResourceCenter.getInstance().getImage(
					nextfIconPath_rollOver);
			btnleft.setPreferredSize(new Dimension(30,30));

			btnright.setOpaque(false);
			btnright.setContentAreaFilled(false);
			btnright.setBorder(null);
			btnright.setFocusPainted(false);
			btnright.setIcon(icon);
			btnright.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					//向后翻页
					if(currentimageindex==listfilevo.size()-1){
						return ;
					}
					currentimageindex+=1;
					initData();
					setBtnandLblstate();
					
				}
			});
			
			btnright.setRolloverIcon(icon_rollover);
		}
		return btnright;
	}

	public UILabel getLblinfo() {
		if(lblinfo ==null){
			lblinfo = new UILabel();
		}
		return lblinfo;
	}

	public ImagePreviewPanel getImagepane() {
		if(imagepane ==null){
			imagepane = new ImagePreviewPanel(scaleSlider);
		}
		return imagepane;
	}

	private UIPanel leftbtnpane ;
	private UIPanel rigthbtnpane ;
	private UIPanel bottomlblpane ;
	private UIPanel centerimagepane ;
	
	private UIButton btnleft ;
	private UIButton btnright ;
	private UILabel lblinfo;
	private ImagePreviewPanel imagepane ;
	
	private void initUI() {
		LayoutManager layout = new BorderLayout();
		this.setLayout(layout);
		add(getLeftbtnpane(),BorderLayout.WEST);
		add(getRigthbtnpane(),BorderLayout.EAST);
		add(getCenterimagepane(),BorderLayout.CENTER);
		add(getBottomlblpane(),BorderLayout.SOUTH);
	}
}
