// Copyright (C) 2014 The Android Open Source Project
//
// 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.googlesource.gerrit.plugins.serviceuser.client;

import com.google.gerrit.plugin.client.Plugin;
import com.google.gerrit.plugin.client.rpc.NativeString;
import com.google.gerrit.plugin.client.rpc.NoContent;
import com.google.gerrit.plugin.client.rpc.RestApi;
import com.google.gerrit.plugin.client.screen.Screen;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ServiceUserScreen extends VerticalPanel {
  static class Factory implements Screen.EntryPoint {
    @Override
    public void onLoad(Screen screen) {
      screen.setPageTitle("Service User " + screen.getToken(1));
      screen.show(new ServiceUserScreen(screen.getToken(1)));
    }
  }

  ServiceUserScreen(String serviceUser) {
    setStyleName("serviceuser-panel");

    new RestApi("config").id("server").view(Plugin.get().getPluginName(), "serviceusers")
        .id(serviceUser).get(new AsyncCallback<ServiceUserInfo>() {
            @Override
            public void onSuccess(final ServiceUserInfo serviceUserInfo) {
              new RestApi("config").id("server")
                  .view(Plugin.get().getPluginName(), "config")
                  .get(new AsyncCallback<ConfigInfo>() {
                    @Override
                    public void onSuccess(ConfigInfo configInfo) {
                      display(serviceUserInfo, configInfo.getAllowEmail());
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                      // never invoked
                    }
                  });
            }

            @Override
            public void onFailure(Throwable caught) {
              // never invoked
            }
          });
  }

  private void display(ServiceUserInfo info, boolean allowEmail) {
    MyTable t = new MyTable();
    t.setStyleName("serviceuser-serviceUserInfoTable");
    t.addRow("Account State", createActiveToggle(info));
    t.addRow("Username", info.username());
    t.addRow("Full Name", new EditableValue(info.username(), info.name()) {
      @Override
      protected void save(String serviceUser, final String newValue) {
        new RestApi("config").id("server")
            .view(Plugin.get().getPluginName(), "serviceusers").id(serviceUser)
            .view("name").put(newValue, new AsyncCallback<NativeString>() {
              @Override
              public void onSuccess(NativeString result) {
                updateValue(newValue);
              }

              @Override
              public void onFailure(Throwable caught) {
                // never invoked
              }
            });
      }
    });
    if (allowEmail) {
      t.addRow("Email Address", new EditableValue(info.username(), info.email()) {
        @Override
        protected void save(String serviceUser, final String newValue) {
          new RestApi("config").id("server")
              .view(Plugin.get().getPluginName(), "serviceusers").id(serviceUser)
              .view("email").put(newValue, new AsyncCallback<NativeString>() {
                @Override
                public void onSuccess(NativeString result) {
                  updateValue(newValue);
                }

                @Override
                public void onFailure(Throwable caught) {
                  // never invoked
                }
              });
        }
      });
    } else {
      t.addRow("Email Address", info.email());
    }
    t.addRow("Created By", info.created_by());
    t.addRow("Created At", info.created_at());
    add(t);

    add(new SshPanel(info.username()));
  }

  private ToggleButton createActiveToggle(final ServiceUserInfo info) {
    final ToggleButton activeToggle = new ToggleButton();
    activeToggle.setStyleName("serviceuser-toggleButton");
    activeToggle.setVisible(false);
    activeToggle.setValue(true);
    activeToggle.setText("Active");
    activeToggle.setValue(false);
    activeToggle.setText("Inactive");
    activeToggle.setValue(info.active());
    activeToggle.setVisible(true);

    activeToggle.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
      @Override
      public void onValueChange(ValueChangeEvent<Boolean> event) {
        if (event.getValue()) {
          new RestApi("config").id("server")
              .view(Plugin.get().getPluginName(), "serviceusers")
              .id(info.username()).view("active")
              .put(new AsyncCallback<NoContent>() {
                @Override
                public void onSuccess(NoContent result) {
                }

                @Override
                public void onFailure(Throwable caught) {
                  // never invoked
                }
              });
        } else {
          new RestApi("config").id("server")
              .view(Plugin.get().getPluginName(), "serviceusers")
              .id(info.username()).view("active")
              .delete(new AsyncCallback<NoContent>() {
                @Override
                public void onSuccess(NoContent result) {
                }

                @Override
                public void onFailure(Throwable caught) {
                  // never invoked
                }
              });
        }
      }
    });

    return activeToggle;
  }

  private static class MyTable extends FlexTable {
    private static int row = 0;

    private void addRow(String label, String value) {
      setWidget(row, 0, new Label(label + ":"));
      setWidget(row, 1, new Label(value));
      row++;
    }

    private void addRow(String label, Widget w) {
      setWidget(row, 0, new Label(label + ":"));
      setWidget(row, 1, w);
      row++;
    }
  }
}

@@ -4381,24 +4381,794 @@
 l());%0A    %7D%0A
+    t.addRow(%22Owner Group%22, new EditableValue(info.username(),%0A        info.owner() != null ? info.owner().name() : %22%22) %7B%0A      @Override%0A      protected void save(String serviceUser, final String newValue) %7B%0A        new RestApi(%22config%22).id(%22server%22)%0A            .view(Plugin.get().getPluginName(), %22serviceusers%22).id(serviceUser)%0A            .view(%22owner%22).put(newValue, new AsyncCallback%3CGroupInfo%3E() %7B%0A              @Override%0A              public void onSuccess(GroupInfo result) %7B%0A                updateValue(result != null ? result.name() : %22%22);%0A                Plugin.get().refresh();%0A              %7D%0A%0A              @Override%0A              public void onFailure(Throwable caught) %7B%0A                // never invoked%0A              %7D%0A            %7D);%0A      %7D%0A    %7D);%0A
     t.addRow

Allow to change owner group from service user screen