// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.diff.tools.external;

import com.intellij.diff.util.DiffUtil;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.intellij.util.ObjectUtils.notNull;

@State(
  name = "ExternalDiffSettings",
  storages = @Storage(DiffUtil.DIFF_CONFIG)
)
public class ExternalDiffSettings implements PersistentStateComponent<ExternalDiffSettings.State> {
  private State myState = new State();

  @Override
  public State getState() {
    return myState;
  }

  @Override
  public void loadState(@NotNull State state) {
    myState = state;
  }

  public static ExternalDiffSettings getInstance() {
    return ServiceManager.getService(ExternalDiffSettings.class);
  }

  public static class State {
    @Nullable public Boolean DIFF_ENABLED = null;
    @Nullable public Boolean DIFF_DEFAULT = null;
    @Nullable public String DIFF_EXE_PATH = null;
    @Nullable public String DIFF_PARAMETERS = null;

    @Nullable public Boolean MERGE_ENABLED = null;
    @Nullable public String MERGE_EXE_PATH = null;
    @Nullable public String MERGE_PARAMETERS = null;
    public boolean MERGE_TRUST_EXIT_CODE = false;
  }

  public boolean isDiffEnabled() {
    return notNull(myState.DIFF_ENABLED, false);
  }

  public void setDiffEnabled(boolean value) {
    myState.DIFF_ENABLED = value;
  }

  public boolean isDiffDefault() {
    return notNull(myState.DIFF_DEFAULT, false);
  }

  public void setDiffDefault(boolean value) {
    myState.DIFF_DEFAULT = value;
  }

  @NotNull
  public String getDiffExePath() {
    return notNull(myState.DIFF_EXE_PATH, "");
  }

  public void setDiffExePath(@NotNull String path) {
    myState.DIFF_EXE_PATH = path;
  }

  @NotNull
  public String getDiffParameters() {
    return notNull(myState.DIFF_PARAMETERS, "%1 %2 %3");
  }

  public void setDiffParameters(@NotNull String path) {
    myState.DIFF_PARAMETERS = path;
  }


  public boolean isMergeEnabled() {
    return notNull(myState.MERGE_ENABLED, false);
  }

  public void setMergeEnabled(boolean value) {
    myState.MERGE_ENABLED = value;
  }

  @NotNull
  public String getMergeExePath() {
    return notNull(myState.MERGE_EXE_PATH, "");
  }

  public void setMergeExePath(@NotNull String path) {
    myState.MERGE_EXE_PATH = path;
  }

  @NotNull
  public String getMergeParameters() {
    return notNull(myState.MERGE_PARAMETERS, "%1 %2 %3 %4");
  }

  public void setMergeParameters(@NotNull String path) {
    myState.MERGE_PARAMETERS = path;
  }

  public boolean isMergeTrustExitCode() {
    return myState.MERGE_TRUST_EXIT_CODE;
  }

  public void setMergeTrustExitCode(boolean value) {
    myState.MERGE_TRUST_EXIT_CODE = value;
  }
}

@@ -476,105 +476,8 @@
 ull;
-%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport static com.intellij.util.ObjectUtils.notNull;
 %0A%0A@S
@@ -1010,34 +1010,24 @@
 e %7B%0A    
-@Nullable 
 public 
-B
+b
 oolean D
@@ -1044,36 +1044,27 @@
 D = 
-null
+false
 ;%0A    
-@Nullable 
 public 
-B
+b
 oole
@@ -1081,28 +1081,29 @@
 FAULT = 
-null
+false
 ;%0A    @N
 ullable 
@@ -1086,39 +1086,38 @@
  = false;%0A    @N
+otN
 ull
-able
  public String D
@@ -1127,36 +1127,34 @@
 _EXE_PATH = 
-null
+%22%22
 ;%0A    @N
 ullable publ
@@ -1133,39 +1133,38 @@
 ATH = %22%22;%0A    @N
+otN
 ull
-able
  public String D
@@ -1184,36 +1184,32 @@
 S = 
-null;%0A%0A    @Nullable
+%22%251 %252 %253%22;%0A%0A   
  public 
 Bool
@@ -1204,17 +1204,17 @@
  public 
-B
+b
 oolean M
@@ -1228,28 +1228,29 @@
 ABLED = 
-null
+false
 ;%0A    @N
 ullable 
@@ -1233,39 +1233,38 @@
  = false;%0A    @N
+otN
 ull
-able
  public String M
@@ -1283,20 +1283,18 @@
 H = 
-null
+%22%22
 ;%0A    @N
 ulla
@@ -1293,15 +1293,14 @@
   @N
+otN
 ull
-able
  pub
@@ -1333,12 +1333,21 @@
 S = 
-null
+%22%251 %252 %253 %254%22
 ;%0A  
@@ -1441,32 +1441,24 @@
 %0A    return 
-notNull(
 myState.DIFF
@@ -1461,32 +1461,24 @@
 DIFF_ENABLED
-, false)
 ;%0A  %7D%0A%0A  pub
@@ -1599,32 +1599,24 @@
 %0A    return 
-notNull(
 myState.DIFF
@@ -1623,24 +1623,16 @@
 _DEFAULT
-, false)
 ;%0A  %7D%0A%0A 
@@ -1768,32 +1768,24 @@
 %0A    return 
-notNull(
 myState.DIFF
@@ -1785,37 +1785,32 @@
 te.DIFF_EXE_PATH
-, %22%22)
 ;%0A  %7D%0A%0A  public 
@@ -1948,32 +1948,24 @@
 %0A    return 
-notNull(
 myState.DIFF
@@ -1979,21 +1979,8 @@
 TERS
-, %22%251 %252 %253%22)
 ;%0A  
@@ -2123,32 +2123,24 @@
 %0A    return 
-notNull(
 myState.MERG
@@ -2152,16 +2152,8 @@
 BLED
-, false)
 ;%0A  
@@ -2296,32 +2296,24 @@
 %0A    return 
-notNull(
 myState.MERG
@@ -2326,13 +2326,8 @@
 PATH
-, %22%22)
 ;%0A  
@@ -2488,16 +2488,8 @@
 urn 
-notNull(
 mySt
@@ -2512,24 +2512,8 @@
 TERS
-, %22%251 %252 %253 %254%22)
 ;%0A  

remove unnecessary nullability in PersistentStateComponent state