package com.guyazhou.tools.plugin.reviewboard.setting;

import com.guyazhou.tools.plugin.reviewboard.forms.ReviewBoardSettingForm;
import com.intellij.openapi.components.*;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * ReviewBoard setting
 * Created by Yakov on 2017/1/2.
 */
@State(
        name = "com.guyazhou.tools.plugin.ReviewBoardSetting",
        storages = {@Storage("rbs.xml")}
)
public class ReviewBoardSetting implements PersistentStateComponent<ReviewBoardSetting.State>, Configurable {

    private static final String DISPLAY_NAME = "Review Board";
    private ReviewBoardSettingForm reviewBoardSettingForm;
    private State state = new State();

    public ReviewBoardSetting() {
        this.reviewBoardSettingForm = new ReviewBoardSettingForm();
    }

    /**
     * Get a instance of the persistent reviewboard setting
     * @return A configured ReviewBoardSetting instance
     */
    public static ReviewBoardSetting getInstance() {
        return ServiceManager.getService(ReviewBoardSetting.class);
    }

    /**
     * Inner class, for persistent state of reviewboard setting info
     */
    public static class State {

        private String serverURL;
        private String username;
        private String password;
        private String groups;
        private String people;
        private String companionUsername;
        private String companionPassword;

        public String getServerURL() {
            return serverURL;
        }
        public void setServerURL(String serverURL) {
            this.serverURL = serverURL;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getGroups() {
            return groups;
        }
        public void setGroups(String groups) {
            this.groups = groups;
        }
        public String getPeople() {
            return people;
        }
        public void setPeople(String people) {
            this.people = people;
        }

        public String getCompanionUsername() {
            return companionUsername;
        }

        public void setCompanionUsername(String companionUsername) {
            this.companionUsername = companionUsername;
        }

        public String getCompanionPassword() {
            return companionPassword;
        }

        public void setCompanionPassword(String companionPassword) {
            this.companionPassword = companionPassword;
        }
    }

    public static String getSettingName() {
        return DISPLAY_NAME;
    }

    /******** implements from persistentStateComponent *******/
    @Nullable
    @Override
    public State getState() {
        return this.state;
    }

    @Override
    public void loadState(State persistentState) {
        if (null != persistentState) {
            XmlSerializerUtil.copyBean(persistentState, this.state);
        }
    }

    /******* implement from Configurable *********/
    @Nls
    @Override
    public String getDisplayName() {
        return DISPLAY_NAME;
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;    // No help topic
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        return reviewBoardSettingForm.getReviewBoardSettingPanel();
    }

    @Override
    public boolean isModified() {
        State persistentState = ReviewBoardSetting.getInstance().getState();
        if (null == persistentState) {
            return !"".equals(reviewBoardSettingForm.getServerURL())
                    | !"".equals(reviewBoardSettingForm.getUsername())
                    | !"".equals(reviewBoardSettingForm.getPassword())
                    | !"".equals(reviewBoardSettingForm.getGroups())
                    | !"".equals(reviewBoardSettingForm.getPeople())
                    | !"".equals(reviewBoardSettingForm.getCompanionUsername())
                    | !"".equals(reviewBoardSettingForm.getCompanionPassword());
        } else {
            return !persistentState.getServerURL().equals(reviewBoardSettingForm.getServerURL())
                    | !persistentState.getUsername().equals(reviewBoardSettingForm.getUsername())
                    | !persistentState.getPassword().equals(reviewBoardSettingForm.getPassword())
                    | !persistentState.getGroups().equals(reviewBoardSettingForm.getGroups())
                    | !persistentState.getPeople().equals(reviewBoardSettingForm.getPeople())
                    | !persistentState.getCompanionUsername().equals(reviewBoardSettingForm.getCompanionUsername())
                    | !persistentState.getCompanionPassword().equals(reviewBoardSettingForm.getCompanionPassword());
        }
    }

    @SuppressWarnings("Duplicates")
    @Override
    public void apply() throws ConfigurationException {
        State persistentState = ReviewBoardSetting.getInstance().getState();
        if (null == persistentState) {
            persistentState = new State();
        }
        persistentState.setServerURL(reviewBoardSettingForm.getServerURL());
        persistentState.setUsername(reviewBoardSettingForm.getUsername());
        persistentState.setPassword(reviewBoardSettingForm.getPassword());
        persistentState.setGroups(reviewBoardSettingForm.getGroups());
        persistentState.setPeople(reviewBoardSettingForm.getPeople());
        persistentState.setCompanionUsername(reviewBoardSettingForm.getCompanionUsername());
        persistentState.setCompanionPassword(reviewBoardSettingForm.getCompanionPassword());
    }

    @SuppressWarnings("Duplicates")
    @Override
    public void reset() {
        State persistentState = ReviewBoardSetting.getInstance().getState();
        if (null == persistentState) {
            return;
        }
        reviewBoardSettingForm.setServerURL(persistentState.getServerURL());
        reviewBoardSettingForm.setUsername(persistentState.getUsername());
        reviewBoardSettingForm.setPassword(persistentState.getPassword());
        reviewBoardSettingForm.setGroups(persistentState.getGroups());
        reviewBoardSettingForm.setPeople(persistentState.getPeople());
        reviewBoardSettingForm.setCompanionUsername(persistentState.getCompanionUsername());
        reviewBoardSettingForm.setCompanionPassword(persistentState.getCompanionPassword());
    }

}

@@ -4489,31 +4489,38 @@
 return !
-persistentState
+reviewBoardSettingForm
 .getServ
@@ -4534,38 +4534,31 @@
 .equals(
-reviewBoardSettingForm
+persistentState
 .getServ
@@ -4593,355 +4593,355 @@
  %7C !
-persistentState.getUsername().equals(reviewBoardSettingForm.getUsername())%0A                    %7C !persistentState.getPassword().equals(reviewBoardSettingForm.getPassword())%0A                    %7C !persistentState.getGroups().equals(reviewBoardSettingForm.getGroups())%0A                    %7C !persistentState.getPeople().equals(reviewBoardSettingForm
+reviewBoardSettingForm.getUsername().equals(persistentState.getUsername())%0A                    %7C !reviewBoardSettingForm.getPassword().equals(persistentState.getPassword())%0A                    %7C !reviewBoardSettingForm.getGroups().equals(persistentState.getGroups())%0A                    %7C !reviewBoardSettingForm.getPeople().equals(persistentState
 .get
@@ -4969,39 +4969,46 @@
          %7C !
-persistentState
+reviewBoardSettingForm
 .getCompanio
@@ -5026,38 +5026,31 @@
 .equals(
-reviewBoardSettingForm
+persistentState
 .getComp
@@ -5089,31 +5089,38 @@
      %7C !
-persistentState
+reviewBoardSettingForm
 .getComp
@@ -5142,38 +5142,31 @@
 .equals(
-reviewBoardSettingForm
+persistentState
 .getComp

fix initial bug