package cn.net.connor.std.processedit.newmeprocess;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.Registry;

import cn.net.connor.std.aifrcp.KDialog;
import cn.net.connor.std.aifrcp.KDialogController;
import cn.net.connor.std.aifrcp.util.KUtil;

@SuppressWarnings("serial")
public class NewProcessDialog extends KDialog {

	private JButton b_ok;
	private JButton b_cel;
	private NewProcessDialogController controller;
	private static final Registry REG = Registry.getRegistry(NewProcessDialog.class);

	public NewProcessDialog(KDialogController controller) {
		super(controller);
		this.controller = (NewProcessDialogController) controller;
	}

	private void setCompEnabled(boolean b) {
		b_ok.setEnabled(b);
		b_cel.setEnabled(b);
	}

	@Override
	protected void addListeners() {
		this.b_cel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
		this.b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					@Override
					public void run() {
						setCompEnabled(false);
						try {
							controller.doCreate();
							disposeDialog();
						} catch (Exception e) {
							e.printStackTrace();
							MessageBox.post(NewProcessDialog.this, e);
						}
						setCompEnabled(true);
					}
				}).start();
			}
		});
	}

	@Override
	protected void initUI() {
		this.setTitle(REG.getString("newprocess.TITLE"));
		this.setPreferredSize(new Dimension(1000, 770));
		this.setMinimumSize(new Dimension(1000, 770));
		this.setLayout(new BorderLayout());
		JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		pane.setResizeWeight(0.8);
		pane.setTopComponent(initProcPanel());
		pane.setBottomComponent(initItemPanel());
		pane.setBorder(null);
		this.add(BorderLayout.CENTER, pane);
		this.add(BorderLayout.SOUTH, initBtnPanel());
	}

	private Component initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
		this.b_ok = new JButton(REG.getString("ok"));
		this.b_cel = new JButton(REG.getString("cancel"));
		panel.add(b_ok);
		panel.add(b_cel);
		return panel;
	}

	private Component initProcPanel() {
		String title = REG.getString("procgroup.TITLE");
		List<PrefComponent> comps = controller.procComps;
		int colCnt = controller.procCols;
		JPanel wrapper = new JPanel(new BorderLayout());
		wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(30,5,5,5), title));
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints s = getGridBagConstraints(0, 0, new Insets(4, 0, 4, 25));
		int size = comps.size();
		int compIndex = 0;
		for(int i=0;i<size;i++) {
			PrefComponent prefComponent = comps.get(i);
			if(prefComponent.isInvisible()) {
				continue;
			}
//			if(compIndex%colCnt==0) {
//				KUtil.newRow(panel, prefComponent.getPanel(), s, 1);
//			}else {
//				KUtil.addCol(panel, prefComponent.getPanel(), s, 1, 0);
//			}
			KUtil.newRow(panel, prefComponent.getPanel(), s, 1);
			compIndex++;
		}
		s.weighty = 1;
		KUtil.newRow(panel, new JLabel(""), s, 1);
		JScrollPane scroll = new JScrollPane(panel);
		scroll.setBorder(null);
		wrapper.add(BorderLayout.CENTER, scroll);
		return wrapper;
	}

	private Component initItemPanel() {
		String title = REG.getString("itemgroup.TITLE");
		List<PrefComponent> comps = controller.itemComps;
		int colCnt = controller.itemCols;
		JPanel wrapper = new JPanel(new BorderLayout());
		wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(30,5,5,5), title));
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints s = getGridBagConstraints(0, 0, new Insets(4, 0, 4, 25));
		int size = comps.size();
		int compIndex = 0;
		for(int i=0;i<size;i++) {
			PrefComponent prefComponent = comps.get(i);
			if(prefComponent.isInvisible()) {
				continue;
			}
//			if(compIndex%colCnt==0) {
//				KUtil.newRow(panel, prefComponent.getPanel(), s, 1);
//			}else {
//				KUtil.addCol(panel, prefComponent.getPanel(), s, 1, 0);
//			}
			KUtil.newRow(panel, prefComponent.getPanel(), s, 1);
			compIndex++;
		}
		s.weighty = 1;
		KUtil.newRow(panel, new JLabel(""), s, 1);
		JScrollPane scroll = new JScrollPane(panel);
		scroll.setBorder(null);
		wrapper.add(BorderLayout.CENTER, scroll);
		return wrapper;
	}

}
