package cn.com.customization.schedule;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentSchedule;
import com.teamcenter.rac.kernel.TCComponentScheduleTask;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;

public class AbstracModifyDialog extends JDialog {
	// public class AbstracModifyDialog extends AbstractAIFDialog{
	public static final long		serialVersionUID	= 1L;
	public String					title;
	public AbstractAIFUIApplication	app;
	public TCSession				session;
	public JRadioButton				radio1;
	public JRadioButton				radio2;
	public JRadioButton				radio3;
	public JRadioButton				radio4;
	public JTextField				txt;
	public String					scheduleID;
	public String					scheduleName;
	public TCComponentSchedule		schedule;
	TCComponent[] tasks = null;
	TCComponentScheduleTask sch_summary_task = null;
	public String					currentRole;
	public TCComponentUser			currentUser;
	public TCComponentUser			owningUser;
	public JTextField				replaceSource;
	public JTextField				replaceObject;
	JCheckBox chkTask ;
	JCheckBox chkDelivery;
	
	boolean isSchedule = false;
	boolean isTopSchedule = false;
	boolean isTask = false;
	boolean isModifyTaskName = true;
	boolean isModifyDeliveryName = true;
	InterfaceAIFComponent[] targets;
	
//	private static final Logger logger =Logger.getLogger(AbstracModifyDialog.class);
	
	public AbstracModifyDialog(AbstractAIFApplication app, String title) {
		this.title = title;
		this.app = (AbstractAIFUIApplication) app;
		session = (TCSession) app.getSession();
		if (getTargetSchedule()) {
			initUI();
		}
	}

	public void initUI() {
		setTitle(" " + title);
		JPanel panel = new JPanel(new PropertyLayout());
		panel.add("1.1.left.center", new JLabel("         "));
		panel.add("1.2.left.center", setCenterPanel());
		panel.add("1.3.left.center", new JLabel("         "));
		panel.add("2.1.left.center", new JLabel("  "));
		this.getContentPane().add(panel);
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		pack();
		validate();
		Dimension screen = getToolkit().getScreenSize();
		setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) / 2);
		this.setVisible(true);
	}

	private JPanel setCenterPanel() {
		JPanel panel1 = new JPanel(new PropertyLayout());
		JTextField idtxt = new JTextField(32);
		idtxt.setText(scheduleID);
		idtxt.setEnabled(false);
		JTextField nametxt = new JTextField(32);
		nametxt.setText(scheduleName);
		nametxt.setEnabled(false);
		
		chkTask = new JCheckBox("任务名称");
		chkTask.setSelected(true);
		chkDelivery = new JCheckBox("交付物名称");
		chkDelivery.setSelected(true);
		
		chkTask.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				if(chkTask.isSelected()){
					isModifyTaskName = true;
				}else{
					isModifyTaskName = false;	
				}
			}
		});

		chkDelivery.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				if(chkDelivery.isSelected()){
					isModifyDeliveryName = true;
				}else{
					isModifyDeliveryName = false;	
				}
			}
			
		});
		
		JPanel panelModify = new JPanel(new FlowLayout());
		panelModify.add(chkTask);
		panelModify.add(chkDelivery);
		
		panel1.add("1.1.left.center", new JLabel("   "));
		panel1.add("2.1.left.center", new JLabel("时间表ID"));
		panel1.add("2.2.left.center", idtxt);
		panel1.add("3.1.left.center", new JLabel("时间表名称"));
		panel1.add("3.2.left.center", nametxt);

		panel1.add("4.1.left.center", new JLabel("更改内容："));
		panel1.add("4.2.left.center", panelModify);
		
		JPanel panel2 = new JPanel(new PropertyLayout());
//		panel2.setBorder(BorderFactory.createTitledBorder("更改方式"));
		panel2.setBorder(BorderFactory.createTitledBorder("增加前缀"));

		radio1 = new JRadioButton("时间表ID");
		radio2 = new JRadioButton("时间表名称");
		radio3 = new JRadioButton("自定义文本");
		
		radio1.setSelected(true);
		radio4 = new JRadioButton("替换文本");
		radio1.addActionListener(new buttonListener());
		radio2.addActionListener(new buttonListener());
		radio3.addActionListener(new buttonListener());
		radio4.addActionListener(new buttonListener());
		txt = new JTextField(27);
		txt.setEnabled(false);
		JPanel txtPanel = new JPanel();
		txtPanel.add(new JLabel("自定义文本："));
		txtPanel.add(txt);
		ButtonGroup bg = new ButtonGroup();
		bg.add(radio1);
		bg.add(radio2);
		bg.add(radio3);
		bg.add(radio4);
		panel2.add("1.1.left.center", radio1);

		panel2.add("2.1.left.center", radio2);
		panel2.add("3.1.left.center", radio3);
		panel2.add("4.1.left.center", txtPanel);
		
		JPanel panel3 = new JPanel(new PropertyLayout());
