package com.github.gtache.lsp.settings;

import com.github.gtache.lsp.PluginMain;
import com.github.gtache.lsp.client.languageserver.serverdefinition.UserConfigurableServerDefinition;
import com.github.gtache.lsp.client.languageserver.serverdefinition.UserConfigurableServerDefinition$;
import com.github.gtache.lsp.requests.Timeout;
import com.github.gtache.lsp.requests.Timeouts;
import com.github.gtache.lsp.utils.ApplicationUtils$;
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 com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

@State(name = "LSPState", storages = @Storage(file = "LSPState.xml"))

/**
 * Class representing the state of the LSP settings
 */
public final class LSPState implements PersistentStateComponent<LSPState> {

    private static final Logger LOG = Logger.getInstance(LSPState.class);

    //Must be public to be saved
    public boolean alwaysSendRequests = false;
    public Map<String, String[]> extToServ = new LinkedHashMap<>(10);
    public Map<Timeouts, Integer> timeouts = new EnumMap<>(Timeouts.class);
    public List<String> coursierResolvers = new ArrayList<>(5);
    public Map<String[], String[]> forcedAssociations = new HashMap<>(10);

    public LSPState() {
    }

    @Nullable
    public static LSPState getInstance() {
        try {
            return ServiceManager.getService(LSPState.class);
        } catch (final Exception e) {
            LOG.warn("Couldn't load LSPState");
            LOG.warn(e);
            ApplicationUtils$.MODULE$.invokeLater(() -> Messages.showErrorDialog("Couldn't load LSP settings, you will need to reconfigure them.", "LSP plugin"));
            return null;
        }
    }

    public List<String> getCoursierResolvers() {
        return coursierResolvers;
    }

    public void setCoursierResolvers(final Collection<String> coursierResolvers) {
        this.coursierResolvers = new ArrayList<>(coursierResolvers);
    }

    public Map<String, UserConfigurableServerDefinition> getExtToServ() {
        return UserConfigurableServerDefinition$.MODULE$.fromArrayMap(extToServ);
    }

    public void setExtToServ(final Map<String, UserConfigurableServerDefinition> extToServ) {
        this.extToServ = UserConfigurableServerDefinition$.MODULE$.toArrayMap(extToServ);
    }

    public boolean isAlwaysSendRequests() {
        return alwaysSendRequests;
    }

    public void setAlwaysSendRequests(final boolean b) {
        this.alwaysSendRequests = b;
    }

    public Map<Timeouts, Integer> getTimeouts() {
        return timeouts;
    }

    public void setTimeouts(final Map<Timeouts, Integer> timeouts) {
        this.timeouts = new EnumMap<>(timeouts);
    }

    public Map<String[], String[]> getForcedAssociations() {
        return forcedAssociations;
    }

    public void setForcedAssociations(final Map<String[], String[]> forcedAssociations) {
        this.forcedAssociations = new HashMap<>(forcedAssociations);
    }

    @Override
    public LSPState getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull final LSPState lspState) {
        try {
            XmlSerializerUtil.copyBean(lspState, this);
            LOG.info("LSP State loaded");
            if (extToServ != null && !extToServ.isEmpty()) {
                PluginMain.setExtToServerDefinition(UserConfigurableServerDefinition$.MODULE$.fromArrayMap(extToServ));
            }
            if (timeouts != null && !timeouts.isEmpty()) {
                Timeout.setTimeouts(timeouts);
            }
            if (forcedAssociations != null && !forcedAssociations.isEmpty()) {
                PluginMain.setForcedAssociations(forcedAssociations);
            }
        } catch (final Exception e) {
            LOG.warn("Couldn't load LSPState");
            LOG.warn(e);
            ApplicationUtils$.MODULE$.invokeLater(() -> Messages.showErrorDialog("Couldn't load LSP settings, you will need to reconfigure them.", "LSP plugin"));
        }
    }

}

@@ -1244,16 +1244,8 @@
 ests
- = false
 ;%0A  
@@ -1288,34 +1288,8 @@
 Serv
- = new LinkedHashMap%3C%3E(10)
 ;%0A  
@@ -1332,40 +1332,8 @@
 outs
- = new EnumMap%3C%3E(Timeouts.class)
 ;%0A  
@@ -1375,29 +1375,8 @@
 vers
- = new ArrayList%3C%3E(5)
 ;%0A  
@@ -1418,39 +1418,125 @@
 rcedAssociations
+;%0A%0A    public LSPState() %7B%0A        alwaysSendRequests = false;%0A        extToServ
  = new 
+Linked
 HashMap%3C%3E(10);%0A%0A
@@ -1538,32 +1538,153 @@
 0);%0A
-%0A    public LSPState() %7B
+        timeouts = new EnumMap%3C%3E(Timeouts.class);%0A        coursierResolvers = new ArrayList%3C%3E(5);%0A        forcedAssociations = new HashMap%3C%3E(10);
 %0A   
@@ -1906,33 +1906,15 @@
 tate
-%22);%0A            LOG.warn(
+ : %22 + 
 e);%0A
@@ -3376,32 +3376,700 @@
 ations);%0A    %7D%0A%0A
+    @Override%0A    public int hashCode() %7B%0A        return Boolean.hashCode(alwaysSendRequests) + 3 * extToServ.hashCode() + 7 * timeouts.hashCode() + 11 * coursierResolvers.hashCode() + 13 * forcedAssociations.hashCode();%0A    %7D%0A%0A    @Override%0A    public boolean equals(final Object that) %7B%0A        if (that instanceof LSPState) %7B%0A            final LSPState thatS = (LSPState) that;%0A            return alwaysSendRequests == thatS.alwaysSendRequests && extToServ.equals(thatS.extToServ) && timeouts.equals(thatS.timeouts) && coursierResolvers.equals(thatS.coursierResolvers) && forcedAssociations.equals(thatS.forcedAssociations);%0A        %7D%0A        return false;%0A    %7D%0A%0A%0A
     @Override%0A  
@@ -4352,32 +4352,8 @@
 null
- && !extToServ.isEmpty()
 ) %7B%0A
@@ -4522,31 +4522,8 @@
 null
- && !timeouts.isEmpty()
 ) %7B%0A
@@ -4629,41 +4629,8 @@
 null
- && !forcedAssociations.isEmpty()
 ) %7B%0A
@@ -4799,33 +4799,15 @@
 tate
-%22);%0A            LOG.warn(
+ : %22 + 
 e);%0A

Update LSPState