package seedu.address.logic.commands;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.UnmodifiableObservableList;
import seedu.address.model.task.ReadOnlyTask;
import seedu.address.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * Deletes a person identified using it's last displayed index from the address book.
 */
public class DeleteCommand extends Command {

    public static final String COMMAND_WORD = "delete";

    public static final String MESSAGE_USAGE = COMMAND_WORD
            + ": Deletes the  Task identified by the index number or specific name used in the last Task listing.\n"
            + "Parameters: INDEX (must be a positive integer) or NAME\n"
            + "Example: " + COMMAND_WORD + " 1 or horror night";

    public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted Task: %1 or event $s";

    public final int targetIndex;
    public final String name;
    public DeleteCommand(int targetIndex) {
        this.targetIndex = targetIndex;
        this.name=null;
    }

    public DeleteCommand(String name){
    	this.name=name;
    	this.targetIndex=-1;
    }
    @Override
    public CommandResult execute() {
        ReadOnlyTask TaskToDelete=null;
        UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();
        if(targetIndex!=-1){
        if (lastShownList.size() < targetIndex) {
            indicateAttemptToExecuteIncorrectCommand();
            return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

        TaskToDelete = lastShownList.get(targetIndex - 1);
        }else{
        	for(ReadOnlyTask e:lastShownList){
        		if(name.trim().equals(e.getName().toString())){
        			TaskToDelete=e;
        			break;
        		}
        	}
        	if(TaskToDelete==null)
        	return new CommandResult(Messages.MESSAGE_INVALID_TASK_NAME);
        }
        try {
            model.deleteTask(TaskToDelete);
        } catch (TaskNotFoundException pnfe) {
            assert false : "The target task cannot be missing";
        }

        return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, TaskToDelete));
    }

}

@@ -850,18 +850,8 @@
 : %251
- or event 
 $s%22;
@@ -912,24 +912,29 @@
 tring name;%0A
+    %0A
     public D
@@ -1022,17 +1022,19 @@
 his.name
-=
+ = 
 null;%0A  
@@ -1091,17 +1091,19 @@
 his.name
-=
+ = 
 name;%0A  
@@ -1121,17 +1121,19 @@
 getIndex
-=
+ = 
 -1;%0A    
@@ -1130,24 +1130,29 @@
 = -1;%0A    %7D%0A
+    %0A
     @Overrid
@@ -1223,17 +1223,19 @@
 ToDelete
-=
+ = 
 null;%0A  
@@ -1336,16 +1336,17 @@
       if
+ 
 (targetI
@@ -1353,15 +1353,22 @@
 ndex
+ 
 !=
+ 
 -1)
+ 
 %7B%0A
+    
     
@@ -1425,16 +1425,20 @@
         
+  
+  
 indicate
@@ -1469,24 +1469,28 @@
 tCommand();%0A
+    
             
@@ -1562,35 +1562,43 @@
 INDEX);%0A        
+   
+ 
 %7D%0A%0A
+    
         TaskToDe
@@ -1653,12 +1653,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -1669,12 +1669,54 @@
     
+  
-%09
+  assert this.name != null;%0A            
 for
+ 
 (Rea
@@ -1726,17 +1726,19 @@
 lyTask e
-:
+ : 
 lastShow
@@ -1743,16 +1743,17 @@
 ownList)
+ 
 %7B%0A      
@@ -1758,12 +1758,19 @@
     
+  
-%09%09
+      
 if
+ 
 (nam
@@ -1809,16 +1809,17 @@
 ring()))
+ 
 %7B%0A      
@@ -1820,19 +1820,28 @@
         
-%09%09%09
+            
 TaskToDe
@@ -1844,17 +1844,19 @@
 ToDelete
-=
+ = 
 e;%0A     
@@ -1862,11 +1862,20 @@
     
-%09%09%09
+            
 brea
@@ -1889,20 +1889,30 @@
     
+     
-%09%09%7D%0A
+   %7D%0A    
         
 %09%7D%0A 
@@ -1907,17 +1907,16 @@
         
-%09
 %7D%0A      
@@ -1921,11 +1921,15 @@
     
+    
-%09
 if
+ 
 (Tas
@@ -1941,10 +1941,12 @@
 lete
+ 
 ==
+ 
 null
@@ -1955,17 +1955,24 @@
         
-%09
+        
 return n

Fix exceptions caused by DeleteCommand