/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.mustbe.consulo.csharp.ide.highlight.quickFix;

import org.jetbrains.annotations.NotNull;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.util.IncorrectOperationException;

/**
 * @author VISTALL
 * @since 12.11.14
 */
public class RenameQuickFix implements IntentionAction
{
	private final String myNewName;
	private final SmartPsiElementPointer<PsiNamedElement> myPointer;

	public RenameQuickFix(@NotNull String newName, @NotNull PsiNamedElement namedElement)
	{
		myNewName = newName;
		myPointer = SmartPointerManager.getInstance(namedElement.getProject()).createSmartPsiElementPointer(namedElement);
	}

	@NotNull
	@Override
	public String getText()
	{
		PsiNamedElement element = myPointer.getElement();
		if(element == null)
		{
			throw new IllegalArgumentException();
		}
		return "Rename '" + element.getName() + "' to '" + myNewName + "'";
	}

	@NotNull
	@Override
	public String getFamilyName()
	{
		return "C#";
	}

	@Override
	public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file)
	{
		return myPointer.getElement() != null;
	}

	@Override
	public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
	{
		PsiNamedElement element = myPointer.getElement();
		if(element == null)
		{
			return;
		}
		element.setName(myNewName);
	}

	@Override
	public boolean startInWriteAction()
	{
		return true;
	}
}

@@ -733,16 +733,25 @@
 tention.
+impl.Base
 Intentio
@@ -759,16 +759,16 @@
 Action;%0A
-
 import c
@@ -1147,19 +1147,20 @@
 Fix 
-implement
+extend
 s 
+Base
 Inte
@@ -1508,192 +1508,17 @@
 );%0A%09
-%7D%0A%0A%09@NotNull%0A%09@Override%0A%09public String getText()%0A%09%7B%0A%09%09PsiNamedElement element = myPointer.getElement();%0A%09%09if(element == null)%0A%09%09%7B%0A%09%09%09throw new IllegalArgumentException();%0A%09%09%7D%0A%09%09return 
+%09setText(
 %22Ren
@@ -1526,17 +1526,22 @@
 me '%22 + 
-e
+namedE
 lement.g
@@ -1577,16 +1577,17 @@
 me + %22'%22
+)
 ;%0A%09%7D%0A%0A%09@
@@ -2017,16 +2017,16 @@
 rn;%0A%09%09%7D%0A
+
 %09%09elemen
@@ -2053,77 +2053,7 @@
 ;%0A%09%7D
-%0A%0A%09@Override%0A%09public boolean startInWriteAction()%0A%09%7B%0A%09%09return true;%0A%09%7D
 %0A%7D%0A

use BaseIntentionAction