package com.karateca.jstoolbox.torelated;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.karateca.jstoolbox.MyAction;
import com.karateca.jstoolbox.config.JsToolboxSettings;

/**
 * @author Andres Dominguez.
 */
public abstract class GoToRelatedAction extends MyAction {

  private final String fileSuffix;
  final String viewSuffix;
  final String testSuffix;

  public GoToRelatedAction() {
    JsToolboxSettings settings = new JsToolboxSettings();

    fileSuffix = settings.getFileSuffix();
    viewSuffix = settings.getViewSuffix();
    testSuffix = settings.getTestSuffix();
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    // Disable if dependencies are not met.
    if (!canEnableAction(e)) {
      return;
    }

    String fileName = getCurrentFileName(e);

    String destinationSuffix = getDestinationSuffix();

    // Is it at destination? Go back to file.
    if (fileName.endsWith(destinationSuffix)) {
      goToFile(e, destinationSuffix, fileSuffix);
    } else if (isFileFile(fileName)) {
      // If I'm in file then go to destination.
      goToFile(e, fileSuffix, getDestinationSuffix());
    } else if (isViewFile(fileName)) {
      // Go from view to test.
      goToFile(e, viewSuffix, testSuffix);
    } else if (isTestFile(fileName)) {
      // Go from test to view.
      goToFile(e, testSuffix, viewSuffix);
    }
  }

  boolean isFileFile(String fileName) {
    return !isTestFile(fileName) && !isViewFile(fileName);
  }

  boolean isViewFile(String fileName) {
    return fileName.endsWith(viewSuffix);
  }

  boolean isTestFile(String fileName) {
    return fileName.endsWith(testSuffix);
  }

  abstract String getDestinationSuffix();
}

@@ -296,22 +296,16 @@
 private 
-final 
 String f
@@ -313,30 +313,24 @@
 leSuffix;%0A  
-final 
 String viewS
@@ -342,14 +342,8 @@
 ;%0A  
-final 
 Stri
@@ -362,232 +362,8 @@
 x;%0A%0A
-  public GoToRelatedAction() %7B%0A    JsToolboxSettings settings = new JsToolboxSettings();%0A%0A    fileSuffix = settings.getFileSuffix();%0A    viewSuffix = settings.getViewSuffix();%0A    testSuffix = settings.getTestSuffix();%0A  %7D%0A%0A
   @O
@@ -512,15 +512,34 @@
 rn;%0A
-
     %7D%0A%0A
+    readConfig();%0A%0A
     
@@ -1155,24 +1155,247 @@
 %0A    %7D%0A  %7D%0A%0A
+  private void readConfig() %7B%0A    JsToolboxSettings settings = new JsToolboxSettings();%0A%0A    fileSuffix = settings.getFileSuffix();%0A    viewSuffix = settings.getViewSuffix();%0A    testSuffix = settings.getTestSuffix();%0A  %7D%0A%0A
   boolean is

read config every time