package com.gx.obe.management.composite;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.LoadingAdatper;
import org.eclipse.swt.core.utils.FileDialogUtils;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.VideoFile;
import com.gx.obe.common.file.download.DownloadFileUtill;
import com.gx.obe.common.file.download.infer.DownloadFileListener;
import com.gx.obe.common.file.utils.ComputeUtill;
import com.gx.obe.common.widget.shell.ProgressMessageDialog;
import com.gx.obe.components.core.Constants;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.VideoReportFileService;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorButton;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.ButtonCellRenderer;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellEditorRender;
import de.kupzog.ktable.renderers.CellEventActionRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

public class EvaluactionVideoComposite extends Composite {
 	private TenderProjectEntity tenderProjectEntity;
	private Composite videoBorderComposite;
	private Composite videoListComposite;
 	boolean isUnqualified = false;
 	private String tenderId ;
	private KTable resultKTable;
	private VideoReportFileService videoReportFileService;
	private List<VideoFile> videoFileList; 
	
	
	public EvaluactionVideoComposite(Composite parent, int style) {
		super(parent, style);
		this.tenderProjectEntity = (TenderProjectEntity)parent.getData("tenderProjectEntity");
		tenderId = (null != tenderProjectEntity.getParentId()) ? tenderProjectEntity.getParentId() : tenderProjectEntity.getId();
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		videoBorderComposite = new Composite(this, SWT.NONE);
		GridLayout gl_s_bord = new GridLayout(1, false);
		gl_s_bord.verticalSpacing = 0;
		gl_s_bord.marginHeight = 1;
		gl_s_bord.horizontalSpacing = 0;
		gl_s_bord.marginWidth = 1;
		videoBorderComposite.setLayout(gl_s_bord);
		
		GridData gd_composite_a2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_a2.widthHint = 350;
		videoBorderComposite.setLayoutData(gd_composite_a2);
		videoBorderComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		videoListComposite = new Composite(videoBorderComposite, SWT.NONE);
		videoListComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_4 = new GridLayout(1, false);
		gl_composite_4.verticalSpacing = 0;
		gl_composite_4.horizontalSpacing = 0;
		gl_composite_4.marginHeight = 0;
		gl_composite_4.marginWidth = 0;
		videoListComposite.setLayout(gl_composite_4);
		videoListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		resultKTable = new KTable(videoListComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY | SWTX.FILL_WITH_LASTCOL);
		resultKTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		resultKTable.setAutoComputerSurplusWidth(true);

		init();
	}
	
	private void init(){
		videoReportFileService = new VideoReportFileService();
		videoFileList = videoReportFileService.getReportFile(tenderId);
		if(null == videoFileList) {
			videoFileList = new ArrayList<>();
		}
		creatTable();
	}
		
	private void creatTable() {
		final CommonKtableCModel commKTableModel = new CommonKtableCModel(resultKTable, 1, 0, videoFileList.size(), 5);
		commKTableModel.setContentRowHeight(Constants.ROW_HIGHT);
		commKTableModel.setColumnWidth(0, 20);
		commKTableModel.setColumnMaxWidth(0, 20);
		commKTableModel.setColumnWidth(1, 500);
		commKTableModel.setColumnMaxWidth(1, 1000);
 		commKTableModel.setColumnWidth(2, 20);
 		commKTableModel.setColumnMaxWidth(2, 40);
		CellContentRender contentRender = new CellContentRender() {
			public Object receiveContent(int row, int col) {
				if (row < commKTableModel.getFixedRowCount()) {
					if (col == 0) {
						return Messages.Num;
					} else if (col == 1) {
						return "视频名称";
					} else if (col == 2) {
						return "视频大小";
					}else if (col == 3) {
						return "时间";
					}else if (col == 4) {
						return "下载";
					}
					return "";
				} else {
					VideoFile file = videoFileList.get(row - commKTableModel.getFixedRowCount());
					if (col == 0) {
						return (row - commKTableModel.getFixedRowCount() + 1);
					}else if(col == 1){
						if(null == file) {
							return "";
						}
						return null == file.getFileName() ? "" : file.getFileName();
					}else if (col == 2) {
						if(null == file) {
							return "";
						}
						return 0 == file.getFileSize() ? "" : ComputeUtill.computeSize(file.getFileSize());
 					}else if (col == 3) {
 						if(null == file) {
							return "";
						}
						return null == file.getCreateTime() ? "" : DateUtils.getFormat(file.getCreateTime(), "yyyy年MM月dd日");
					}else if (col == 4) {
						return "下载";
					}
				}
				return "";
			}	
		};
		commKTableModel.setCellContentRender(contentRender);
				
		// 设置单元格渲染器
		final ButtonCellRenderer buttonCellRenderer = new ButtonCellRenderer();
		final TextWrapCellRenderer textWrapCellRenderer = new TextWrapCellRenderer();
		CellRender cellRender = new CellRender() {
			public Object receiveCellRender(int row, int col) {
				if (commKTableModel.isFixedCell(col, row)) {
					return new FixedWrapCellRenderer();
				} else {
					if (col == 4) {
						return buttonCellRenderer;
					}
					return textWrapCellRenderer;
				}
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 设置单元格编辑器
		CellEditorRender cellEditorRender = new CellEditorRender() {
			public Object receiveCellEditor(int row, int col) {
				if (row > -commKTableModel.getFixedRowCount()) {
					if (col == 4) {
						return new KTableCellEditorButton(resultKTable, new CellEventActionRender() {
							public void receiveAction(int row, int col) {
								downloadVideoFile(row - commKTableModel.getFixedRowCount());
							}
						});
					}
				}
				return null;
			}
		};
		commKTableModel.setCellEditorRender(cellEditorRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row < commKTableModel.getFixedRowCount()) {
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				} else {
					if (col == 0) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					} else if (col == 2 ) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					} else {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}
				}
			}
		};
		commKTableModel.setCellAlignmentRender(cellAlignmentRender);		
		resultKTable.setModel(commKTableModel);
	}

	private void downloadVideoFile(int loc) {
		VideoFile file = videoFileList.get(loc);
		String path = FileDialogUtils.getDirectorySavePath("请选择");
		final ProgressMessageDialog messageProgress = new ProgressMessageDialog(getShell(), false, 100, new LoadingAdatper() {
			public void finish(boolean flag) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						MessageDialog.openInformation(getShell(), "下载完成");
 					}
				});
			}
		});
		messageProgress.setCountDownTime(3);	
		DownloadFileUtill downloadFileUtill = new DownloadFileUtill(file.getFilePath(), file.getFileName(),path ,new DownloadFileListener() {
			
			@Override
			public void speed(String speed) {}
			
			@Override
			public void remainingTime(String remainingTime) {}
			
			@Override
			public void progress(int percent) {
				messageProgress.setProgress(percent);
			}
			
			@Override
			public void complate(boolean flag) {
				messageProgress.finish(flag);
			}
			@Override
			public void checkMD5(boolean flag) {}

			@Override
			public void writeSize(long size) {
			}
		});
		downloadFileUtill.startDownloadFile();
 		messageProgress.setTitle("正在下载文件请稍后...");
		messageProgress.open();
	}
	@Override
	protected void checkSubclass() {
	}
}
