package com.gx.obe.management.composite;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.gx.obe.action.bean.Action;
import com.gx.obe.business.entity.MenuFunction;
import com.gx.obe.business.enumeration.ProjectRuleEnum;
import com.gx.obe.business.vo.ExchangeRate;
import com.gx.obe.common.dialog.BidPriceSubItemDialog;
import com.gx.obe.common.dialog.ExchangeRateDialog;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.composite.TitleComposite;
import com.gx.obe.common.widget.composite.TotalEvaluationPriceTableComposite;
import com.gx.obe.common.widget.dialog.SetEditPriceItemDialog;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.message.menu.MenuMessages;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.utils.ExchangeRateUitls;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description:
 * @author guoyr
 */
public class EvaluationPriceComposite extends Composite {
	
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private SupplierService supplierService = new SupplierService();
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private List<Supplier> supplierList;
	private Composite evalPrice_composite;
	private ELink addSubbidPriceBtn;
	private boolean canEdit = true;
	private TitleComposite titleComposite;
	private Action projectAction;
	private Menu menu;
	private MenuFunctionUtils menuFunctionUtils = new MenuFunctionUtils();
	private ELink setEditPriceItemLink;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public EvaluationPriceComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		this.projectAction = projectAction;
		
		titleComposite = new TitleComposite(this, SWT.NONE);
		titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		ELink exchange_rate_label = titleComposite.addELink("EXCHANGE_RATE");
		exchange_rate_label.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				exchangeRateEvaluationAction();
			}
		});
		exchange_rate_label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/import.png"));
		exchange_rate_label.setText(Messages.CurrenciesExchangeRate);
		
		addSubbidPriceBtn = titleComposite.addELink("CONFIG_BID_PRICE_TABLE");
		addSubbidPriceBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addSubbidPriceBtn.setText(Messages.Button_configBidOpiningList);
		addSubbidPriceBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				addSubbidPriceAction();
			}
		});
		
		setEditPriceItemLink = titleComposite.addELink("SET_MODIFY_ITEM");
		setEditPriceItemLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				setEditPriceItemAction();
			}
		});
		setEditPriceItemLink.setText(Messages.SetCorrectQuotation);
		
		evalPrice_composite = new Composite(this, SWT.NONE);
		evalPrice_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evalPrice_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		evalPrice_composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		init();
	}
	
	/**
	 * @Description: 多币种汇率转换评标价
	 * @author guoyr
	 */
	private void exchangeRateEvaluationAction() {
		ExchangeRateDialog exchangeRateDialog = new ExchangeRateDialog(getShell(), tenderProjectEntity, tenderProjectRuleEntity);
		exchangeRateDialog.setTitle(Messages.CurrenciesExchangeRate.concat(Messages.EvaluationPrice));
		exchangeRateDialog.addExchangeRateListener(new ExchangeRateUitls.ExchangeRateListener() {
			public boolean exchangeRate(List<BidPriceResult> bidPriceResultList, ExchangeRate exchangeRate) {
				String changeMessage = ExchangeRateUitls.exchangeRate(tenderProjectEntity, bidPriceResultList, (ExchangeRate) tenderProjectRuleEntity.getParamByKey(ProjectRuleEnum.EXCHANGE_RATE));
				if (StringUtils.isEmpty(changeMessage)) {
					refreshBidPrice();
					return true;
				} else {
					MessageDialog.openError(getShell(), changeMessage);
					return false;
					
				}
			}
			
		});
		exchangeRateDialog.open();
	}
	
	/**
	 * @Description: 开标一览表管理
	 * @author guoyr
	 */
	private void addSubbidPriceAction() {
		if (!addSubbidPriceBtn.getEnabled()) {
			return;
		}
		BidPriceSubItemDialog subBidPriceDialog = new BidPriceSubItemDialog(getShell(), tenderProjectEntity, tenderProjectRuleEntity, menuFunctionUtils);
		subBidPriceDialog.setTitle(Messages.Button_configBidOpiningList);
		subBidPriceDialog.open();
		refreshBidPrice();
	}
	
	/**
	 * @Description: 设置修正报价项
	 * @author chenxw
	 */
	private void setEditPriceItemAction() {
		if (!setEditPriceItemLink.getEnabled()) return;
		new SetEditPriceItemDialog(getShell(), tenderProjectEntity).open();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init() {
		if (this.isDisposed()) {
			return;
		}
		tenderProjectEntity = projectAction.getTenderProject();
		menu = projectAction.getMenu();
		menuFunctionUtils.initFunctionAuthority(menu, titleComposite.getFunctionComposite());
		titleComposite.setTitle(ObjectUtils.getObjString(MenuMessages.getValue(menu.getLanguageCode(), menu.getMenuShortName())));
		tenderProjectRuleEntity = projectAction.refreshTenderProjectRule();
		// 已经完成价格评审或推荐投标人，不能再修改
		if (evaluationStepService.isCompletePriceAndRecommonedStep(tenderProjectEntity.getId())) {
			canEdit = false;
		}
		if (MenuFunction.HIDE.equals(menuFunctionUtils.getAuthority("EDIT"))) {
			canEdit = false;
		}
		refreshBidPrice();
	}
	
	/**
	 * @Description: 刷新投标报价
	 * @author guoyr
	 */
	public void refreshBidPrice() {
		for (Control control : evalPrice_composite.getChildren()) {
			control.dispose();
		}
		supplierList = supplierService.getOpenBidSuccessSupplier(tenderProjectEntity.getId());
		
		TotalEvaluationPriceTableComposite evaluationPriceTable = new TotalEvaluationPriceTableComposite(evalPrice_composite, SWT.NONE);
		evaluationPriceTable.refresh(tenderProjectEntity, tenderProjectRuleEntity, supplierList, canEdit);
		evalPrice_composite.layout();
	}
	
	@Override
	protected void checkSubclass() {}
}