//		panel3.setBorder(BorderFactory.createTitledBorder("替换"));
		replaceSource = new JTextField(28);
		replaceObject = new JTextField(28);
		replaceSource.setEnabled(false);
		replaceObject.setEnabled(false);
		panel3.add("1.1.left.center", new JLabel(" 查找內容："));
		panel3.add("1.2.left.center", replaceSource);
		panel3.add("2.1.left.center", new JLabel(" 替 换 为："));
		panel3.add("2.2.left.center", replaceObject);
		
		JPanel panePlace = new JPanel(new PropertyLayout());
		panePlace.add("1.1.left.center", radio4);
		panePlace.add("2.1.left.center", panel3);

		JPanel panel = new JPanel(new PropertyLayout());
		panel.add("1.1.left.center", panel1);
		panel.add("2.1.left.center", panel2);
		panel.add("3.1.left.center", panePlace);
		panel.add("4.1.center.center", setButtonPanel());

		return panel;
	}

	private JPanel setButtonPanel() {
		JPanel panel = new JPanel(new PropertyLayout());
		JButton confirmBtn = new JButton("确定更改");
		JButton cancelBtn = new JButton("取消更改");
//		JButton replaceBtn = new JButton("全部替换");
		confirmBtn.addActionListener(new buttonListener());
		cancelBtn.addActionListener(new buttonListener());
//		replaceBtn.addActionListener(new buttonListener());

		panel.add("1.1.left.center", new JLabel("  "));
		panel.add("2.1.left.center", confirmBtn);
		// panel.add("2.2.left.center", replaceBtn);
		panel.add("2.2.left.center", cancelBtn);
		return panel;
	}

	public boolean getTargetSchedule() {
		boolean flag = true;
		
		targets = app.getTargetComponents();
		if(targets == null || targets.length == 0){
			MessageBox.post("请选择时间表或者时间表任务执行操作！", "提示", MessageBox.INFORMATION);
			return false;
		}else{
			for(int i = 0;i < targets.length; i++){
				if(targets[i] instanceof TCComponentSchedule){
					isSchedule = true; 
				}else if(targets[i] instanceof TCComponentScheduleTask){
					try {
						if(((TCComponentScheduleTask)targets[0]).getReferenceProperty("fnd0ParentTask") == null){
							isSchedule = true; 
							isTopSchedule =true;//说明是在时间表管理器中选中的时间表，此时时间表加载的仍然是时间表任务
						}else{
							isTask = true; 
						}
					} catch (TCException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(isSchedule && isTask){
				MessageBox.post("请选择时间表或者时间表任务执行操作！", "提示", MessageBox.INFORMATION);
				return false;
			}
		}
		
		try {
			if(isSchedule && !isTask){
				if(isTopSchedule){
					schedule = (TCComponentSchedule) ((TCComponentScheduleTask)targets[0]).getReferenceProperty("schedule_tag");
				}else{
					schedule = (TCComponentSchedule) targets[0];
				}
			}else{
				schedule = (TCComponentSchedule) ((TCComponentScheduleTask)targets[0]).getReferenceProperty("schedule_tag");
			}
			
			if(isSchedule && !isTask){
				try {
					tasks = schedule.getAllTasks();
				} catch (TCException e) {
					e.printStackTrace();
				}	
			}else if(!isSchedule && isTask){
				tasks = new TCComponent[targets.length];
				for(int i = 0; i < targets.length; i++){
					tasks[i] = (TCComponent) targets[i];
				}
			}
			try {
//				sch_summary_task = (TCComponentScheduleTask) schedule.getReferenceProperty("sch_summary_task");
				sch_summary_task = (TCComponentScheduleTask) schedule.getReferenceProperty("fnd0SummaryTask");
			} catch (TCException e) {
				e.printStackTrace();
			}
			System.out.println("schedule = " + schedule);
			
			scheduleID = schedule.getProperty("item_id");
			scheduleName = schedule.getProperty("object_name");
			currentUser = session.getUser();
			currentRole = session.getCurrentRole().toString();
			owningUser = (TCComponentUser) schedule.getReferenceProperty("owning_user");
		} catch (TCException e) {
			e.printStackTrace();
		}
		return flag;
	}

	
	class buttonListener implements ActionListener {
		public void actionPerformed(ActionEvent actionevent) {
			Object obj = actionevent.getSource();
			if (obj instanceof JButton) {
				JButton button = (JButton) obj;
				String buttonName = button.getText();
				if (buttonName.equals("取消更改")) {
					cancelAction();
				} else if (buttonName.equals("确定更改")) {
					if(!isModifyDeliveryName && !isModifyTaskName){
						MessageBox.post("请选择更改内容！", "提示", MessageBox.INFORMATION);
						return;
					}
					if (radio1.isSelected()) {
						confirmAction(0, "");
					} else if (radio2.isSelected()) {
						confirmAction(1, "");
					} else if (radio3.isSelected()) {
						confirmAction(2, txt.getText());
					} else if (radio4.isSelected()) {
						replaceAction();
					}
				}
			} else {
				if (obj instanceof JRadioButton) {
					if (radio3.isSelected()) {
						txt.setEnabled(true);
						replaceSource.setEnabled(false);
						replaceObject.setEnabled(false);
						replaceSource.setText("");
						replaceObject.setText("");
					} else {
						if (radio4.isSelected()) {
							replaceSource.setEnabled(true);
							replaceObject.setEnabled(true);
						} else {
							replaceSource.setEnabled(false);
							replaceObject.setEnabled(false);
							replaceSource.setText("");
							replaceObject.setText("");
						}
						txt.setEnabled(false);
						txt.setText("");
					}
				}
			}
		}
	}

	public String replaceName(String s, String source, String object) {
		String newString = "";
		int first = 0;
		while (s.indexOf(source) != -1) {
			first = s.indexOf(source);
			if (first != s.length()) {
				newString = newString + s.substring(0, first) + object;
				s = s.substring(first + source.length(), s.length());
			}
		}
		newString = newString + s;
		return newString;
	}

	public void replaceAction() {}

	public void confirmAction(int index, String txt) {}

	public void cancelAction() {}
}
