package com.gx.obe.evaluation.step.composite.struct;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.action.bean.EAction;
import com.gx.obe.action.utils.ActionUtil;
import com.gx.obe.answer.dialog.InquireEnvelopDialog;
import com.gx.obe.business.entity.TorrentFile;
import com.gx.obe.common.file.dialog.DownloadDecBidFileDialog;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.EvalLeftButton;
import com.gx.obe.common.widget.shell.ToolsShell;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.InputStreamUtil;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.Activator;
import com.gx.obe.evaluation.step.action.ViewBidPriceEvaluationResultAction;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.RuntimeUtils;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.tools.dialog.BatchSignatureDialog;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评审界面
 * @author chenxw
 */
public class EvaluationComposite extends EvaluationView {
	
	protected ImageResolver imageResolver = new ImageResolver();
	protected Composite parentComposite;
	private MenuFunctionUtils menuFunctionUtils = new MenuFunctionUtils();
	private Menu menu;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public EvaluationComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		this.setLayout(gridLayout);
		this.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.setBackgroundMode(SWT.INHERIT_FORCE);
		this.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		menu = (Menu) parent.getData("menu");
		Composite left_tool_composite = new Composite(this, SWT.NONE);
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		gl_left_tool_composite.verticalSpacing = 2;
		left_tool_composite.setLayout(gl_left_tool_composite);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		left_tool_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.NONE);
		goto_back.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		goto_back.setToolTipText(Messages.Button_return);
		goto_back.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		goto_back.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/back.png"));
		
		EvalLeftButton refresh_evaluation = new EvalLeftButton(left_tool_composite, SWT.NONE);
		refresh_evaluation.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshAction();
			}
		});
		refresh_evaluation.setToolTipText(Messages.Button_refresh);
		refresh_evaluation.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refresh_evaluation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		EvalLeftButton download_bidfile = new EvalLeftButton(left_tool_composite, SWT.NONE);
		download_bidfile.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				downloadBidFileAction();
			}
		});
		download_bidfile.setToolTipText(Messages.Button_downloadBidFile);
		download_bidfile.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		download_bidfile.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/download.png"));
		
		menuFunctionUtils.addFunction(download_bidfile, "EVALUATION_VIEW_DOWNLOADBIDFILE");		
		
		ESepator sep = new ESepator(left_tool_composite, SWT.NONE);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		EvalLeftButton bidPrice = new EvalLeftButton(left_tool_composite, SWT.NONE);
		bidPrice.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				viewBidPice();
			}
		});
		bidPrice.setToolTipText("查看报价（得分）");
		bidPrice.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/bidPrice.png"));
		
		menuFunctionUtils.addFunction(bidPrice, "EVALUATION_VIEW_SHOWBIDPRICE");
		
		
		EvalLeftButton inquireEnvelop = new EvalLeftButton(left_tool_composite, SWT.NONE);
		inquireEnvelop.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				createInquireEnvelopAction();
			}
		});
		inquireEnvelop.setToolTipText("发起询标、澄清通知");
		inquireEnvelop.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/inquireEnvelop.png"));
		menuFunctionUtils.addFunction(inquireEnvelop, "EVALUATION_VIEW_INQUIREENVELOP");

		
		
		EvalLeftButton signature = new EvalLeftButton(left_tool_composite, SWT.NONE);
		signature.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				signatureAction(false);
			}
		});
		signature.setToolTipText(Messages.Button_caSignature);
		signature.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/signature.png"));
		menuFunctionUtils.addFunction(signature, "EVALUATION_VIEW_CASIGNATURE");
		
		
		
		EvalLeftButton handSignature = new EvalLeftButton(left_tool_composite, SWT.NONE);
		handSignature.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				signatureAction(true);
			}
		});
		handSignature.setToolTipText(Messages.Button_handSignature);
		handSignature.setImage(ResourceManager.getPluginImage(Activator.PLUGIN_ID, "icons/pen_white.png"));
		menuFunctionUtils.addFunction(handSignature, "EVALUATION_VIEW_HANDSIGNATURE");
		
		parentComposite = new Composite(this, SWT.NONE);
		GridLayout gl_parentComposite = new GridLayout(1, false);
		gl_parentComposite.marginHeight = 0;
		gl_parentComposite.marginWidth = 0;
		parentComposite.setLayout(gl_parentComposite);
		parentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		menuFunctionUtils.initFunctionAuthority(menu);
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw
	 */
	protected void init() {
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, curTender.getId(), curStep, curUser.getId());
	}
	
	/**
	 * @Description: 返回
	 * @author chenxw
	 */
	protected void goBackAction() {
		if (null == expertEvaluationListener) {
			return;
		}
		expertEvaluationListener.complete();
	}
	
	/**
	 * @Description: 刷新项目信息
	 * @author chenxw
	 */
	protected void refreshAction() {}
	
	/**
	 * @Description: 下载投标文件
	 * @author chenxw
	 */
	private void downloadBidFileAction() {
		final ToolsShell downloadToolsShell = new ToolsShell(getShell());
		Composite toolGroup = downloadToolsShell.addToolBar(Messages.Button_downloadDecBidFile, null, true);
		Image toolImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/lan.png");
		Image wifiImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/wifi.png");
		
		downloadToolsShell.addTool(toolGroup, Messages.Button_downloadFtp, toolImage, true, new ToolsShell.ClickListener() {
			public void click() {
				downloadToolsShell.close();
				new DownloadDecBidFileDialog(getShell(), curTender, true).open();
			}
		});
		final TorrentFile torrentFile = torrentFileService.getTorrentFileByTenderId(curTender.getId());
		boolean enabled = null != torrentFile && null != torrentFile.getTenderId() && null != torrentFile.getFileBite();
		downloadToolsShell.addTool(toolGroup, Messages.Button_downloadBt, toolImage, enabled, new ToolsShell.ClickListener() {
			public void click() {
				downloadToolsShell.close();
				if (null != torrentFile && null != torrentFile.getId()) {
					String tempFilePath = FileConstants.TEMP_FOLDER + "torrent" + File.separator + torrentFile.getTenderId() + File.separator + torrentFile.getFileName();
					InputStreamUtil.writeFile(torrentFile.getFileBite(), tempFilePath);
					File templateFile = new File(tempFilePath);
					if (templateFile.exists()) {
						RuntimeUtils.openFIle(templateFile, FileConstants.getUtorrentRoot());
					}
				}
			}
		});
		
		downloadToolsShell.addTool(toolGroup, Messages.Button_downloadOnline, wifiImage, true, new ToolsShell.ClickListener() {
			public void click() {
				downloadToolsShell.close();
				new DownloadDecBidFileDialog(getShell(), curTender, false).open();
			}
		});
		
		downloadToolsShell.setWidth(500);
		downloadToolsShell.pack();
		downloadToolsShell.open();
		SwtUtils.setCenter(downloadToolsShell);
	}
	
	/**
	 * @Description: 查看投标报价
	 * @author wangxiang
	 * @param supplier
	 */
	public void viewBidPice() {
		EAction action = (EAction) ActionUtil.getEAction(ViewBidPriceEvaluationResultAction.class.getName());
		action.setData("evaluationStep", curStep);
		action.setTenderProject(curTender);
		action.setData(Constants.CUR_EXPERT, curExpert);
		action.setName("查看价格");
		action.setParent(getShell());
		action.run();
	}
		
	/**
	 * @Description: 发起询标、澄清通知
	 * @author wangxiang
	 */
	private void createInquireEnvelopAction() {
		InquireEnvelopDialog dialog = new InquireEnvelopDialog(getShell(), null, curTender, null, curExpert.getUser(), curStep);
		dialog.open();
	}
	
	/**
	 * @Description: 签章
	 * @author wangxiang
	 */
	private void signatureAction(boolean isHandSign) {
		String signaturePdf = PropertiesUtils.getProperty(isHandSign ? ConfigConstants.HAND_SIGNATURE_PDF : ConfigConstants.CA_SIGNATURE_PDF, "");
		BatchSignatureDialog signatureDialog = new BatchSignatureDialog(getShell(), curTender, isHandSign, signaturePdf);
		signatureDialog.open();
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
