package cn.net.connor.std.processedit.meopviewfx.reorder;

import java.awt.BorderLayout;
import java.awt.Component;
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.Map;

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

import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.Registry;
import com.teamcenter.rac.util.iTextField;

import cn.net.connor.std.aifrcp.KDialog;
import cn.net.connor.std.aifrcp.util.KUtil;
import cn.net.connor.std.processedit.meopviewfx.bean.KTableRow;
import cn.net.connor.std.processedit.meopviewfx.bean.PropConfig;
import javafx.application.Platform;
import javafx.scene.control.TableView;

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

	public static final Registry REG = Registry.getRegistry(ReorderDialog.class);
	private JButton b_Ok;
	private JButton b_Cancel;
	private iTextField tf_startNum;
	private iTextField tf_stepNum;
	private int startNum;
	private int stepNum;
	private TableView<KTableRow> table;
	private Map<PropConfig, Integer> sequenceConfigMap;

	public ReorderDialog(TableView<KTableRow> table, Map<PropConfig, Integer> sequenceConfigMap) {
		super();
		this.table = table;
		this.sequenceConfigMap = sequenceConfigMap;
	}

	@Override
	protected void addListeners() {
		b_Cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
		b_Ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					loadNumbers();
					disposeDialog();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(ReorderDialog.this, e1);
				}
			}
		});
	}

	protected void loadNumbers() throws Exception {
		String startNumStr = tf_startNum.getText().trim();
		String stepNumStr = tf_stepNum.getText().trim();
		if (KUtil.isEmpty(startNumStr) || KUtil.isEmpty(stepNumStr)) {
			throw new TCException(REG.getString("require.error"));
		}
		this.startNum = -1;
		this.stepNum = -1;
		try {
			startNum = Integer.parseInt(startNumStr);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw new TCException(String.format(REG.getString("format.error1"), startNumStr));
		}
		try {
			stepNum = Integer.parseInt(stepNumStr);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw new TCException(String.format(REG.getString("format.error1"), stepNumStr));
		}
		Platform.runLater(() -> {
			KTableRow[] rows = table.getItems().toArray(new KTableRow[] {});
			int len = KUtil.getLen(rows);
			for (int i = 0; i < len; i++) {
				KTableRow row = rows[i];
				String seq = String.valueOf(startNum + i * stepNum);
				for (PropConfig sequenceConfig : this.sequenceConfigMap.keySet()) {
					row.stringProps.get(sequenceConfig).setValue(seq);
				}
			}
			int maxSeq = startNum + stepNum * (len - 1);
			for (PropConfig sequenceConfig : this.sequenceConfigMap.keySet()) {
				sequenceConfigMap.put(sequenceConfig, maxSeq);
			}
		});
	}

	@Override
	protected void initUI() {
		this.setTitle(REG.getString("dialog.title"));
		this.setLayout(new BorderLayout());
		this.add(BorderLayout.CENTER, initCenterPanel());
		this.add(BorderLayout.SOUTH, initBtnPanel());
	}

	private Component initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 8));
		panel.add(this.b_Ok = new JButton(REG.getString("ok.btn")));
		panel.add(this.b_Cancel = new JButton(REG.getString("cancel.btn")));
		return panel;
	}

	private Component initCenterPanel() {
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints s = getGridBagConstraints(0, 0, new Insets(8, 8, 0, 0));
		panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 8, 8));
		KUtil.newRow(panel, new JLabel(REG.getString("startnum.label")), s, 1);
		KUtil.addCol(panel, this.tf_startNum = new iTextField(16, true), s, 1, 0);
		KUtil.newRow(panel, new JLabel(REG.getString("stepnum.label")), s, 1);
		KUtil.addCol(panel, this.tf_stepNum = new iTextField(16, true), s, 1, 0);
		return panel;
	}

}
