/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.tasks.impl;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.CheckinProjectPanel;
import com.intellij.openapi.vcs.changes.CommitContext;
import com.intellij.openapi.vcs.checkin.CheckinHandler;
import com.intellij.openapi.vcs.checkin.CheckinHandlerFactory;
import com.intellij.tasks.LocalTask;
import com.intellij.tasks.Task;
import com.intellij.tasks.TaskManager;
import com.intellij.tasks.TaskRepository;
import com.intellij.tasks.context.WorkingContextManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Date;

/**
 * @author Dmitry Avdeev
 *         Date: 12/29/11
 */
public class TaskCheckinHandlerFactory extends CheckinHandlerFactory {

  @NotNull
  @Override
  public CheckinHandler createHandler(final CheckinProjectPanel panel, final CommitContext commitContext) {
    return new CheckinHandler() {
      @Override
      public void checkinSuccessful() {
        final String message = panel.getCommitMessage();
        if (message != null) {
          final Project project = panel.getProject();
          final TaskManagerImpl manager = (TaskManagerImpl)TaskManager.getManager(project);
          if (manager.getState().saveContextOnCommit) {
            final Task task = findTask(message, manager);

            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                final WorkingContextManager contextManager = WorkingContextManager.getInstance(project);
                if (task != null) {
                  LocalTask localTask = manager.addTask(task);
                  localTask.setUpdated(new Date());
                  contextManager.saveContext(localTask);
                }
                else {
                  contextManager.saveContext(null, message);
                }
              }
            });
          }
        }
      }
    };
  }

  @Nullable
  private static Task findTask(String message, TaskManager manager) {
    TaskRepository[] repositories = manager.getAllRepositories();
    for (TaskRepository repository : repositories) {
      String id = repository.extractId(message);
      try {
        Task task = repository.findTask(id);
        if (task != null) {
          return task;
        }
      }
      catch (Exception ignore) {

      }
    }
    return null;
  }
}

@@ -1889,30 +1889,24 @@
             
-final 
 Task task = 
@@ -1933,17 +1933,16 @@
 nager);%0A
-%0A
         
@@ -1949,295 +1949,96 @@
     
-//noinspection SSBasedInspection%0A            SwingUtilities.invokeLater(new Runnable() %7B%0A              @Override%0A              public void run() %7B%0A                final WorkingContextManager contextManager = WorkingContextManager.getInstance(project);%0A                if (task != null) %7B
+if (task == null) %7B%0A              task = manager.createLocalTask(message);%0A            %7D
 %0A   
@@ -2046,21 +2046,21 @@
         
-     
+final
  LocalTa
@@ -2109,22 +2109,16 @@
         
-      
 localTas
@@ -2143,16 +2143,17 @@
 ate());%0A
+%0A
         
@@ -2160,52 +2160,96 @@
     
-      contextManager.saveContext(localTask);
+//noinspection SSBasedInspection%0A            SwingUtilities.invokeLater(new Runnable() %7B
 %0A   
@@ -2251,35 +2251,41 @@
 %7B%0A              
-  %7D
+@Override
 %0A               
@@ -2287,14 +2287,25 @@
     
-  else
+public void run()
  %7B%0A 
@@ -2319,19 +2319,24 @@
         
-  c
+WorkingC
 ontextMa
@@ -2345,53 +2345,52 @@
 ger.
-saveContext(null, message);%0A                %7D
+getInstance(project).saveContext(localTask);
 %0A   

save task on commit