/*
 * Copyright 2000-2012 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 org.jetbrains.idea.svn.history;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.VcsKey;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.changes.MarkerVcsContentRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.SvnRevisionNumber;
import org.jetbrains.idea.svn.SvnVcs;
import org.jetbrains.idea.svn.actions.AbstractShowPropertiesDiffAction;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNWCClient;

/**
 * Created with IntelliJ IDEA.
 * User: Irina.Chernushina
 * Date: 2/22/12
 * Time: 10:28 AM
 */
public class SvnLazyPropertyContentRevision implements ContentRevision, MarkerVcsContentRevision {
  private final FilePath myPath;
  private final VcsRevisionNumber myNumber;
  private final Project myProject;
  private final SVNURL myUrl;
  private String myContent;

  public SvnLazyPropertyContentRevision(FilePath path, VcsRevisionNumber number, Project project, SVNURL url) {
    myPath = path;
    myNumber = number;
    myProject = project;
    myUrl = url;
  }

  @Override
  public String getContent() throws VcsException {
    if (myContent == null) {
      myContent = loadContent();
    }
    return myContent;
  }

  private String loadContent() {
    final SvnVcs vcs = SvnVcs.getInstance(myProject);
    final SVNWCClient client = vcs.createWCClient();
    final Ref<String> ref = new Ref<String>();
    final Runnable runnable = new Runnable() {
      @Override
      public void run() {
        try {
          ref.set(AbstractShowPropertiesDiffAction.getPropertyList(myUrl, ((SvnRevisionNumber) myNumber).getRevision(), client));
        }
        catch (SVNException e) {
          // unknown node kind (node deleted)
          /*if (e.getErrorMessage().getErrorCode().getCode() == 145000) {
            return "";
          }*/
          ref.set("Can not get properties: " + e.getMessage());
        }
      }
    };
    if (ApplicationManager.getApplication().isDispatchThread()) {
      ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, SvnBundle.message("progress.title.loading.file.properties"),
                                                                        false, myProject);
    }
    else {
      runnable.run();
    }
    return ref.get();
  }

  @NotNull
  @Override
  public FilePath getFile() {
    return myPath;
  }

  @NotNull
  @Override
  public VcsRevisionNumber getRevisionNumber() {
    return myNumber;
  }

  @Override
  public VcsKey getVcsKey() {
    return SvnVcs.getKey();
  }
}

@@ -3023,16 +3023,42 @@
  %7B%0A     
+ final boolean completed =
  Progres
@@ -3079,16 +3079,25 @@
 stance()
+%0A        
 .runProc
@@ -3199,99 +3199,159 @@
 s%22),
-%0A                                                                        false, myProject);
+ true, myProject);%0A      if (! completed) %7B%0A        return %22Properties load for revision %22 + getRevisionNumber().asString() + %22 was canceled.%22;%0A      %7D
 %0A   

allow to cancel properties contents load, for: IDEA-91712 Requesting diff of file from svn while project is updating results in blocked UI