/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.jenkins.plugins.googlecontainerregistryauth;

import javax.annotation.Nullable;

import com.google.api.client.util.Strings;

import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import jenkins.model.GlobalConfiguration;

import net.sf.json.JSONObject;


/**
 * Provides global configuration for this plgin.
 */
@Extension
public class GoogleContainerRegistryCredentialGlobalConfig
    extends GlobalConfiguration {
  private static final String GCR_SERVER = "gcr.io,*.gcr.io";

  public GoogleContainerRegistryCredentialGlobalConfig() {
    oldDescriptor = new GoogleContainerRegistryCredential.DescriptorImpl();
    if (Strings.isNullOrEmpty(oldDescriptor.getGcrServer())) {
      load();
    } else {
      gcrServer = oldDescriptor.getGcrServer();
    }
  }

  /** {@inheritDoc} */
  @Override
  public String getDisplayName() {
    return Messages.
        GoogleContainerRegistryCredential_GlobalDisplayName();
  }

  /** {@inheritDoc} */
  @Override
  public boolean configure(StaplerRequest req, JSONObject json)
      throws FormException {
    json = json.getJSONObject(getDisplayName());
    gcrServer = json.has("gcrServer") ?
        json.getString("gcrServer") : null;
    save();
    oldDescriptor.deleteConfigFile();
    return true;
  }

  /**
   * @return the Google Registry Container server URL.
   */
  @Nullable public String getGcrServer() {
    return Strings.isNullOrEmpty(gcrServer) ? GCR_SERVER : gcrServer;
  }

  private String gcrServer = null;
  GoogleContainerRegistryCredential.DescriptorImpl oldDescriptor;
}

@@ -1178,24 +1178,71 @@
 lConfig() %7B%0A
+    load();%0A    if (oldDescriptor == null) %7B%0A  
     oldDescr
@@ -1305,24 +1305,30 @@
 Impl();%0A
+    %7D%0A
     if (
 Strings.
@@ -1319,16 +1319,17 @@
     if (
+!
 Strings.
@@ -1385,29 +1385,50 @@
     
-load();%0A    %7D else
+if (Strings.isNullOrEmpty(gcrServer))
  %7B%0A
+  
     
@@ -1467,24 +1467,88 @@
 crServer();%0A
+        save();%0A      %7D%0A      oldDescriptor.deleteConfigFile();%0A
     %7D%0A  %7D%0A%0A 
@@ -1982,46 +1982,8 @@
 ();%0A
-    oldDescriptor.deleteConfigFile();%0A
     
@@ -2185,16 +2185,149 @@
 r;%0A  %7D%0A%0A
+  protected Object readResolve() %7B%0A    oldDescriptor = new GoogleContainerRegistryCredential.DescriptorImpl();%0A    return this;%0A  %7D%0A%0A
   privat
@@ -2353,18 +2353,28 @@
 = null;%0A
-
   
+transient 
 GoogleCo

fix NPE when saving global config after restart