package com.intellij.codeInsight.daemon;

import com.intellij.codeInsight.daemon.impl.analysis.HighlightingSettingsPerFile;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.codeInspection.ex.InspectionProfileManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.ExportableApplicationComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.*;
import com.intellij.psi.PsiElement;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public class DaemonCodeAnalyzerSettings implements NamedJDOMExternalizable, Cloneable, ExportableApplicationComponent {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings");


  public DaemonCodeAnalyzerSettings() {
  }

  public static DaemonCodeAnalyzerSettings getInstance() {
    return ApplicationManager.getApplication().getComponent(DaemonCodeAnalyzerSettings.class);
  }

  public File[] getExportFiles() {
    return new File[]{PathManager.getOptionsFile(this)};
  }

  public String getPresentableName() {
    return "Error highlighting settings";
  }

  public boolean NEXT_ERROR_ACTION_GOES_TO_ERRORS_FIRST = false;
  public int AUTOREPARSE_DELAY = 300;
  public boolean SHOW_ADD_IMPORT_HINTS = true;
  public String NO_AUTO_IMPORT_PATTERN = "[a-z].?";
  public boolean SUPPRESS_WARNINGS = true;
  public boolean SHOW_METHOD_SEPARATORS = false;
  public int ERROR_STRIPE_MARK_MIN_HEIGHT = 3;

  private InspectionProfileImpl myInspectionProfile;

  public InspectionProfileImpl getInspectionProfile() {
    if (myInspectionProfile == null) {
      final String[] avaliableProfileNames = InspectionProfileManager.getInstance().getAvaliableProfileNames();
      if (avaliableProfileNames == null || avaliableProfileNames.length == 0){
        //can't be
        return null;
      } else {
        myInspectionProfile = InspectionProfileManager.getInstance().getProfile(avaliableProfileNames[0]);
      }
    }
    if (!myInspectionProfile.wasInitialized()) {
      myInspectionProfile.load();
    }
    return myInspectionProfile;
  }

  public InspectionProfileImpl getInspectionProfile(PsiElement psiRoot){
    InspectionProfileImpl inspectionProfile = psiRoot == null ? null : HighlightingSettingsPerFile.getInstance(psiRoot.getProject()).getInspectionProfile(psiRoot);
    return inspectionProfile != null ? inspectionProfile : getInspectionProfile();
  }

  //set in error settings to hightlight only
  public void setInspectionProfile(InspectionProfileImpl inspectionProfile) {
    myInspectionProfile = inspectionProfile;
  }

  public boolean isCodeHighlightingChanged(DaemonCodeAnalyzerSettings oldSettings) {
    try {
      Element rootNew = new Element("root");
      writeExternal(rootNew);

      Element rootOld = new Element("root");
      oldSettings.writeExternal(rootOld);

      if (JDOMUtil.areElementsEqual(rootOld, rootNew)) {
        oldSettings.myInspectionProfile.writeExternal(rootOld);
        myInspectionProfile.writeExternal(rootNew);
        return !JDOMUtil.areElementsEqual(rootOld, rootNew);
      }
      else {
        return true;
      }
    }
    catch (WriteExternalException e) {
      LOG.error(e);
    }

    return false;
  }

  public String getComponentName() {
    return "DaemonCodeAnalyzerSettings";
  }

  public void initComponent() { }

  public void disposeComponent() {
  }

  public String getExternalFileName() {
    return "editor.codeinsight";
  }

  public Object clone() {
    DaemonCodeAnalyzerSettings settings = new DaemonCodeAnalyzerSettings();
    InspectionProfileImpl inspectionProfile = new InspectionProfileImpl("copy", InspectionProfileManager.getInstance());
    inspectionProfile.copyFrom(getInspectionProfile());
    settings.myInspectionProfile = inspectionProfile;
    settings.AUTOREPARSE_DELAY = AUTOREPARSE_DELAY;
    settings.SHOW_ADD_IMPORT_HINTS = SHOW_ADD_IMPORT_HINTS;
    settings.SHOW_METHOD_SEPARATORS = SHOW_METHOD_SEPARATORS;
    settings.NO_AUTO_IMPORT_PATTERN = NO_AUTO_IMPORT_PATTERN;
    return settings;
  }

  public void readExternal(Element element) throws InvalidDataException {
    DefaultJDOMExternalizer.readExternal(this, element);
    InspectionProfileConvertor.getInstance().storeEditorHighlightingProfile(element);
    String profileName = element.getAttributeValue("profile");
    if (profileName != null) {
      myInspectionProfile = InspectionProfileManager.getInstance().getProfile(profileName);
    }
    else {
      myInspectionProfile =
      InspectionProfileManager.getInstance().getProfile(InspectionProfileConvertor.OLD_HIGHTLIGHTING_SETTINGS_PROFILE);
    }
  }

  public void writeExternal(Element element) throws WriteExternalException {
    DefaultJDOMExternalizer.writeExternal(this, element);
    //clone
    if (myInspectionProfile != null) {
      element.setAttribute("profile", myInspectionProfile.getName());
    }
    else {
      element.setAttribute("profile", "Default");
    }
  }

  public boolean isImportHintEnabled() {
    return SHOW_ADD_IMPORT_HINTS;
  }

  public void setImportHintEnabled(boolean isImportHintEnabled) {
    SHOW_ADD_IMPORT_HINTS = isImportHintEnabled;
  }

}

@@ -573,51 +573,8 @@
 ent;
-%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aim
@@ -1728,79 +1728,169 @@
 nal 
-String%5B%5D avaliableProfileNames = InspectionProfileManager.getInstance()
+InspectionProfileManager inspectionProfileManager = InspectionProfileManager.getInstance();%0A      final String%5B%5D avaliableProfileNames = inspectionProfileManager
 .get
@@ -2011,16 +2011,24 @@
 can't be
+ but ...
 %0A       
@@ -2035,20 +2035,63 @@
  return 
-null
+inspectionProfileManager.createDefaultProfile()
 ;%0A      
@@ -2121,33 +2121,33 @@
 ectionProfile = 
-I
+i
 nspectionProfile
@@ -2150,38 +2150,24 @@
 fileManager.
-getInstance().
 getProfile(a
@@ -4575,24 +4575,126 @@
 %22profile%22);%0A
+    final InspectionProfileManager inspectionProfileManager = InspectionProfileManager.getInstance();%0A
     if (prof
@@ -4740,17 +4740,17 @@
 ofile = 
-I
+i
 nspectio
@@ -4761,38 +4761,24 @@
 fileManager.
-getInstance().
 getProfile(p
@@ -4838,16 +4838,10 @@
 le =
-%0A      I
+ i
 nspe
@@ -4864,94 +4864,29 @@
 ger.
-getInstance().getProfile(InspectionProfileConvertor.OLD_HIGHTLIGHTING_SETTINGS_PROFILE
+createDefaultProfile(
 );%0A 

set up default profile on 45 migration