package cn.com.customization.schedule;

import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentScheduleDeliverable;
import com.teamcenter.rac.kernel.TCComponentScheduleTask;
import com.teamcenter.rac.kernel.TCComponentTaskDeliverable;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;

public class ModifyTaskOrDeliveryNameDialog extends AbstracModifyDialog {

	/**
	 * serialVersionUID:TODO（描述变量的功能）
	 * @since Ver 1.1
	 */
	public static final long	serialVersionUID	= 1L;
	
	public ModifyTaskOrDeliveryNameDialog(AbstractAIFApplication abstractaifapplication, String title) {
		super(abstractaifapplication, title);
	}

	public void confirmAction(int index, String txt) {
		if (!((currentRole.equalsIgnoreCase("DBA")) || (currentUser.equals(owningUser)))) {
			MessageBox.post("不是时间表所有者或DBA用户，不能修改！", "INFO", MessageBox.ERROR);
			return;
		}
		if(isModifyTaskName){
			modifyTaskName(index, txt, false);
		}
		if(isModifyDeliveryName){
			modifyDeliveryName(index, txt, false);
		}
		this.dispose();
	}

	public void modifyTaskName(int index, String txt, boolean isReplace) {
		try {
			System.out.println("modifyTaskName = " + tasks.length);
			for (int i = 0; i < tasks.length; i++) {
				TCComponentScheduleTask task = (TCComponentScheduleTask) tasks[i];
				System.out.println("modifyTaskName:: sch_summary_task = " + sch_summary_task);
				
				if (sch_summary_task != null && !sch_summary_task.equals(task)) {
					
					String status = task.getTCProperty("fnd0status").getStringValue();
					System.out.println("status = "+status);
					//MessageBox.post("status1222222222211111="+status, "INFO", MessageBox.ERROR);
					if (status.equals("not_started")||status.equals("未开始")) {
						String taskName = task.getProperty("object_name");
						System.out.println("taskName="+taskName);
						if (isReplace) {
							taskName = replaceName(taskName, replaceSource.getText(), replaceObject.getText());
							task.setProperty("object_name", taskName);
						} else {
							switch (index) {
								case 0:
									System.out.println(" scheduleID + taskName=" +scheduleID + taskName);
									task.setProperty("object_name", scheduleID + taskName);
									break;
								case 1:
									task.setProperty("object_name", scheduleName + taskName);
									break;
								case 2:
									task.setProperty("object_name", txt + taskName);
									break;
							}
						}
					}
				}
			}
		}
		catch (TCException e) {
			e.printStackTrace();
		}
	}

	/**
	 * modifyDeliveryName:修改时间表交付物名称
	 * */
	public void modifyDeliveryName(int index, String txt, boolean isReplace) {
		try {
			System.out.println("modifyDeliveryName = " + tasks.length);
			for (int i = 0; i < tasks.length; i++) {
				TCComponentScheduleTask task = (TCComponentScheduleTask) tasks[i];
				String status = task.getProperty("status");
				if (status.equals("0")) {
//					String taskName = task.getProperty("object_name");
					TCComponent[] deliveries = task.getReferenceListProperty("sch_task_deliverable_list");
					if (deliveries != null) {
						for (int m = 0; m < deliveries.length; m++) {
							TCComponentTaskDeliverable delivery = (TCComponentTaskDeliverable) deliveries[m];
							
							System.out.println("delivery = " + delivery);
							TCComponentScheduleDeliverable scheduleDelivery = (TCComponentScheduleDeliverable) delivery.getReferenceProperty("schedule_deliverable");
							TCComponent deliverable_inst = scheduleDelivery.getReferenceProperty("deliverable_inst");
							if (deliverable_inst == null) {
								String deliverable_name = scheduleDelivery.getProperty("deliverable_name");
								String strTaskDeliverableName = delivery.getProperty("object_name");
								System.out.println("strTaskDeliverableName[" + i +"] = " + strTaskDeliverableName);
								if (isReplace) {
									deliverable_name = replaceName(deliverable_name, replaceSource.getText(), replaceObject.getText());
									scheduleDelivery.setProperty("deliverable_name", deliverable_name);
									scheduleDelivery.setProperty("object_name", deliverable_name);
									
									strTaskDeliverableName = replaceName(strTaskDeliverableName, replaceSource.getText(), replaceObject.getText());
									delivery.setProperty("object_name",strTaskDeliverableName);
								} else {
									switch (index) {
										case 0:
											scheduleDelivery.setProperty("deliverable_name", scheduleID + deliverable_name);
											scheduleDelivery.setProperty("object_name", scheduleID + deliverable_name);
											delivery.setProperty("object_name",scheduleID + strTaskDeliverableName);
											break;
										case 1:
											scheduleDelivery.setProperty("deliverable_name", scheduleName + deliverable_name);
											scheduleDelivery.setProperty("object_name", scheduleName + deliverable_name);
											delivery.setProperty("object_name",scheduleName + strTaskDeliverableName);
											break;
										case 2:
											scheduleDelivery.setProperty("deliverable_name", txt + deliverable_name);
											scheduleDelivery.setProperty("object_name", txt + deliverable_name);
											delivery.setProperty("object_name",txt + strTaskDeliverableName);
											break;
									}
								}
							}
						}
					}
				}
			}
		}
		catch (TCException e) {
			e.printStackTrace();
		}
	}

	
	public void replaceAction() {
		if(isModifyTaskName){
			modifyTaskName(-1, "", true);
		}
		if(isModifyDeliveryName){
			modifyDeliveryName(-1, "", true);	
		}
		this.dispose();
	}

	public void cancelAction() {
		this.dispose();
	}
}