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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
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 org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.ExpertEnum;
import com.gx.obe.http.util.Base64;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.UserService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class ExpertSignResultComposite extends Composite {

	private ImageResolver imageResolver = new ImageResolver();
	private UserService userService = new UserService();
	private ExpertService expertService;
	private List<Expert> expertList;
	private Image defaultAvatorImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/male_96.png");
	private ScrolledComposite scrolledComposite;
	private Composite signResultContent;
	private Composite parentComposite;

	{
		LogUtils.logClass(ExpertSignResultComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ExpertSignResultComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
				if(null != expertList){
					expertList.clear();
				}
			}
		});
		setLayout(new GridLayout(1, false));
		scrolledComposite = new ScrolledComposite(this, SWT.H_SCROLL | SWT.V_SCROLL);
		GridData gd_scrolledComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_scrolledComposite.widthHint = 500;
		scrolledComposite.setLayoutData(gd_scrolledComposite);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		signResultContent = new Composite(scrolledComposite, SWT.NONE);
		signResultContent.setLayout(new GridLayout(1, false));
		
		parentComposite = new Composite(signResultContent, SWT.NONE);
		parentComposite.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true, 1, 1));
		parentComposite.setLayout(new GridLayout(3, false));
		{
			
			Composite compositeOut = new Composite(parentComposite, SWT.NONE);
			compositeOut.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
			compositeOut.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
			GridLayout gl_compositeOut = new GridLayout(1, false);
			gl_compositeOut.marginHeight = 1;
			gl_compositeOut.marginWidth = 1;
			compositeOut.setLayout(gl_compositeOut);
			
			Composite composite = new Composite(compositeOut, SWT.NONE);
			composite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
			composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			GridLayout gl_composite = new GridLayout(2, false);
			gl_composite.horizontalSpacing = 0;
			gl_composite.verticalSpacing = 0;
			gl_composite.marginHeight = 0;
			gl_composite.marginWidth = 0;
			composite.setLayout(gl_composite);
			SwtUtils.setCircleRegion(compositeOut, 2);
			SwtUtils.setCircleRegion(composite, 2);
			
			ImageButton userAvatorLabel = new ImageButton(composite, SWT.BOTTOM);
			userAvatorLabel.setBorderColor(ColorConstants.BORDER_LINE_COLOR);
			userAvatorLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
			userAvatorLabel.setText("");
			userAvatorLabel.setImage(defaultAvatorImage);
			
			Composite composite_3 = new Composite(composite, SWT.NONE);
			composite_3.setLayout(new GridLayout(2, false));
			GridData gd_composite_3 = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
			gd_composite_3.widthHint = 220;
			gd_composite_3.heightHint = 100;
			composite_3.setLayoutData(gd_composite_3);
			
			CLabel label_3 = new CLabel(composite_3, SWT.NONE);
			label_3.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
			label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			label_3.setText(Messages.FullName+"：");
			
			Text userNameText = new Text(composite_3, SWT.READ_ONLY);
			userNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
			
			CLabel label_8 = new CLabel(composite_3, SWT.NONE);
			label_8.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
			label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			label_8.setText(Messages.Telephone +Messages.Telephone +"：");
			
			Label phoneText = new Label(composite_3, SWT.WRAP);
			phoneText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			
			CLabel label_5 = new CLabel(composite_3, SWT.NONE);
			label_5.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
			label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			label_5.setText(Messages.ProfessionalTitle+"：");
			
			Label professionalTitleText = new Label(composite_3, SWT.WRAP);
			GridData gd_professionalTitleText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
			gd_professionalTitleText.widthHint = 100;
			professionalTitleText.setLayoutData(gd_professionalTitleText);
			
			CLabel label_6 = new CLabel(composite_3, SWT.NONE);
			label_6.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
			label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			label_6.setText(Messages.Specialty+"：");
			
			Label specialtyText = new Label(composite_3, SWT.WRAP);
			GridData gd_specialtyText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
			gd_specialtyText.widthHint = 100;
			specialtyText.setLayoutData(gd_specialtyText);
			
			CLabel label_7 = new CLabel(composite_3, SWT.NONE);
			label_7.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
			label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			label_7.setText(Messages.WorkCompany + "：");
			
			Label companyText = new Label(composite_3, SWT.NONE);
			companyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
			
			Composite composite_1 = new Composite(composite, SWT.NONE);
			composite_1.setLayout(new GridLayout(1, false));
			composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
			
			Label lblNewLabel = new Label(composite_1, SWT.NONE);
			lblNewLabel.setAlignment(SWT.CENTER);
			lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
			
		}
		
		scrolledComposite.setContent(signResultContent);
		scrolledComposite.setMinSize(signResultContent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		init();
	}
	
	private void init(){
		expertService = new ExpertService();
		scrolledComposite.getVerticalBar().setIncrement(20);
		scrolledComposite.forceFocus();
		scrolledComposite.addListener(SWT.Activate, new Listener() {
	        public void handleEvent(Event e) {
	            scrolledComposite.setFocus();
	        }
	    });
	}
	  static int[] circle(int r, int offsetX, int offsetY) {
		    int[] polygon = new int[8 * r + 4];
		    for (int i = 0; i < 2 * r + 1; i++) {
		      int x = i - r;
		      int y = (int) Math.sqrt(r * r - x * x);
		      polygon[2 * i] = offsetX + x;
		      polygon[2 * i + 1] = offsetY + y;
		      polygon[8 * r - 2 * i - 2] = offsetX + x;
		      polygon[8 * r - 2 * i - 1] = offsetY - y;
		    }
		    return polygon;
		  }
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity){
		parentComposite.getChildren()[0].dispose();
		expertList = expertService.getExpertList(tenderProjectEntity.getId(), ExpertEnum.ALL);
		
		// 为了显示样式，没有专家时生成一个临时专家
		if(null == expertList || expertList.size() == 0){
			expertList = new ArrayList<Expert>();
			Expert expert = new Expert();
			expertList.add(expert);
		}
		if(null != expertList && expertList.size() > 0){
			for(Expert expert : expertList){
				AuthUser user = expert.getUser();
				user = userService.findById(expert.getUserId());
				expert = expertService.getById(expert.getId());
				initExpertInfor(user, expert);
			}
		}
		signResultContent.layout();
		scrolledComposite.setContent(signResultContent);
		scrolledComposite.setMinSize(signResultContent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 初始化专家的信息
	 * @author guoyr
	 */
	private void initExpertInfor(AuthUser user, Expert expert) {
		if(null == user)user = new AuthUser();
		if(null == expert)expert = new Expert();

//		Composite compositeOut = new Composite(signResultContent, SWT.NONE);
//		compositeOut.setBackground(ColorConstants.BORDER_LINE_COLOR);
//		GridLayout gl_compositeOut = new GridLayout(1, false);
//		gl_compositeOut.marginHeight = 1;
//		gl_compositeOut.marginWidth = 1;
//		compositeOut.setLayout(gl_compositeOut);
		Composite compositeOut = new Composite(parentComposite, SWT.NONE);
		compositeOut.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
		compositeOut.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		GridLayout gl_compositeOut = new GridLayout(1, false);
		gl_compositeOut.marginHeight = 1;
		gl_compositeOut.marginWidth = 1;
		compositeOut.setLayout(gl_compositeOut);
		
		Composite composite = new Composite(compositeOut, SWT.NONE);
		GridData gd_composite = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_composite.minimumHeight = 80;
		composite.setLayoutData(gd_composite);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		SwtUtils.setCircleRegion(compositeOut, 2);
		SwtUtils.setCircleRegion(composite, 2);
		
		ImageButton userAvatorLabel = new ImageButton(composite, SWT.BORDER|SWT.BOTTOM);
		userAvatorLabel.setImage(defaultAvatorImage);
		userAvatorLabel.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false, 1, 1));
		
		Composite composite_3 = new Composite(composite, SWT.NONE);
		composite_3.setLayout(new GridLayout(2, false));
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.widthHint = 250;
		gd_composite_3.minimumHeight = 150;
		composite_3.setLayoutData(gd_composite_3);
		
		CLabel label_3 = new CLabel(composite_3, SWT.NONE);
		label_3.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText(Messages.FullName+"：");
		label_3.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Text user_name = new Text(composite_3, SWT.READ_ONLY);
		user_name.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label_8 = new CLabel(composite_3, SWT.NONE);
		label_8.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setText(Messages.Telephone +"：");
		label_8.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Label phone = new Label(composite_3, SWT.WRAP);
		phone.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label_5 = new CLabel(composite_3, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_5.setText(Messages.ProfessionalTitle+"：");
		label_5.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Text professional_title = new Text(composite_3, SWT.WRAP | SWT.READ_ONLY);
		GridData gd_professionalTitleText = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
		gd_professionalTitleText.widthHint = 200;
		professional_title.setLayoutData(gd_professionalTitleText);
		
		CLabel label_6 = new CLabel(composite_3, SWT.NONE);
		label_6.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_6.setText(Messages.Specialty+"：");
		label_6.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Text specialty = new Text(composite_3, SWT.WRAP | SWT.READ_ONLY);
		GridData gd_specialtyText = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
		gd_specialtyText.widthHint = 200;
		specialty.setLayoutData(gd_specialtyText);
		
		CLabel label_7 = new CLabel(composite_3, SWT.NONE);
		label_7.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, true, 1, 1));
		label_7.setText(Messages.WorkCompany + "：");
		label_7.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Text company = new Text(composite_3, SWT.WRAP | SWT.READ_ONLY);
		GridData gd_companyText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_companyText.widthHint = 200;
		company.setLayoutData(gd_companyText);
	
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label sign_type = new Label(composite_1, SWT.NONE);
		sign_type.setAlignment(SWT.CENTER);
		sign_type.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));

		user_name.setText(ObjectUtils.getObjString(user.getUserName()));
		phone.setText(ObjectUtils.getObjString(user.getPhone()));
		professional_title.setText(ObjectUtils.getObjString(user.getProfessionalTitle()));
		specialty.setText(ObjectUtils.getObjString(user.getSpecialty()));
		company.setText(ObjectUtils.getObjString(user.getCompany()));
		company.setToolTipText(ObjectUtils.getObjString(user.getCompany()));
		sign_type.setText(expert.getSignTypeCN());
		if(!ExpertEnum.SIGN.equals(expert.getSignType()) && !ExpertEnum.AVOID.equals(expert.getSignType())){
			composite_1.setBackground(SWTResourceManager.getColor(240, 240, 240));
			sign_type.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		}else{
			sign_type.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			composite_1.setBackground(SWTResourceManager.getColor(77,163,19));
		}
		Image image = imageResolver.getImage(Base64.decode(user.getAvatar()));
		if(null !=image){
			userAvatorLabel.setImage(image, defaultAvatorImage.getBounds().width, defaultAvatorImage.getBounds().height);
		}else {
			userAvatorLabel.setImage(defaultAvatorImage);
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
