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

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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 com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.evaluation.step.listener.AiEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.web.entity.Struct;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 结构化列表
 * @author chenxw 
 */
public class StructListComposite extends Composite implements StructComposite.SkipListener {
	
	private Composite structComposite;
	private List<Struct> structList;
	private Composite buttonComposite;
	private ImageButton preStructBtn;
	private ImageButton nextStructBtn;
	private ImageButton listStructBtn;
	private int curStructIndex;
	private AiEvaluationListener listener;
	
	/**
	 * Create the shell.
	 * 
	 * @param rightComposite
	 * @param none
	 */
	public StructListComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		Composite composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ESepator sep = new ESepator(composite, SWT.HORIZONTAL);
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		structComposite = new Composite(composite, SWT.NONE);
		GridLayout gl_structComposite = new GridLayout(1, false);
		gl_structComposite.marginHeight = 0;
		gl_structComposite.marginWidth = 0;
		structComposite.setLayout(gl_structComposite);
		structComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ESepator but_sep = new ESepator(composite, SWT.HORIZONTAL);
		but_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		but_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		buttonComposite = new Composite(composite, SWT.NONE);
		buttonComposite.setLayout(new GridLayout(3, true));
		buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		preStructBtn = new ImageButton(buttonComposite, SWT.NONE);
		preStructBtn.setBorderColor(ColorConstants.SKIN_BG);
		preStructBtn.setBackground(ColorConstants.SKIN_BG);
		preStructBtn.setAlpha(10);
		preStructBtn.setTopMargin(5);
		preStructBtn.setBottomMargin(5);
		preStructBtn.setLeftMargin(10);
		preStructBtn.setRightMargin(10);
		preStructBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/pre.png"));
		preStructBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (curStructIndex > 1) {
					curStructIndex--;
					refreshStructComposite();
				}
			}
		});
		preStructBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		preStructBtn.setText(Messages.PreviousItem);
		
		listStructBtn = new ImageButton(buttonComposite, SWT.NONE);
		listStructBtn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		listStructBtn.setBorderColor(ColorConstants.SKIN_BG);
		listStructBtn.setBackground(ColorConstants.SKIN_BG);
		listStructBtn.setAlpha(10);
		listStructBtn.setTopMargin(5);
		listStructBtn.setBottomMargin(5);
		listStructBtn.setLeftMargin(10);
		listStructBtn.setRightMargin(10);
		listStructBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_up.png"));
		listStructBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {

			}
		});
		listStructBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		
		nextStructBtn = new ImageButton(buttonComposite, SWT.NONE);
		nextStructBtn.setBorderColor(ColorConstants.SKIN_BG);
		nextStructBtn.setBackground(ColorConstants.SKIN_BG);
		nextStructBtn.setAlpha(10);
		nextStructBtn.setTopMargin(5);
		nextStructBtn.setBottomMargin(5);
		nextStructBtn.setLeftMargin(10);
		nextStructBtn.setRightMargin(10);
		nextStructBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/next.png"));
		nextStructBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (curStructIndex < structList.size()) {
					curStructIndex++;
					refreshStructComposite();
				}
			}
		});
		nextStructBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		nextStructBtn.setText(Messages.NextItem);
	}
	
	/** 
	 * @Description: 下一个
	 * @author chenxw 
	 */
	public void next() {
		if (curStructIndex < structList.size()) {
			curStructIndex++;
			refreshStructComposite();
		}
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param supplier
	 * @param structList 
	 */
	public void refresh(List<Struct> structList) {
		this.structList = structList;
		this.curStructIndex = 1;
		
		refreshStructComposite();
	}
	
	/**
	 * @Description: 刷新结果化界面
	 * @author chenxw 
	 */
	private void refreshStructComposite() {
		for (Control control : structComposite.getChildren()) {
			control.dispose();
		}
		
		if (CollectionUtils.isNull(structList)) {
			buttonComposite.setVisible(false);
			Label noBidFileLabel = new Label(structComposite, SWT.WRAP);
			noBidFileLabel.setForeground(SWTResourceManager.getColor(200, 200, 200));
			noBidFileLabel.setFont(SWTResourceManager.getFont("宋体", 30, SWT.BOLD));
			noBidFileLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
			noBidFileLabel.setText("当前指标投标人未对其进行结构化数据维护，请专家查看标书内容后对投标人进行评审！");
		} else {
			listStructBtn.setText(curStructIndex + "/" + structList.size());
			preStructBtn.setEnabled(true);
			nextStructBtn.setEnabled(true);
			if (curStructIndex == structList.size()) {
				nextStructBtn.setEnabled(false);
			}
			if (curStructIndex == 1) {
				preStructBtn.setEnabled(false);
			}
			buttonComposite.setVisible(true);
			StructComposite composite = new StructComposite(structComposite, SWT.NONE);
			composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
			composite.addSkipListener(this);
			composite.addAiEvaluationListener(listener);
			composite.refresh(structList.get(curStructIndex - 1), curStructIndex == structList.size());
		}
		
		structComposite.layout();
	}
	
	public void addAiEvaluationListener(AiEvaluationListener listener) {
		this.listener = listener;
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
