package com.gx.obe.management.dialog;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.VerifyFloatListener;
import org.eclipse.swt.core.listener.VerifyMinListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
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 org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.business.entity.ScoreItem;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.swtdesigner.ResourceManager;

/**
 * @Description: 客观打分项界面
 * @author guoyr
 */
public class ScoreItemDialog extends EDialog {
	
	private String factorScore;// 指标分值
	private ScoreItem scoreItem;// 当前选中的客观打分项
	private List<ScoreItem> itemList = new ArrayList<ScoreItem>();// 存储全部客观打分项
	private CallBack callBack;
	private Text scoreText;
	private Text scoreDescText;

	{
		LogUtils.logClass(ScoreItemDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ScoreItemDialog(Shell parentShell, String factorScore, ScoreItem scoreItem, List<ScoreItem> itemList, CallBack callBack) {
		super(parentShell);
		this.factorScore = factorScore;
		this.scoreItem = scoreItem;
		this.itemList = itemList;
		this.callBack = callBack;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		CLabel label_1 = new CLabel(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText(Messages.Score + "：");
		
		scoreText = new Text(composite, SWT.BORDER);
		scoreText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		scoreText.addVerifyListener(new VerifyRequiredListener(scoreText, MessageFormat.format(Messages.Tips_required, Messages.Score)));
		scoreText.addVerifyListener(new VerifyFloatListener(MessageFormat.format(Messages.Tips_pleaseEnterTheCorrect, Messages.Score)));
		scoreText.addVerifyListener(new VerifyMinListener(-1, MessageFormat.format(Messages.Tips_pleaseEnterTheCorrect, Messages.Score)));
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label.setText(Messages.Description + "：");
		
		scoreDescText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData gd_scoreDescText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_scoreDescText.heightHint = 50;
		scoreDescText.setLayoutData(gd_scoreDescText);
		scoreDescText.setTextLimit(200);
//		scoreDescText.addVerifyListener(new VerifyRequiredListener(scoreDescText, "打分说明不能为空！"));
		
		Label label_2 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		EButton save_btn = new EButton(composite_1, SWT.NONE);
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		GridData gd_completeBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_completeBtn.widthHint = 90;
		save_btn.setLayoutData(gd_completeBtn);
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				completeAction();
			}
		});
		save_btn.setText(Messages.Button_ok);
		
		EButton cancel_btn = new EButton(composite_1, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		GridData gd_cancelBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_cancelBtn.widthHint = 90;
		cancel_btn.setLayoutData(gd_cancelBtn);
		cancel_btn.setText(Messages.Button_cancel);
		
		init();
		return container;
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		if(null == scoreItem){
			scoreItem = new ScoreItem();
		}
		scoreText.setText(ObjectUtils.getObjString(scoreItem.getScore()));
		scoreDescText.setText(ObjectUtils.getObjString(scoreItem.getName()));
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void completeAction(){
		if(!ValidateUtils.checkInput(scoreText.getParent())){
			return;
		}
		
		BigDecimal score = ObjectUtils.getObjBigDecimal(scoreText.getText().trim(), Constants.SCORE_SCALE);
		BigDecimal maxScore = ObjectUtils.getObjBigDecimal(factorScore, Constants.SCORE_SCALE);
		if(score.compareTo(maxScore) > 0){
			MessageDialog.openInformation(getShell(), "客观范围打分分值范围：0-"+maxScore+"！");
			scoreText.setFocus();
			return ;
		}
		
		boolean isExist = false;
		for(ScoreItem item : itemList){
			if((StringUtils.isNotEmpty(scoreItem.getId()) ? !StringUtils.equals(item.getId(), scoreItem.getId()) : true) && score.compareTo(item.getScore()) == 0){
				isExist = true;
			}
		}
		if(!isExist){
			scoreItem.setId(UuidUtils.getUUID());
			scoreItem.setScore(score);
			scoreItem.setName(scoreDescText.getText().trim());
			if(null != callBack){
				callBack.callBackObject(scoreItem);
				cancelAction();
			}
		}else{
			MessageDialog.openWarning(getShell(), "分值【"+score+"】" + Messages.Tips_AlreadyExistsPleaseRetype);
			scoreDescText.setFocus();
			return ;
		}
	}
	
	/**
	 * @Description: 取消
	 * @author guoyr
	 */
	private void cancelAction(){
		getShell().close();
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(300, 200);
	}
}
